The binary serves the account routes the Vue client already calls: status,
setup, password and passkey sign-in, recovery, sign-out, OIDC, account
passkeys, password and recovery codes, personal tokens and capabilities.
The API authenticates a session cookie, a personal token or the bootstrap
admin token, and unsafe requests pass cross-origin protection. The
dashboard serves the Vue app at /.
Copied from internal/webui/{accounts,passkeys,personal_tokens,
api_capabilities,auth}.go, internal/infrastructure/oidc/{flow,oidc}.go and
internal/app/archieui/account_oidc.go, which archie-ui still imports.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
// Package dashboard serves the single binary's SPA: index.html and its
|
|
// assets from the embedded build. Every other route belongs to a module.
|
|
package dashboard
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpjson"
|
|
)
|
|
|
|
const page = "index.html"
|
|
|
|
// Routes serves the SPA from dist on mux. A path under /api/ that no module
|
|
// claimed is a JSON 404, never the page. With no build embedded, the page
|
|
// says how to build it.
|
|
//
|
|
// One pattern serves both: a method-scoped fallback beside the /api/ catch-all
|
|
// is a ServeMux conflict, since Go matches the more general pattern for the
|
|
// methods the other doesn't name.
|
|
func Routes(mux *http.ServeMux, dist fs.FS) {
|
|
mux.Handle("/", handler(dist))
|
|
}
|
|
|
|
func handler(dist fs.FS) http.Handler {
|
|
if dist == nil {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if strings.HasPrefix(r.URL.Path, "/api/") {
|
|
httpjson.Error(w, http.StatusNotFound, "no such route")
|
|
return
|
|
}
|
|
http.Error(w, "this binary was built without the dashboard; run task ui and rebuild", http.StatusNotFound)
|
|
})
|
|
}
|
|
files := http.FileServerFS(dist)
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if strings.HasPrefix(r.URL.Path, "/api/") {
|
|
httpjson.Error(w, http.StatusNotFound, "no such route")
|
|
return
|
|
}
|
|
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
|
w.Header().Set("Allow", "GET, HEAD")
|
|
http.Error(w, "the dashboard is read-only", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
name := strings.TrimPrefix(r.URL.Path, "/")
|
|
if name != "" && name != page {
|
|
if info, err := fs.Stat(dist, name); err == nil && !info.IsDir() {
|
|
if strings.HasPrefix(name, "assets/") {
|
|
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
|
}
|
|
files.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
}
|
|
// Every other path is a route inside the SPA.
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
http.ServeFileFS(w, r, dist, page)
|
|
})
|
|
}
|