Files
archie/modules/dashboard/stream.go
T
samandCopilot 59b0ac03c9 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>
2026-11-20 00:34:46 +11:00

61 lines
1.9 KiB
Go

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
}