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]>
62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
// 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"
|
|
"io/fs"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
// 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 tracker.user.js
|
|
userScript []byte
|
|
)
|
|
|
|
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)
|
|
}
|
|
|
|
// 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
|
|
// browser with Violentmonkey installed offers to install it, and Violentmonkey
|
|
// then checks the same URL for updates. It holds no secrets, so it is public.
|
|
func UserScript(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/javascript; charset=utf-8")
|
|
w.Write(userScript)
|
|
}
|