refactor(archie,webui,gateway): remove the dashboard, which now ships from archie-frontend
This commit is contained in:
636 files changed
+77
-44027
No files matched your search
@@ -1,69 +0,0 @@
|
||||
// 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"
|
||||
|
||||
// csp is the browser-side backstop for chat-rendered Markdown: no remote
|
||||
// image may load, so a reply cannot leak data through an image request. It
|
||||
// is image-only on purpose; every other directive would risk breaking the
|
||||
// SPA's scripts and styles.
|
||||
const csp = "img-src 'self' data:"
|
||||
|
||||
// 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
|
||||
}
|
||||
w.Header().Set("Content-Security-Policy", csp)
|
||||
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
|
||||
}
|
||||
w.Header().Set("Content-Security-Policy", csp)
|
||||
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)
|
||||
})
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
// TestRoutesServeThePageBesideTheAPICatchAll guards a boot-time panic: a
|
||||
// method-scoped "GET /" fallback cannot sit beside the "/api/" catch-all on one
|
||||
// ServeMux, so the binary crashed on start with the dashboard built in. It also
|
||||
// pins what each path answers: an unclaimed API path is a JSON 404 rather than
|
||||
// the page, and the page is read-only.
|
||||
func TestRoutesServeThePageBesideTheAPICatchAll(t *testing.T) {
|
||||
dist := fstest.MapFS{
|
||||
page: &fstest.MapFile{Data: []byte("<html>archie</html>")},
|
||||
"assets/app-1.js": &fstest.MapFile{Data: []byte("console.log(1)")},
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
dist fs.FS
|
||||
method string
|
||||
path string
|
||||
wantStatus int
|
||||
wantContent string
|
||||
// wantCSP pins the image-only Content-Security-Policy backstop on
|
||||
// every non-API response (#2073); API fallbacks must not carry it.
|
||||
wantCSP string
|
||||
}{
|
||||
{name: "the page serves a client route", dist: dist, method: http.MethodGet, path: "/runs/42", wantStatus: http.StatusOK, wantContent: "<html>archie</html>", wantCSP: "img-src 'self' data:"},
|
||||
{name: "an asset serves as itself", dist: dist, method: http.MethodGet, path: "/assets/app-1.js", wantStatus: http.StatusOK, wantContent: "console.log(1)", wantCSP: "img-src 'self' data:"},
|
||||
{name: "an unclaimed API path is JSON", dist: dist, method: http.MethodGet, path: "/api/nope", wantStatus: http.StatusNotFound, wantContent: "no such route"},
|
||||
{name: "an API path denies every method", dist: dist, method: http.MethodPost, path: "/api/nope", wantStatus: http.StatusNotFound, wantContent: "no such route"},
|
||||
{name: "the page is read-only", dist: dist, method: http.MethodPost, path: "/runs", wantStatus: http.StatusMethodNotAllowed, wantContent: "read-only", wantCSP: "img-src 'self' data:"},
|
||||
{name: "a build without the dashboard says how to make it", method: http.MethodGet, path: "/runs", wantStatus: http.StatusNotFound, wantContent: "task ui", wantCSP: "img-src 'self' data:"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
Routes(mux, tt.dist)
|
||||
w := httptest.NewRecorder()
|
||||
mux.ServeHTTP(w, httptest.NewRequestWithContext(t.Context(), tt.method, tt.path, nil))
|
||||
if w.Code != tt.wantStatus {
|
||||
t.Fatalf("status = %d, want %d", w.Code, tt.wantStatus)
|
||||
}
|
||||
if body := w.Body.String(); !strings.Contains(body, tt.wantContent) {
|
||||
t.Fatalf("body = %q, want it to contain %q", body, tt.wantContent)
|
||||
}
|
||||
if csp := w.Header().Get("Content-Security-Policy"); csp != tt.wantCSP {
|
||||
t.Fatalf("Content-Security-Policy = %q, want %q", csp, tt.wantCSP)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
// Package dashboard serves the dashboard's live event stream.
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
|
||||
Reference in new issue
Block a user