37 lines
1.8 KiB
TypeScript
37 lines
1.8 KiB
TypeScript
import type { Handle } from '@sveltejs/kit';
|
|
import { migrate } from '$lib/server/db/migrate.js';
|
|
import { startWorker } from '$lib/server/pipeline/worker.js';
|
|
import { interruptDeployments } from '$lib/server/provisioning/history.js';
|
|
import { authenticationHandle } from '$lib/server/auth/guard.js';
|
|
import { invalidateFleet } from '$lib/server/pipeline/bus.js';
|
|
import { startPerformanceMonitor } from '$lib/server/performance.js';
|
|
import { withOperationSource } from '$lib/server/hosts/operation-context.js';
|
|
import { withHostRequestContext } from '$lib/server/hosts/request-context.js';
|
|
|
|
let booted = false;
|
|
function boot(): void {
|
|
if (booted) return;
|
|
booted = true;
|
|
migrate();
|
|
interruptDeployments();
|
|
startWorker();
|
|
startPerformanceMonitor();
|
|
}
|
|
|
|
// SvelteKit calls handle() for every request; using the first request as the
|
|
// boot trigger keeps the whole app single-process without needing an
|
|
// out-of-band init hook.
|
|
export const handle: Handle = async (input) => {
|
|
boot();
|
|
const started = performance.now();
|
|
const response = await withHostRequestContext(() => withOperationSource(`${input.event.request.method} ${input.event.route.id ?? 'Web request'}`,
|
|
() => authenticationHandle(input)));
|
|
const elapsed = performance.now() - started;
|
|
response.headers.append('Server-Timing', `app;dur=${elapsed.toFixed(1)}`);
|
|
if (process.env.EC_PERF_LOG === 'true' && elapsed > 250 && !input.event.route.id?.startsWith('/api/agent/')) {
|
|
console.info('[request]', { route: input.event.route.id, method: input.event.request.method, status: response.status, elapsedMs: Math.round(elapsed) });
|
|
}
|
|
if (input.event.locals.user && !['GET','HEAD','OPTIONS'].includes(input.event.request.method) && response.status < 400) invalidateFleet();
|
|
return response;
|
|
};
|