// 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(`Activity Tracker

The dashboard wasn't built into this binary. Run make web and rebuild.

`) // 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) }