Move the dashboard and setup pages to a Vue SPA
The pages are now a Vue 3 + Vite + TypeScript app in web/, built into internal/web/dist/app and embedded in the binary. Go still serves index.html for / and /setup behind the admin gate, plus the hashed /assets/* (public, cached forever). The router takes its base from the current URL, so it works under nginx's /tracker/ prefix unchanged. Only dist/.gitkeep is committed, so go build and go test need no Node; an unbuilt binary shows a "run make web" page at /. The Dockerfile gains a Node stage, CI typechecks and builds the app, and make build/run build it first. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+36
-23
@@ -1,42 +1,55 @@
|
||||
// Package web holds the dashboard pages, which read /api/v1 with the main
|
||||
// site's session cookies, and the browser userscript.
|
||||
// Package web serves the dashboard, a Vue app in /web that reads /api/v1 with
|
||||
// the main site's session cookies, and the browser userscript.
|
||||
package web
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"embed"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed index.html
|
||||
Index []byte
|
||||
// LoginRedirect is served instead of a page when the visitor isn't the
|
||||
// dist holds the Vue build in dist/app (`make web`). Only dist/.gitkeep is
|
||||
// committed, so the Go code builds and tests without Node.
|
||||
//go:embed all:dist
|
||||
dist embed.FS
|
||||
// LoginRedirect is served instead of the app when the visitor isn't the
|
||||
// site's admin.
|
||||
//go:embed login.html
|
||||
LoginRedirect []byte
|
||||
//go:embed setup.html
|
||||
Setup []byte
|
||||
//go:embed tracker.user.js
|
||||
userScript []byte
|
||||
//go:embed style.css
|
||||
style []byte
|
||||
)
|
||||
|
||||
// Style serves the stylesheet the pages share. It's public: the login
|
||||
// redirect doesn't use it, but there's nothing private in it either.
|
||||
func Style(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/css; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Write(style)
|
||||
var app, _ = fs.Sub(dist, "dist/app")
|
||||
|
||||
// notBuilt stands in for the app in a binary built without `make web`.
|
||||
var notBuilt = []byte(`<!doctype html><meta charset="utf-8"><title>Activity Tracker</title>
|
||||
<p>The dashboard wasn't built into this binary. Run <code>make web</code> and rebuild.</p>`)
|
||||
|
||||
// App serves the app's index.html, for every route the app handles.
|
||||
func App(w http.ResponseWriter, r *http.Request) {
|
||||
page, err := fs.ReadFile(app, "index.html")
|
||||
if err != nil {
|
||||
page = notBuilt
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Write(page)
|
||||
}
|
||||
|
||||
// Page serves a static HTML page.
|
||||
func Page(page []byte) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Write(page)
|
||||
}
|
||||
// Assets serves the app's scripts and styles. Their names carry a content
|
||||
// hash, so they can be cached forever, and they hold no secrets, so they are
|
||||
// public.
|
||||
func Assets() http.Handler {
|
||||
files := http.FileServerFS(app)
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := fs.Stat(app, strings.TrimPrefix(r.URL.Path, "/")); err == nil {
|
||||
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
||||
}
|
||||
files.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// UserScript serves the browser collector. Opening a *.user.js URL in a
|
||||
|
||||
Reference in New Issue
Block a user