diff --git a/data.json b/data.json deleted file mode 100644 index 123a4f6..0000000 --- a/data.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "job": "none", - "emails": [], - "newletter-sent": 0 -} \ No newline at end of file diff --git a/main.go b/main.go index 992bfa5..90a4713 100644 --- a/main.go +++ b/main.go @@ -22,112 +22,11 @@ func main() { 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.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { - tmpl, err := template.ParseFS(sourceRoot, "templates/main.html", "pages/index.html") - if err != nil { - log.Println("parse error:", err) - 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) - } - }) + mux.HandleFunc("GET /{$}", servePage("home", "templates/pages/index.html", sourceRoot, false)) + mux.HandleFunc("GET /research", servePage("research", "templates/pages/research.html", sourceRoot, true)) + mux.HandleFunc("GET /bookkeeping", servePage("bookkeeping", "templates/pages/bookkeeping.html", sourceRoot, true)) + mux.HandleFunc("GET /personal", servePage("personal", "templates/pages/interests.html", sourceRoot, true)) staticRoot, err := fs.Sub(sourceFS, "source/static") if err != nil { @@ -145,3 +44,29 @@ func main() { 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) + } + } +} diff --git a/source/pages/bookkeeping.html b/source/templates/pages/bookkeeping.html similarity index 100% rename from source/pages/bookkeeping.html rename to source/templates/pages/bookkeeping.html diff --git a/source/pages/index.html b/source/templates/pages/index.html similarity index 100% rename from source/pages/index.html rename to source/templates/pages/index.html diff --git a/source/pages/interests.html b/source/templates/pages/interests.html similarity index 100% rename from source/pages/interests.html rename to source/templates/pages/interests.html diff --git a/source/pages/research.html b/source/templates/pages/research.html similarity index 100% rename from source/pages/research.html rename to source/templates/pages/research.html