keyemail-dev/main.go

220 lines
6.3 KiB
Go
Raw Normal View History

2024-06-13 02:53:09 +00:00
package main
import (
"net/http"
"os"
"log"
"html/template"
2024-07-26 09:57:25 +00:00
"strings"
"strconv"
2024-06-13 02:53:09 +00:00
)
type Index struct {
WebpageTitle string
2024-07-26 09:57:25 +00:00
WebpageIcon string
WebpageKeywords string
2024-07-26 12:53:10 +00:00
WebpageDescription string
WebpageURL string
2024-07-26 09:57:25 +00:00
GalleryItems []GalleryItems
SocialItems []SocialItems
VideoItems []VideoItems
GalleryPhoto *GalleryItems
VideoContent *VideoItems
Nav bool
2024-06-13 02:53:09 +00:00
}
2024-07-26 12:53:10 +00:00
type responseWriter struct {
http.ResponseWriter
status int
}
2024-07-26 09:57:25 +00:00
type GalleryItems struct {
URL string
ID int
Title string
}
type SocialItems struct {
URL string
Name string
Username string
Icon string
Color string
}
type VideoItems struct {
URL string
ID int
Title string
Description string
Thumbnail string
}
var data = &Index {
WebpageTitle: "Keyemail",
WebpageIcon: "/src/assets/main/favicon.ico",
WebpageKeywords: "keyemail",
WebpageDescription: "Welcome to keyemail.dev! A profile based on Keyemail.",
2024-07-26 12:53:10 +00:00
WebpageURL: "https://keyemail.dev",
2024-07-26 09:57:25 +00:00
GalleryItems: galleryItems,
SocialItems: socialItems,
VideoItems: videoItems,
}
var currentData = &Index{}
var galleryItems = []GalleryItems {
{"/src/assets/gallery/2_cats.jpg", 1, "Resting Day"},
{"/src/assets/gallery/friend_cat.jpg", 2, "Orange Furr"},
{"/src/assets/gallery/miku_poster.jpg", 3, "Miku Spotted"},
{"/src/assets/gallery/networking.jpg", 4, "Wires of Networking"},
{"/src/assets/gallery/another_friend_cat.jpg", 5, "Night Cat"},
{"/src/assets/gallery/pc_case_stickers.jpg", 6, "I use arch BTW"},
{"/src/assets/gallery/movie_picture_1.jpg", 7, "Prime Cut Cafe"},
{"/src/assets/gallery/movie_picture_2.jpg", 8, "Big Fountain"},
{"/src/assets/gallery/bird.jpg", 9, "Birds Eye"},
{"/src/assets/gallery/japanese_garden_1.jpg", 10, "Pond of Fishes"},
{"/src/assets/gallery/japanese_garden_2.jpg", 11, "Waterfall Pond"},
{"/src/assets/gallery/japanese_garden_3.jpg", 12, "Bridge of Faith"},
{"/src/assets/gallery/japanese_garden_4.jpg", 13, "Little Rocks"},
{"/src/assets/gallery/japanese_garden_5.jpg", 14, "Arc over Pond"},
{"/src/assets/gallery/japanese_garden_6.jpg", 15, "Beautiful Pond"},
}
var socialItems = []SocialItems {
{"https://discord.gg/VTEn4zuh", "Discord", "keyemail", "/src/assets/icons/discord.svg", "5865F2"},
{"https://github.com/Keyemail", "GitHub", "Keyemail", "/src/assets/icons/github.svg", "171515"},
{"https://steamcommunity.com/id/keyemail/", "Steam", "Keyemail", "/src/assets/icons/steam.svg", "000000"},
{"https://open.spotify.com/user/316yuurxrw3zcprxsnrvgamxktp4", "Spotify", "Keyemail", "/src/assets/icons/spotify.svg", "1DB954"},
{"https://www.youtube.com/channel/UCCNkKG8XoZCh52vLCbXYy7g", "YouTube", "Keyemail", "/src/assets/icons/youtube.svg", "CD201F"},
{"https://www.instagram.com/keyemail1?igsh=OGQ5ZDc2ODk2ZA%3D%3D&utm_source=qr", "Instagram", "keyemail1", "/src/assets/icons/instagram.svg", "C13584"},
}
var videoItems = []VideoItems {
{"/src/assets/videos/JP-Installing-Windows-Project.mp4", 1, "JP Installing Windows Project", "This is a guide in japanese where I teach you how to install windows completely. This was made because of a JP Project I had to do.", "/src/assets/video_thumbnails/JP-Installing-Windows-Project.png"},
{"/src/assets/videos/orange-cat.mp4", 2, "Orange Cat", "Cute little orange cat that my friend has!! Hes so cute lol.", "/src/assets/video_thumbnails/orange-cat.png"},
}
func renderTemplate(w http.ResponseWriter, tmpl string, data *Index, nav bool) {
var templateFiles[]string
templateFiles = append(templateFiles, "src/pages/" + tmpl + ".tmpl", "src/index.tmpl", "src/components/navbar.tmpl")
templates, err := template.ParseFiles(templateFiles...)
2024-06-13 02:53:09 +00:00
if err != nil {
2024-07-26 09:57:25 +00:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2024-06-13 02:53:09 +00:00
return
}
2024-07-26 09:57:25 +00:00
data.Nav = nav
err = templates.ExecuteTemplate(w, "base", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
2024-06-13 02:53:09 +00:00
}
2024-07-26 09:57:25 +00:00
log.Println("GET /" + tmpl)
}
2024-06-13 02:53:09 +00:00
2024-07-26 09:57:25 +00:00
func galleryContentHandler(w http.ResponseWriter, req *http.Request) {
parts := strings.Split(req.URL.Path, "/")
if len(parts) == 3 && parts[1] == "gallery" {
idStr := parts[2]
id, err := strconv.Atoi(idStr)
if err != nil || id < 1 || id > len(galleryItems) {
renderTemplate(w, "404", data, true)
return
}
var item GalleryItems
for _, gi := range galleryItems {
if gi.ID == id {
item = gi
break
}
}
*currentData = *data
currentData.GalleryPhoto = &item
currentData.GalleryItems = nil
renderTemplate(w, "gallery_content", currentData, true)
return
}
renderTemplate(w, "404", data, true)
}
func videoContentHandler(w http.ResponseWriter, req *http.Request) {
parts := strings.Split(req.URL.Path, "/")
if len(parts) == 3 && parts[1] == "videos" {
idStr := parts[2]
id, err := strconv.Atoi(idStr)
if err!= nil || id < 1 || id > len(videoItems) {
renderTemplate(w, "404", data, true)
return
}
var item VideoItems
for _, gi := range videoItems {
if gi.ID == id {
item = gi
break
}
}
*currentData = *data
currentData.VideoContent = &item
currentData.VideoItems = nil
renderTemplate(w, "video_content", currentData, true)
2024-06-13 02:53:09 +00:00
return
}
2024-07-26 09:57:25 +00:00
renderTemplate(w, "404", data, true)
}
func videoHandler(w http.ResponseWriter, req *http.Request) {
2024-07-26 12:53:10 +00:00
renderTemplate(w, "video", data, true)
2024-07-26 09:57:25 +00:00
}
func socialsHandler(w http.ResponseWriter, req *http.Request) {
2024-07-26 12:53:10 +00:00
renderTemplate(w, "socials", data, true)
2024-07-26 09:57:25 +00:00
}
func galleryHandler(w http.ResponseWriter, req *http.Request) {
2024-07-26 12:53:10 +00:00
renderTemplate(w, "gallery", data, true)
2024-07-26 09:57:25 +00:00
}
func indexHandler(w http.ResponseWriter, req *http.Request) {
2024-07-26 13:04:55 +00:00
if req.URL.Path != "/" {
renderTemplate(w, "404", data, true)
return
}
2024-07-26 12:53:10 +00:00
renderTemplate(w, "home", data, true)
2024-06-13 02:53:09 +00:00
}
func main() {
if _, err := os.Stat("src/index.tmpl"); os.IsNotExist(err) {
log.Fatal("Template file does not exist:\n", err)
}
2024-07-26 09:57:25 +00:00
fs := http.FileServer(http.Dir("./src"))
http.Handle("/src/", http.StripPrefix("/src/", fs))
2024-06-13 02:53:09 +00:00
port := ":3939"
http.HandleFunc("/", indexHandler)
2024-07-26 09:57:25 +00:00
http.HandleFunc("/socials", socialsHandler)
http.HandleFunc("/gallery", galleryHandler)
http.HandleFunc("/videos", videoHandler)
http.HandleFunc("/gallery/", galleryContentHandler)
http.HandleFunc("/videos/", videoContentHandler)
2024-07-26 12:53:10 +00:00
2024-06-13 02:53:09 +00:00
log.Println("Server running at http://localhost" + port)
http.ListenAndServe(port, nil)
}