package httpapi import ( "embed" "mime" "net/http" "path/filepath" "strings" ) //go:embed static/index.html static/style.css static/app.js var staticFS embed.FS // staticFiles holds the embedded static files. var staticFiles = map[string]string{ "/": "static/index.html", "/index.html": "static/index.html", "/style.css": "static/style.css", "/app.js": "static/app.js", } // frontendHandler serves the embedded frontend files. func frontendHandler(w http.ResponseWriter, r *http.Request) { path := r.URL.Path // Look up the file filePath, ok := staticFiles[path] if !ok { // Default to index.html for unknown routes (SPA-style) filePath = staticFiles["/"] } data, err := staticFS.ReadFile(filePath) if err != nil { http.Error(w, "Not Found", http.StatusNotFound) return } // Set content type based on file extension ext := strings.ToLower(filepath.Ext(filePath)) contentType := mime.TypeByExtension(ext) if contentType == "" { switch ext { case ".html": contentType = "text/html; charset=utf-8" case ".css": contentType = "text/css; charset=utf-8" case ".js": contentType = "application/javascript; charset=utf-8" } } w.Header().Set("Content-Type", contentType) w.Header().Set("Cache-Control", "no-cache") w.Write(data) } // SetupFrontend registers the frontend handler on the given ServeMux. func SetupFrontend(mux *http.ServeMux) { mux.HandleFunc("/", frontendHandler) }