DNS-2: fix embedded web asset routes

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
Ubuntu 2026-08-03 18:22:00 +00:00
parent ae0604ab68
commit 7379d6e6e7
2 changed files with 29 additions and 3 deletions

12
main.go
View File

@ -91,13 +91,21 @@ func main() {
mux.HandleFunc("GET /api/health", func(w http.ResponseWriter, _ *http.Request) { mux.HandleFunc("GET /api/health", func(w http.ResponseWriter, _ *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}) })
assets, _ := fs.Sub(webFS, "web") mux.Handle("/web/", http.StripPrefix("/web", staticHandler()))
mux.Handle("/", http.FileServer(http.FS(assets))) mux.Handle("/", staticHandler())
addr := ":" + env("PORT", "8080") addr := ":" + env("PORT", "8080")
log.Printf("DNS Orbit listening on %s", addr) log.Printf("DNS Orbit listening on %s", addr)
log.Fatal(http.ListenAndServe(addr, securityHeaders(mux))) log.Fatal(http.ListenAndServe(addr, securityHeaders(mux)))
} }
func staticHandler() http.Handler {
assets, err := fs.Sub(webFS, "web")
if err != nil {
panic(err)
}
return http.FileServer(http.FS(assets))
}
func env(k, v string) string { func env(k, v string) string {
if x := os.Getenv(k); x != "" { if x := os.Getenv(k); x != "" {
return x return x

View File

@ -1,6 +1,10 @@
package main package main
import "testing" import (
"net/http"
"net/http/httptest"
"testing"
)
func TestNormalizeDomain(t *testing.T) { func TestNormalizeDomain(t *testing.T) {
cases := map[string]string{"example.com": "example.com", "https://WWW.Example.com/a": "www.example.com", "example.com/": "example.com"} cases := map[string]string{"example.com": "example.com", "https://WWW.Example.com/a": "www.example.com", "example.com/": "example.com"}
@ -16,3 +20,17 @@ func TestNormalizeDomain(t *testing.T) {
} }
} }
} }
func TestWebAssetRoutes(t *testing.T) {
mux := http.NewServeMux()
mux.Handle("/web/", http.StripPrefix("/web", staticHandler()))
mux.Handle("/", staticHandler())
for _, path := range []string{"/", "/web/app.js", "/web/style.css"} {
recorder := httptest.NewRecorder()
mux.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, path, nil))
if recorder.Code != http.StatusOK {
t.Errorf("GET %s returned %d, want 200", path, recorder.Code)
}
}
}