// Static-only demo server. No storage, sessions, request logs or upstreams. package main import ( "embed" "io" "io/fs" "log" "net/http" "os" "path" "strings" "time" ) //go:embed public var assets embed.FS func serve(w http.ResponseWriter, r *http.Request) { h := w.Header() policy := "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; worker-src 'self'; frame-src 'self'; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'" if r.URL.Path == "/worker.js" { policy = "default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; connect-src 'self'" } h.Set("Content-Security-Policy", policy) h.Set("X-Content-Type-Options", "nosniff") h.Set("Referrer-Policy", "no-referrer") h.Set("X-Frame-Options", "DENY") h.Set("Cross-Origin-Resource-Policy", "same-origin") h.Set("Permissions-Policy", "camera=(), microphone=(), geolocation=(), payment=()") if r.Method != "GET" && r.Method != "HEAD" { h.Set("Allow", "GET, HEAD") w.WriteHeader(405) return } name := strings.TrimPrefix(r.URL.Path, "/") if name == "health" { w.WriteHeader(204) return } if name == "" { name = "index.html" } if !fs.ValidPath(name) { http.NotFound(w, r) return } typ := map[string]string{".txt": "text/plain; charset=utf-8", ".html": "text/html; charset=utf-8", ".js": "text/javascript", ".css": "text/css", ".json": "application/json", ".wasm": "application/wasm", ".zip": "application/zip"}[path.Ext(name)] f, err := assets.Open("public/" + name) if err != nil || typ == "" { http.NotFound(w, r) return } defer f.Close() info, err := f.Stat() if err != nil || info.IsDir() { http.NotFound(w, r) return } h.Set("Content-Type", typ) h.Set("Cache-Control", "no-cache") http.ServeContent(w, r, name, time.Time{}, f.(io.ReadSeeker)) } func main() { if len(os.Args) == 2 && os.Args[1] == "-health" { c := http.Client{Timeout: 2 * time.Second} r, e := c.Get("http://127.0.0.1:8080/health") if e != nil || r.StatusCode != 204 { os.Exit(1) } r.Body.Close() return } server := http.Server{Addr: ":8080", Handler: http.HandlerFunc(serve), ReadHeaderTimeout: 4 * time.Second, ReadTimeout: 8 * time.Second, WriteTimeout: 45 * time.Second, IdleTimeout: 20 * time.Second, MaxHeaderBytes: 8192} log.Fatal(server.ListenAndServe()) }