keyemail-dev/main.go

215 lines
6.2 KiB
Go

package main
import (
"net/http"
"os"
"log"
"html/template"
"strings"
"strconv"
)
type Index struct {
WebpageTitle string
WebpageIcon string
WebpageKeywords string
WebpageDescription string
WebpageURL string
GalleryItems []GalleryItems
SocialItems []SocialItems
VideoItems []VideoItems
GalleryPhoto *GalleryItems
VideoContent *VideoItems
Nav bool
}
type responseWriter struct {
http.ResponseWriter
status int
}
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.",
WebpageURL: "https://keyemail.dev",
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...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data.Nav = nav
err = templates.ExecuteTemplate(w, "base", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
log.Println("GET /" + tmpl)
}
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)
return
}
renderTemplate(w, "404", data, true)
}
func videoHandler(w http.ResponseWriter, req *http.Request) {
renderTemplate(w, "video", data, true)
}
func socialsHandler(w http.ResponseWriter, req *http.Request) {
renderTemplate(w, "socials", data, true)
}
func galleryHandler(w http.ResponseWriter, req *http.Request) {
renderTemplate(w, "gallery", data, true)
}
func indexHandler(w http.ResponseWriter, req *http.Request) {
renderTemplate(w, "home", data, true)
}
func main() {
if _, err := os.Stat("src/index.tmpl"); os.IsNotExist(err) {
log.Fatal("Template file does not exist:\n", err)
}
fs := http.FileServer(http.Dir("./src"))
http.Handle("/src/", http.StripPrefix("/src/", fs))
port := ":3939"
http.HandleFunc("/", indexHandler)
http.HandleFunc("/socials", socialsHandler)
http.HandleFunc("/gallery", galleryHandler)
http.HandleFunc("/videos", videoHandler)
http.HandleFunc("/gallery/", galleryContentHandler)
http.HandleFunc("/videos/", videoContentHandler)
log.Println("Server running at http://localhost" + port)
http.ListenAndServe(port, nil)
}