feat(web): push live updates to the dashboard
Reading a page and wondering whether it is still true is a poor way to watch a deployment, and polling every page on a timer costs far more than it returns. The server keeps one `text/event-stream` per open tab and publishes on it: a write once it has actually succeeded, tagged with the page it concerns, a deployment when it starts and when it ends, an incoming Gitea webhook, plus a tick and a heartbeat that stops proxies closing an idle stream. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
6 files changed
+351
-1
No files matched your search
+11
-1
@@ -8,6 +8,7 @@ import { handleGiteaWebhook } from '../webhooks/gitea-webhook.js';
|
||||
import { registerApiRoutes } from './api/index.js';
|
||||
import { authenticate, assertSameOrigin } from './auth/session.js';
|
||||
import { authorize } from './auth/guard.js';
|
||||
import { broadcast, closeEventStreams, topicForPath } from './events.js';
|
||||
import { HttpError, readJsonBody, readRawBody, sendEmpty, sendJson, sendStatic } from './http.js';
|
||||
import { createRouter } from './router.js';
|
||||
|
||||
@@ -102,8 +103,15 @@ export function createHandler({ client = null, setupOnly = false, startedAt = Da
|
||||
context,
|
||||
});
|
||||
|
||||
// A handler that wrote the response itself (a redirect) returns nothing.
|
||||
// A handler that wrote the response itself (a redirect, an event stream)
|
||||
// returns nothing.
|
||||
if (!response.headersSent) sendJson(response, 200, result ?? { ok: true });
|
||||
|
||||
// Announce the change only once it has gone through, so a dashboard never
|
||||
// refreshes on a write that was rejected.
|
||||
if (request.method !== 'GET') {
|
||||
broadcast(topicForPath(pathname), { path: pathname, method: request.method });
|
||||
}
|
||||
} catch (error) {
|
||||
const status = statusOf(error);
|
||||
if (status >= 500) log.error(`${request.method} ${pathname} failed`, error);
|
||||
@@ -127,6 +135,8 @@ export function startWebServer({ client = null, setupOnly = false } = {}) {
|
||||
log.info(`Dashboard listening on ${env.web.host}:${env.web.port}`, { url, setupOnly });
|
||||
});
|
||||
server.on('error', (error) => log.error('Dashboard server error', error));
|
||||
// Open event streams would otherwise keep the server from closing.
|
||||
server.on('close', closeEventStreams);
|
||||
return server;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user