move pages and make serve simplier

This commit is contained in:
samantha42
2026-09-11 10:04:29 +02:00
parent 989ae62915
commit b7cf68995d
6 changed files with 30 additions and 110 deletions
-5
View File
@@ -1,5 +0,0 @@
{
"job": "none",
"emails": [],
"newletter-sent": 0
}
+30 -105
View File
@@ -22,112 +22,11 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
// print exactly what got embedded
fs.WalkDir(sourceRoot, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.Println("walk error:", err)
return nil
}
log.Println("embedded:", path)
return nil
})
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("GET /{$}", servePage("home", "templates/pages/index.html", sourceRoot, false))
tmpl, err := template.ParseFS(sourceRoot, "templates/main.html", "pages/index.html") mux.HandleFunc("GET /research", servePage("research", "templates/pages/research.html", sourceRoot, true))
if err != nil { mux.HandleFunc("GET /bookkeeping", servePage("bookkeeping", "templates/pages/bookkeeping.html", sourceRoot, true))
log.Println("parse error:", err) mux.HandleFunc("GET /personal", servePage("personal", "templates/pages/interests.html", sourceRoot, true))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := struct {
Title string
Sub bool
}{
Title: "home",
Sub: false,
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// execute the base file by name — main.html is the entrypoint
if err := tmpl.ExecuteTemplate(w, "main.html", data); err != nil {
log.Println("execute error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
mux.HandleFunc("GET /research", func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFS(sourceRoot, "templates/main.html", "pages/research.html")
if err != nil {
log.Println("parse error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := struct {
Title string
Sub bool
}{
Title: "research",
Sub: true,
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// execute the base file by name — main.html is the entrypoint
if err := tmpl.ExecuteTemplate(w, "main.html", data); err != nil {
log.Println("execute error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
mux.HandleFunc("GET /bookkeeping", func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFS(sourceRoot, "templates/main.html", "pages/bookkeeping.html")
if err != nil {
log.Println("parse error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := struct {
Title string
Sub bool
}{
Title: "bookkeeping",
Sub: true,
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// execute the base file by name — main.html is the entrypoint
if err := tmpl.ExecuteTemplate(w, "main.html", data); err != nil {
log.Println("execute error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
mux.HandleFunc("GET /personal", func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFS(sourceRoot, "templates/main.html", "pages/interests.html")
if err != nil {
log.Println("parse error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := struct {
Title string
Sub bool
}{
Title: "personal",
Sub: true,
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// execute the base file by name — main.html is the entrypoint
if err := tmpl.ExecuteTemplate(w, "main.html", data); err != nil {
log.Println("execute error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
staticRoot, err := fs.Sub(sourceFS, "source/static") staticRoot, err := fs.Sub(sourceFS, "source/static")
if err != nil { if err != nil {
@@ -145,3 +44,29 @@ func main() {
log.Fatal(http.ListenAndServe(addr, mux)) log.Fatal(http.ListenAndServe(addr, mux))
} }
func servePage(name string, templatefile string, sourcefile fs.FS, sub bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFS(sourcefile, "templates/main.html", templatefile)
if err != nil {
log.Println("parse error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := struct {
Title string
Sub bool
}{
Title: name,
Sub: sub,
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// execute the base file by name — main.html is the entrypoint
if err := tmpl.ExecuteTemplate(w, "main.html", data); err != nil {
log.Println("execute error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}