| 10 | import { swaggerConfig } from "../swagger"; |
| 11 | |
| 12 | export const createApp = () => { |
| 13 | const app = new Elysia({ |
| 14 | cookie: { |
| 15 | secrets: env.JWT_SECRET, |
| 16 | sign: [AUTH_COOKIE_NAME], |
| 17 | }, |
| 18 | }); |
| 19 | |
| 20 | if (env.isDevelopment) { |
| 21 | app.use(swaggerConfig); |
| 22 | } |
| 23 | |
| 24 | /* |
| 25 | * Security headers (CSP, HSTS, X-Frame-Options, etc.) are set by Traefik in |
| 26 | * front of this service, not here. The api container is meant to run behind |
| 27 | * a Traefik instance — running it standalone leaves it without those headers. |
| 28 | * See infra/compose/compose/docker-compose.production-labels.yml. |
| 29 | */ |
| 30 | /* |
| 31 | * App-level catch-all error handler. Route-level `.onError` covers |
| 32 | * thrown errors INSIDE a matched route, but Elysia's NOT_FOUND for |
| 33 | * an unmatched path (or method mismatch) never reaches a route — so |
| 34 | * without this handler the client got a plain-text Elysia default |
| 35 | * response instead of the canonical JSON envelope. With it, every |
| 36 | * unknown route also returns `{ success: false, error: { code, |
| 37 | * message, timestamp } }`, identical to handler errors. |
| 38 | */ |
| 39 | app.onError(({ code, error, set }) => |
| 40 | errorHandler({ code: String(code), error, set }) |
| 41 | ); |
| 42 | |
| 43 | const cors = buildCors(); |
| 44 | |
| 45 | let configured = app.use(bodyLimit).use(requestLogger).use(metricsObserver); |
| 46 | |
| 47 | if (cors !== undefined) { |
| 48 | configured = configured.use(cors); |
| 49 | } |
| 50 | |
| 51 | return ( |
| 52 | configured |
| 53 | .use(buildRateLimit()) |
| 54 | /* |
| 55 | * Health probes + Prometheus metrics mounted at root (no /api/v1 |
| 56 | * prefix) so orchestrators and the scrape pipeline hit the |
| 57 | * conventional URLs. |
| 58 | */ |
| 59 | .use(routes.health) |
| 60 | .use(routes.metrics) |
| 61 | .group("/api/v1/capabilities", (group) => group.use(routes.capabilities)) |
| 62 | .group("/api/v1/auth", (group) => group.use(routes.auth)) |
| 63 | .group("/api/v1/users", (group) => group.use(routes.users)) |
| 64 | .group("/api/v1/billing", (group) => group.use(routes.billing)) |
| 65 | .group("/api/v1/admin", (group) => group.use(routes.admin)) |
| 66 | .group("/api/v1/dashboard", (group) => group.use(routes.dashboard)) |
| 67 | .group("/api/v1/notifications", (group) => |
| 68 | group.use(routes.notifications) |
| 69 | ) |