From 7379d6e6e7e04749b89e18018654d0d9d94a9440 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Mon, 3 Aug 2026 18:22:00 +0000 Subject: [PATCH] DNS-2: fix embedded web asset routes Co-authored-by: multica-agent --- main.go | 12 ++++++++++-- main_test.go | 20 +++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 371abed..3ad3d5d 100644 --- a/main.go +++ b/main.go @@ -91,13 +91,21 @@ func main() { mux.HandleFunc("GET /api/health", func(w http.ResponseWriter, _ *http.Request) { json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) }) - assets, _ := fs.Sub(webFS, "web") - mux.Handle("/", http.FileServer(http.FS(assets))) + mux.Handle("/web/", http.StripPrefix("/web", staticHandler())) + mux.Handle("/", staticHandler()) addr := ":" + env("PORT", "8080") log.Printf("DNS Orbit listening on %s", addr) 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 { if x := os.Getenv(k); x != "" { return x diff --git a/main_test.go b/main_test.go index 8a581af..86f6a36 100644 --- a/main_test.go +++ b/main_test.go @@ -1,6 +1,10 @@ package main -import "testing" +import ( + "net/http" + "net/http/httptest" + "testing" +) 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"} @@ -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) + } + } +}