DNS-2: fix embedded web asset routes
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
parent
ae0604ab68
commit
7379d6e6e7
12
main.go
12
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
|
||||
|
||||
20
main_test.go
20
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user