From 59b0ac03c938e4d51c8d4778b78fe1459f6a7363 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 20 Nov 2026 00:34:46 +1100 Subject: [PATCH] feat(dashboard): serve the live stream the Vue dashboard connects to Same frames as internal/webui/sse.go with a keepalive; signed-in only. Topics are added as each page moves onto the binary. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- api/openapi.yaml | 19 +++++++ internal/app/archie/api.go | 2 + internal/platform/httpapi/httpapi_test.go | 8 +++ modules/dashboard/stream.go | 60 +++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 modules/dashboard/stream.go diff --git a/api/openapi.yaml b/api/openapi.yaml index e7707129d..bfa022f38 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -4484,6 +4484,25 @@ paths: summary: Set the fields a source's events are grouped by tags: - Events + /api/stream: + get: + operationId: stream-dashboard + responses: + "200": + content: + text/event-stream: + schema: + type: string + description: "Server-sent events: one data frame of {\"topic\", \"data\"} per update, and a keepalive comment while idle" + default: + content: + application/problem+json: + schema: + $ref: "#/components/schemas/ErrorModel" + description: Error + summary: Stream live dashboard updates + tags: + - Dashboard /api/system/bootstrap: get: operationId: get-bootstrap diff --git a/internal/app/archie/api.go b/internal/app/archie/api.go index 123eb1276..5a08adb3c 100644 --- a/internal/app/archie/api.go +++ b/internal/app/archie/api.go @@ -10,6 +10,7 @@ import ( "github.com/samcharles93/archie-core/modules/approvals" "github.com/samcharles93/archie-core/modules/chat" "github.com/samcharles93/archie-core/modules/connectors" + "github.com/samcharles93/archie-core/modules/dashboard" "github.com/samcharles93/archie-core/modules/deliveries" "github.com/samcharles93/archie-core/modules/drafts" "github.com/samcharles93/archie-core/modules/events" @@ -49,6 +50,7 @@ func (m apiModules) register(api huma.API) { m.engine.Register(api, orgID, m.hear, m.judge, m.allow) m.drafts.Register(api) m.chat.Register(api) + dashboard.Register(api) } // Document returns the API's OpenAPI document as YAML. It makes the same diff --git a/internal/platform/httpapi/httpapi_test.go b/internal/platform/httpapi/httpapi_test.go index b84932cbe..64abee8ab 100644 --- a/internal/platform/httpapi/httpapi_test.go +++ b/internal/platform/httpapi/httpapi_test.go @@ -41,6 +41,8 @@ func TestOperationAccess(t *testing.T) { {name: "public sign-in from another origin", method: http.MethodPost, path: "/public", fetchSite: "cross-site", want: http.StatusForbidden}, {name: "signed-in without a credential", method: http.MethodGet, path: "/signed-in", want: http.StatusUnauthorized}, {name: "signed-in as a person", method: http.MethodGet, path: "/signed-in", caller: "person", want: http.StatusOK}, + {name: "a stream without a credential", method: http.MethodGet, path: "/stream", want: http.StatusUnauthorized}, + {name: "a stream as a person", method: http.MethodGet, path: "/stream", caller: "person", want: http.StatusOK}, {name: "admin without a credential", method: http.MethodGet, path: "/admin", want: http.StatusUnauthorized}, {name: "admin as a person", method: http.MethodGet, path: "/admin", caller: "person", want: http.StatusForbidden}, {name: "admin as the admin", method: http.MethodGet, path: "/admin", caller: "admin", want: http.StatusOK}, @@ -56,6 +58,12 @@ func TestOperationAccess(t *testing.T) { huma.Register(api, SignedIn(huma.Operation{OperationID: "signed-in-" + method, Method: method, Path: "/signed-in"}), reached) huma.Register(api, huma.Operation{OperationID: "admin-" + method, Method: method, Path: "/admin"}, reached) } + // A streamed response writes its own status, so it must still pass the + // guard before its body runs. + huma.Register(api, SignedIn(huma.Operation{OperationID: "stream", Method: http.MethodGet, Path: "/stream"}), + func(context.Context, *struct{}) (*huma.StreamResponse, error) { + return &huma.StreamResponse{Body: func(hc huma.Context) { hc.SetStatus(http.StatusOK) }}, nil + }) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { req := httptest.NewRequestWithContext(t.Context(), tt.method, tt.path, nil) diff --git a/modules/dashboard/stream.go b/modules/dashboard/stream.go new file mode 100644 index 000000000..3823b5cea --- /dev/null +++ b/modules/dashboard/stream.go @@ -0,0 +1,60 @@ +package dashboard + +import ( + "context" + "fmt" + "net/http" + "time" + + "github.com/danielgtaylor/huma/v2" + + "github.com/samcharles93/archie-core/internal/platform/httpapi" +) + +// keepalive is how often an idle stream writes a comment, so proxies keep +// the connection open and the browser notices one that died. +const keepalive = 25 * time.Second + +// Register adds the dashboard's live stream: the one EventSource the SPA +// keeps open, framing each update as `data: {"topic":…,"data":…}`. Nothing +// this binary publishes has a dashboard reader yet, so the stream carries +// only keepalives; it still tells the connection indicator the server is +// up, and a reconnect still makes every mounted page re-read. +func Register(api huma.API) { + huma.Register(api, httpapi.SignedIn(huma.Operation{ + OperationID: "stream-dashboard", Method: http.MethodGet, Path: "/api/stream", + Summary: "Stream live dashboard updates", Tags: []string{"Dashboard"}, + Responses: map[string]*huma.Response{"200": { + Description: "Server-sent events: one data frame of {\"topic\", \"data\"} per update, and a keepalive comment while idle", + Content: map[string]*huma.MediaType{"text/event-stream": {Schema: &huma.Schema{Type: huma.TypeString}}}, + }}, + }), stream) +} + +func stream(_ context.Context, _ *struct{}) (*huma.StreamResponse, error) { + return &huma.StreamResponse{Body: func(hc huma.Context) { + w := hc.BodyWriter() + flusher, ok := w.(http.Flusher) + if !ok { + hc.SetStatus(http.StatusInternalServerError) + return + } + hc.SetHeader("Content-Type", "text/event-stream") + hc.SetHeader("Cache-Control", "no-store") + hc.SetStatus(http.StatusOK) + flusher.Flush() + tick := time.NewTicker(keepalive) + defer tick.Stop() + for { + select { + case <-hc.Context().Done(): + return + case <-tick.C: + if _, err := fmt.Fprint(w, ": keepalive\n\n"); err != nil { + return + } + flusher.Flush() + } + } + }}, nil +}