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>
This commit is contained in:
samandCopilot committed 2026-11-20 00:34:46 +11:00
1 parent c12f6b7947
commit 59b0ac03c9
4 files changed
+89

No files matched your search

+19
View File
@@ -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
+2
View File
@@ -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
@@ -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)
+60
View File
@@ -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
}