package main
import (
"embed"
"html/template"
"io/fs"
"log"
"net/http"
)
//go:embed source static
var sourceFS embed.FS
func main() {
staticRoot, err := fs.Sub(sourceFS, "source")
if err != nil {
log.Fatal(err)
}
// print exactly what got embedded
fs.WalkDir(staticRoot, ".", 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(staticRoot, "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(staticRoot, "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(staticRoot, "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 /automation", func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFS(staticRoot, "templates/main.html", "pages/automation.html")
if err != nil {
log.Println("parse error:", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := struct {
Title string
Sub bool
}{
Title: "automation",
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 /interests", func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFS(staticRoot, "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: "interests",
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)
}
})
assetsRoot, err := fs.Sub(sourceFS, "static")
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServerFS(assetsRoot)))
log.Println("running on http://localhost:8081/")
log.Fatal(http.ListenAndServe(":8081", mux))
}