move pages and make serve simplier
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user