141 lines
3.6 KiB
Go
141 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"flag"
|
|
"fmt"
|
|
"html/template"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
//go:embed source static
|
|
var sourceFS embed.FS
|
|
|
|
func main() {
|
|
port := flag.Int("port", 8081, "port to listen on")
|
|
flag.Parse()
|
|
|
|
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 /personal", 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: "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)
|
|
}
|
|
})
|
|
|
|
assetsRoot, err := fs.Sub(sourceFS, "static")
|
|
|
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServerFS(assetsRoot)))
|
|
|
|
addr := fmt.Sprintf(":%d", *port)
|
|
log.Printf("running on http://localhost%s/\n", addr)
|
|
log.Fatal(http.ListenAndServe(addr, mux))
|
|
|
|
}
|