/** * The Express app (docs/ACCOUNTS-1.0-PLAN.md §1). Everything is under /api, so the site and its API * share one address (as Events 2.2): nginx sends /api to this backend and the rest to Next.js. * * /api/health version and database, for monitoring and the Hub * /api/oidc/… the OpenID Connect provider (discovery, auth, token, …) * /api/self/… the account page's own sign-in * /api/church/v1, /api/links/v1/… for the church's other products * /api/accounts/v1/requests what a linked product asks (import, people.*; contracts 1.8.0) * /api/v2/… the pages' API (sign-in steps, account, admin, setup) */ import express from 'express'; import type Provider from 'oidc-provider'; import type { Ctx } from './context.js'; import { sameOrigin, sendError } from './http.js'; import { adminRouter } from './admin/routes.js'; import { interactionRouter, selfRouter } from './interaction/routes.js'; import { meRouter } from './me/routes.js'; import { LOGOUT_JS } from './oidc/pages.js'; import { publicRouter } from './public/routes.js'; import { requestsRouter } from './links/requests.js'; import { setupRouter } from './setup/routes.js'; import { health } from './hub/client.js'; import { appVersion } from './lib/version.js'; export function createApp(ctx: Ctx, provider: Provider) { const app = express(); app.disable('x-powered-by'); if (ctx.config.trustProxySetting !== false) app.set('trust proxy', ctx.config.trustProxySetting); app.use((_req, res, next) => { res.setHeader('X-Content-Type-Options', 'nosniff'); res.setHeader('Referrer-Policy', 'same-origin'); res.setHeader('X-Frame-Options', 'DENY'); res.setHeader('X-Robots-Tag', 'noindex, nofollow'); next(); }); app.get('/api/health', async (_req, res) => { const h = await health(ctx); res.status(h.db ? 200 : 503).json({ status: h.db ? 'ok' : 'error', product: 'accounts', version: appVersion(), db: h.db }); }); app.get('/api/static/logout.js', (_req, res) => { res.type('application/javascript').set('Cache-Control', 'public, max-age=3600').send(LOGOUT_JS); }); // The provider parses its own bodies: mounted before express.json. app.use('/api/oidc', provider.callback()); app.use('/api/self', selfRouter(ctx)); app.use('/api', requestsRouter(ctx, provider)); app.use('/api', publicRouter(ctx)); const v2 = express.Router(); v2.use(express.json({ limit: '64kb' })); v2.use(sameOrigin(ctx)); v2.use(interactionRouter(ctx, provider)); v2.use(setupRouter(ctx)); v2.use(meRouter(ctx, provider)); v2.use('/admin', adminRouter(ctx, provider)); v2.use((_req, res) => res.status(404).json({ error: { code: 'NOT_FOUND', message: 'There is no such API endpoint.' } })); app.use('/api/v2', v2); app.use((err: unknown, _req: express.Request, res: express.Response, next: express.NextFunction) => { if (res.headersSent) return next(err); const e = err as { type?: string }; if (e?.type === 'entity.parse.failed') return res.status(400).json({ error: { code: 'VALIDATION', message: 'The body must be JSON.' } }); return sendError(res, err); }); return app; }