49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
import express from 'express';
|
|
import pino from 'pino';
|
|
import pinoHttp from 'pino-http';
|
|
import { assertSafeWorkerRuntimeConfig } from './lib/runtime-safety.js';
|
|
|
|
const logger = pino({ level: process.env.LOG_LEVEL || 'info' });
|
|
|
|
export function createWorkerApp({ dispatch = async () => ({}) } = {}) {
|
|
assertSafeWorkerRuntimeConfig();
|
|
const app = express();
|
|
|
|
app.use(
|
|
pinoHttp({
|
|
logger,
|
|
})
|
|
);
|
|
app.use(express.json({ limit: '256kb' }));
|
|
|
|
app.get('/health', (_req, res) => {
|
|
res.status(200).json({ ok: true, service: 'notification-worker-v2' });
|
|
});
|
|
|
|
app.get('/readyz', (_req, res) => {
|
|
res.status(200).json({ ok: true, service: 'notification-worker-v2' });
|
|
});
|
|
|
|
app.post('/tasks/dispatch-notifications', async (req, res) => {
|
|
try {
|
|
const summary = await dispatch();
|
|
res.status(200).json({ ok: true, summary });
|
|
} catch (error) {
|
|
req.log?.error?.({ err: error }, 'notification dispatch failed');
|
|
res.status(500).json({
|
|
ok: false,
|
|
error: error?.message || String(error),
|
|
});
|
|
}
|
|
});
|
|
|
|
app.use((_req, res) => {
|
|
res.status(404).json({
|
|
code: 'NOT_FOUND',
|
|
message: 'Route not found',
|
|
});
|
|
});
|
|
|
|
return app;
|
|
}
|