This commit is contained in:
root
2026-09-20 14:16:41 +02:00
parent 0798933b05
commit b6d1909d7f
56 changed files with 3752 additions and 1989 deletions
+38 -7
View File
@@ -6,11 +6,12 @@ import (
"encoding/json"
"io/fs"
"net/http"
"os"
"sort"
"strings"
"wh/docs"
"wh/examples"
"halcyon/docs"
"halcyon/examples"
)
//go:embed static
@@ -18,15 +19,18 @@ var static embed.FS
// Handler serves the site. Mount it as the catch-all route; more specific API
// routes registered on the same mux take precedence.
func Handler() http.Handler {
//
// manualPath is the rendered PDF manual; it is opened on every request, so a
// new render is served as soon as it is renamed into place, without a restart.
func Handler(manualPath string) http.Handler {
sub, _ := fs.Sub(static, "static")
files := http.FileServerFS(sub)
mux := http.NewServeMux()
mux.Handle("GET /", files)
mux.HandleFunc("GET /manual.txt", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write(docs.Manual)
mux.HandleFunc("GET /manual.md", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/markdown; charset=utf-8")
w.Write(docs.Markdown)
})
mux.HandleFunc("GET /examples/index.json", func(w http.ResponseWriter, r *http.Request) {
entries, _ := fs.ReadDir(examples.FS, ".")
@@ -41,7 +45,34 @@ func Handler() http.Handler {
json.NewEncoder(w).Encode(names)
})
mux.Handle("GET /examples/", http.StripPrefix("/examples/", http.FileServerFS(examples.FS)))
return secure(mux)
top := http.NewServeMux()
top.HandleFunc("GET /manual.pdf", manualPDF(manualPath)) // not under the site's CSP: PDF viewers need their own resources
top.Handle("/", secure(mux))
return top
}
// manualPDF serves the manual rendered separately from the app.
func manualPDF(path string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
f, err := os.Open(path)
if err != nil {
http.Error(w, "The PDF manual has not been rendered yet (run scripts/render-manual.sh). "+
"The Markdown source is at /manual.md.", http.StatusNotFound)
return
}
defer f.Close()
st, err := f.Stat()
if err != nil {
http.Error(w, "cannot read the PDF manual", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/pdf")
w.Header().Set("Content-Disposition", `inline; filename="halcyon-hc33-manual.pdf"`)
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Cache-Control", "no-cache") // revalidate, so a fresh render shows up
http.ServeContent(w, r, "manual.pdf", st.ModTime(), f) // ranges and If-Modified-Since
}
}
// secure adds headers appropriate to a page that loads only its own assets.