fix(webui): move the live stream to /api/stream so reloading /events serves the page

This commit is contained in:
sam committed 2026-09-23 21:16:29 +10:00
1 parent 9de96c1d42
commit 36e69724a4
10 files changed
+43 -19

No files matched your search

+1 -1
View File
@@ -14,7 +14,7 @@ import (
//
// This handler is transport only: parsing the log format belongs to the
// logging package, which owns it. Live output reaches the dashboard over the
// existing /events stream; this endpoint exists so the view has history from
// existing /api/stream stream; this endpoint exists so the view has history from
// before the browser connected, which is the whole reason file logging landed
// first.
func (s *Server) handleLogs(w http.ResponseWriter, r *http.Request) {
+31 -6
View File
@@ -85,7 +85,7 @@ func TestEventPumpKeepsItsWatermarkAcrossAStoreFailure(t *testing.T) {
}
// The bead's headline claim: an event written to the State Store by another
// process reaches a browser hanging on /events, with no in-process bus.
// process reaches a browser hanging on /api/stream, with no in-process bus.
func TestEventPumpReachesALiveSSEClient(t *testing.T) {
srv := newRemoteTestServer(t)
pump := newTestPump(t, srv)
@@ -123,7 +123,7 @@ func TestEventPumpReachesALiveSSEClient(t *testing.T) {
}
}
// newSSETestServer serves the dashboard's real route set, so /events goes
// newSSETestServer serves the dashboard's real route set, so /api/stream goes
// through requireToken and the mux exactly as a browser reaches it.
func newSSETestServer(t *testing.T, srv *Server) *httptest.Server {
t.Helper()
@@ -132,25 +132,25 @@ func newSSETestServer(t *testing.T, srv *Server) *httptest.Server {
return ts
}
// openSSEStream connects to /events and relays each response line on a
// openSSEStream connects to /api/stream and relays each response line on a
// channel. The request context ends the handler, which is what lets the
// test server close.
func openSSEStream(t *testing.T, ctx context.Context, ts *httptest.Server) <-chan string {
t.Helper()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/events", nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/api/stream", nil)
if err != nil {
t.Fatalf("sse request: %v", err)
}
resp, err := ts.Client().Do(req)
if err != nil {
t.Fatalf("GET /events: %v", err)
t.Fatalf("GET /api/stream: %v", err)
}
// Closed at cleanup rather than by the reader goroutine: the body is the
// live stream and must outlive this function, but it still has exactly
// one owner and one close.
t.Cleanup(func() { _ = resp.Body.Close() })
if resp.StatusCode != http.StatusOK {
t.Fatalf("GET /events = %d, want 200", resp.StatusCode)
t.Fatalf("GET /api/stream = %d, want 200", resp.StatusCode)
}
lines := make(chan string, 64)
go func() {
@@ -243,3 +243,28 @@ func TestPumpPriming(t *testing.T) {
})
}
}
// A dashboard route reloaded in the browser must get the app shell. The live
// stream once sat at /events, the same path as the Events page, so a reload
// there rendered the raw event stream instead of the page.
func TestDashboardRoutesServeTheAppShell(t *testing.T) {
ts := newSSETestServer(t, &Server{})
for _, path := range []string{"/events", "/events?tab=inspector"} {
t.Run(path, func(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+path, nil)
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("GET %s: %v", path, err)
}
defer resp.Body.Close()
if ct := resp.Header.Get("Content-Type"); strings.HasPrefix(ct, "text/event-stream") {
t.Fatalf("GET %s served the event stream, want the app shell", path)
}
})
}
}
+1 -1
View File
@@ -310,7 +310,7 @@ func (s *Server) Handler() http.Handler {
s.registerChatRoutes(mux)
mux.HandleFunc("GET /health/detailed", s.handleHealthDetailed)
mux.HandleFunc("GET /events", s.handleSSE)
mux.HandleFunc("GET /api/stream", s.handleSSE)
mux.Handle("GET /", s.assets())
top := http.NewServeMux()
+1 -1
View File
@@ -252,7 +252,7 @@ func TestSSESinceDegradesUnparseableCursorToBeginning(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/events", nil)
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/stream", nil)
if tt.since != "" {
q := req.URL.Query()
q.Set("since", tt.since)
+4 -4
View File
@@ -316,7 +316,7 @@ func TestHandleSSEEventsSinceError(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/events?since=0", nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/api/stream?since=0", nil)
if err != nil {
t.Fatal(err)
}
@@ -362,7 +362,7 @@ func TestHandleSSEUsesLastEventIDForCatchUp(t *testing.T) {
ts := httptest.NewServer(srv.Handler())
t.Cleanup(ts.Close)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/events", nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/api/stream", nil)
if err != nil {
t.Fatal(err)
}
@@ -407,7 +407,7 @@ func TestHandleSSEBacklogAndLive(t *testing.T) {
// The request carries t.Context() so the SSE stream is torn down when
// the test ends rather than leaking the connection and its goroutine.
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/events", nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/api/stream", nil)
if err != nil {
t.Fatal(err)
}
@@ -454,7 +454,7 @@ func TestHandleSSEFiltersTurnCompletedFromLiveBroadcast(t *testing.T) {
ts := httptest.NewServer(srv.Handler())
t.Cleanup(ts.Close)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/events", nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/api/stream", nil)
if err != nil {
t.Fatal(err)
}