diff --git a/.gitignore b/.gitignore index 8325f1cb..488373ff 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ # go generate /cmd/hakurei/LICENSE -/cmd/pkgserver/ui/static/*.js +/cmd/pkgserver/internal/ui/static /internal/pkg/testdata/testtool /internal/rosa/hakurei_current.tar.gz diff --git a/cmd/pkgserver/ui/index.html b/cmd/pkgserver/internal/ui/index.html similarity index 100% rename from cmd/pkgserver/ui/index.html rename to cmd/pkgserver/internal/ui/index.html diff --git a/cmd/pkgserver/ui/index.ts b/cmd/pkgserver/internal/ui/index.ts similarity index 100% rename from cmd/pkgserver/ui/index.ts rename to cmd/pkgserver/internal/ui/index.ts diff --git a/cmd/pkgserver/ui/static/style.css b/cmd/pkgserver/internal/ui/style.css similarity index 100% rename from cmd/pkgserver/ui/static/style.css rename to cmd/pkgserver/internal/ui/style.css diff --git a/cmd/pkgserver/ui/tsconfig.json b/cmd/pkgserver/internal/ui/tsconfig.json similarity index 100% rename from cmd/pkgserver/ui/tsconfig.json rename to cmd/pkgserver/internal/ui/tsconfig.json diff --git a/cmd/pkgserver/internal/ui/ui.go b/cmd/pkgserver/internal/ui/ui.go new file mode 100644 index 00000000..fbee1b7f --- /dev/null +++ b/cmd/pkgserver/internal/ui/ui.go @@ -0,0 +1,21 @@ +// Package ui holds the static web UI. +package ui + +import "net/http" + +func serveWebUI(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") + w.Header().Set("Pragma", "no-cache") + w.Header().Set("Expires", "0") + w.Header().Set("X-Content-Type-Options", "nosniff") + w.Header().Set("X-XSS-Protection", "1") + w.Header().Set("X-Frame-Options", "DENY") + + http.ServeFileFS(w, r, content, "static/index.html") +} + +// Register arranges for mux to serve the embedded frontend. +func Register(mux *http.ServeMux) { + mux.Handle("GET /", http.FileServer(http.FS(content))) + mux.HandleFunc("GET /{$}", serveWebUI) +} diff --git a/cmd/pkgserver/internal/ui/ui_full.go b/cmd/pkgserver/internal/ui/ui_full.go new file mode 100644 index 00000000..232f5e65 --- /dev/null +++ b/cmd/pkgserver/internal/ui/ui_full.go @@ -0,0 +1,10 @@ +//go:build frontend + +package ui + +import "embed" + +//go:generate tsc +//go:generate cp index.html style.css static +//go:embed static +var content embed.FS diff --git a/cmd/pkgserver/ui_stub.go b/cmd/pkgserver/internal/ui/ui_stub.go similarity index 84% rename from cmd/pkgserver/ui_stub.go rename to cmd/pkgserver/internal/ui/ui_stub.go index bdd691da..fea38491 100644 --- a/cmd/pkgserver/ui_stub.go +++ b/cmd/pkgserver/internal/ui/ui_stub.go @@ -1,6 +1,6 @@ //go:build !frontend -package main +package ui import "testing/fstest" diff --git a/cmd/pkgserver/main.go b/cmd/pkgserver/main.go index 82165a03..6889dfe1 100644 --- a/cmd/pkgserver/main.go +++ b/cmd/pkgserver/main.go @@ -11,6 +11,7 @@ import ( "time" "hakurei.app/check" + "hakurei.app/cmd/pkgserver/internal/ui" "hakurei.app/command" "hakurei.app/internal/pkg" "hakurei.app/internal/rosa" @@ -81,7 +82,7 @@ func main() { } }() var mux http.ServeMux - uiRoutes(&mux) + ui.Register(&mux) index.registerAPI(&mux) server := http.Server{ Addr: flagAddr, diff --git a/cmd/pkgserver/ui.go b/cmd/pkgserver/ui.go deleted file mode 100644 index 54ab2696..00000000 --- a/cmd/pkgserver/ui.go +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import "net/http" - -func serveWebUI(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") - w.Header().Set("Pragma", "no-cache") - w.Header().Set("Expires", "0") - w.Header().Set("X-Content-Type-Options", "nosniff") - w.Header().Set("X-XSS-Protection", "1") - w.Header().Set("X-Frame-Options", "DENY") - - http.ServeFileFS(w, r, content, "ui/index.html") -} -func serveStaticContent(w http.ResponseWriter, r *http.Request) { - switch r.URL.Path { - case "/static/style.css": - http.ServeFileFS(w, r, content, "ui/static/style.css") - case "/favicon.ico": - http.ServeFileFS(w, r, content, "ui/static/favicon.ico") - case "/static/index.js": - http.ServeFileFS(w, r, content, "ui/static/index.js") - default: - http.NotFound(w, r) - - } -} - -func uiRoutes(mux *http.ServeMux) { - mux.HandleFunc("GET /{$}", serveWebUI) - mux.HandleFunc("GET /favicon.ico", serveStaticContent) - mux.HandleFunc("GET /static/", serveStaticContent) -} diff --git a/cmd/pkgserver/ui/static/favicon.ico b/cmd/pkgserver/ui/static/favicon.ico deleted file mode 100644 index 2fefdfd7..00000000 Binary files a/cmd/pkgserver/ui/static/favicon.ico and /dev/null differ diff --git a/cmd/pkgserver/ui_full.go b/cmd/pkgserver/ui_full.go deleted file mode 100644 index f7f730c6..00000000 --- a/cmd/pkgserver/ui_full.go +++ /dev/null @@ -1,9 +0,0 @@ -//go:build frontend - -package main - -import "embed" - -//go:generate tsc -p ui -//go:embed ui/* -var content embed.FS