2025-11-03 12:38:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"html/template"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
dashboard = newBuiltTemplate("dashboard", "base")
|
2025-11-04 00:02:51 +00:00
|
|
|
root = newBuiltTemplate("root", "base")
|
|
|
|
|
signup = newBuiltTemplate("signup", "base")
|
2025-11-03 12:38:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type BuiltTemplate struct {
|
|
|
|
|
files []string
|
|
|
|
|
template *template.Template
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Link struct {
|
|
|
|
|
Href string
|
|
|
|
|
Title string
|
|
|
|
|
}
|
|
|
|
|
type ContentDashboard struct {
|
|
|
|
|
BabbleLinks []Link
|
|
|
|
|
Username string
|
|
|
|
|
}
|
|
|
|
|
type ContentRoot struct {
|
|
|
|
|
BabbleLinks []Link
|
|
|
|
|
}
|
2025-11-04 00:02:51 +00:00
|
|
|
type ContentSignup struct {
|
|
|
|
|
}
|
2025-11-03 12:38:47 +00:00
|
|
|
|
|
|
|
|
func (bt *BuiltTemplate) ExecuteTemplate(w io.Writer, data any) error {
|
|
|
|
|
name := bt.files[0] + ".html"
|
|
|
|
|
if bt.template == nil {
|
|
|
|
|
templ := parseFromDisk(bt.files)
|
|
|
|
|
if templ == nil {
|
|
|
|
|
w.Write([]byte("Failed to read from disk"))
|
|
|
|
|
return errors.New("Template parsing failed")
|
|
|
|
|
}
|
|
|
|
|
return templ.ExecuteTemplate(w, name, data)
|
|
|
|
|
} else {
|
|
|
|
|
return bt.template.ExecuteTemplate(w, name, data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func htmlDashboard(w io.Writer, path string, username string) error {
|
|
|
|
|
data := ContentDashboard{
|
|
|
|
|
Username: username,
|
|
|
|
|
}
|
|
|
|
|
return dashboard.ExecuteTemplate(w, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func htmlRoot(w io.Writer, path string) error {
|
|
|
|
|
data := ContentRoot{
|
|
|
|
|
}
|
|
|
|
|
return root.ExecuteTemplate(w, data)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-04 00:02:51 +00:00
|
|
|
func htmlSignup(w io.Writer, path string) error {
|
|
|
|
|
data := ContentSignup{
|
|
|
|
|
}
|
|
|
|
|
return signup.ExecuteTemplate(w, data)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-03 12:38:47 +00:00
|
|
|
func makeFuncMap() template.FuncMap {
|
|
|
|
|
funcMap := template.FuncMap{}
|
|
|
|
|
return funcMap
|
|
|
|
|
}
|
|
|
|
|
func newBuiltTemplate(files ...string) BuiltTemplate {
|
|
|
|
|
files_on_disk := true
|
|
|
|
|
for _, f := range files {
|
|
|
|
|
full_path := "templates/" + f + ".html"
|
|
|
|
|
_, err := os.Stat(full_path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
files_on_disk = false
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if files_on_disk {
|
|
|
|
|
return BuiltTemplate{
|
|
|
|
|
files: files,
|
|
|
|
|
template: nil,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return BuiltTemplate{
|
|
|
|
|
files: files,
|
|
|
|
|
template: parseEmbedded(files),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseEmbedded(files []string) *template.Template {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseFromDisk(files []string) *template.Template {
|
|
|
|
|
funcMap := makeFuncMap()
|
|
|
|
|
paths := make([]string, 0)
|
|
|
|
|
for _, f := range files {
|
|
|
|
|
paths = append(paths, "templates/"+f+".html")
|
|
|
|
|
}
|
|
|
|
|
name := files[0] + ".html"
|
|
|
|
|
templ, err := template.New(name).Funcs(funcMap).ParseFiles(paths...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("TEMPLATE FAILED", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return templ
|
|
|
|
|
}
|