-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.ts
More file actions
191 lines (175 loc) · 6.77 KB
/
Copy pathapp.ts
File metadata and controls
191 lines (175 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import Fastify, { type FastifyInstance } from 'fastify'
import cors from '@fastify/cors'
import helmet from '@fastify/helmet'
import rateLimit from '@fastify/rate-limit'
import swagger from '@fastify/swagger'
import swaggerUi from '@fastify/swagger-ui'
import { config } from './config.js'
import { projectRoutes } from './routes/projects.js'
import { memberRoutes } from './routes/members.js'
import { assessmentRoutes } from './routes/assessments.js'
import { commentRoutes } from './routes/comments.js'
import { syncRoutes } from './routes/sync.js'
export const API_VERSION = '1.0.0'
export interface BuildAppOptions {
logger?: boolean
/** Expose Swagger UI + /api/openapi.json. Defaults to config.exposeApiDocs. */
exposeApiDocs?: boolean
/** Fastify trustProxy value (proxy CIDR / hop count). Defaults to config.trustProxy. */
trustProxy?: string | boolean | number
/**
* Per-instance rate-limit max (requests per IP per minute). The entry point
* passes the global limit divided by the worker count, since each worker has
* its own in-memory store. Defaults to config.rateLimit.max.
*/
rateLimitMax?: number
}
export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyInstance> {
const exposeApiDocs = options.exposeApiDocs ?? config.exposeApiDocs
const app = Fastify({
logger: options.logger ?? true,
bodyLimit: 25 * 1024 * 1024, // 25 MB — assessments with embedded images can be large
// Trust the proxy hop so req.ip is the real client (rate limiting); see config.ts.
trustProxy: options.trustProxy ?? config.trustProxy,
})
await app.register(helmet, {
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
styleSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", 'data:'],
connectSrc: ["'self'"],
frameAncestors: ["'none'"],
},
},
})
await app.register(cors, config.cors)
await app.register(rateLimit, { max: options.rateLimitMax ?? config.rateLimit.max, timeWindow: '1 minute' })
if (exposeApiDocs) await app.register(swagger, {
openapi: {
info: {
title: 'Invulhulpen API',
description: 'REST API voor het beheren van assessments en projecten waarin assessments gegroepeerd kunnen worden.',
version: API_VERSION,
contact: {
name: 'Invulhulpen — MinBZK',
url: config.publicUrl,
email: 'RIG@rijksoverheid.nl',
},
},
servers: [{ url: '/' }],
tags: [
{ name: 'assessments', description: 'Assessments beheren' },
{ name: 'projects', description: 'Projecten en leden beheren' },
{ name: 'sync', description: 'Collaboration sync signals voor polling clients' },
],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
responses: {
TooManyRequests: {
description: 'Rate limit overschreden (max 300 requests per minuut)',
content: {
'application/problem+json': {
schema: {
type: 'object',
properties: {
type: { type: 'string', example: 'https://httpproblems.com/http-status/429' },
title: { type: 'string', example: 'Te veel verzoeken' },
status: { type: 'integer', example: 429 },
detail: {
type: 'string',
example: 'Maximaal aantal verzoeken overschreden. Probeer het later opnieuw.',
},
instance: { type: 'string', example: '/api/v1/projects' },
},
},
},
},
headers: {
'Retry-After': {
description: 'Seconden tot de rate limit reset',
schema: { type: 'integer' },
},
'X-RateLimit-Limit': {
description: 'Maximum aantal requests per tijdvenster',
schema: { type: 'integer', example: 300 },
},
'X-RateLimit-Remaining': {
description: 'Resterend aantal requests in huidig tijdvenster',
schema: { type: 'integer' },
},
},
},
},
},
security: [{ bearerAuth: [] }],
},
})
if (exposeApiDocs) await app.register(swaggerUi, {
routePrefix: '/api/docs',
logo: { content: Buffer.from(''), type: 'image/svg+xml' },
theme: {
title: 'Invulhulpen API',
css: [
{
filename: 'custom.css',
content: '.topbar, .servers-title, .servers { display: none !important; }',
},
],
},
uiConfig: {
docExpansion: 'list',
deepLinking: true,
displayRequestDuration: true,
persistAuthorization: true,
tryItOutEnabled: true,
syntaxHighlight: { theme: 'monokai' },
},
})
app.addHook('onSend', async (_request, reply) => {
reply.header('API-Version', API_VERSION)
reply.header('Cache-Control', 'no-store')
})
app.setErrorHandler(async (error: { statusCode?: number; message?: string }, request, reply) => {
const status = error.statusCode ?? 500
if (status === 429) {
return reply.status(429).type('application/problem+json').send({
type: 'https://httpproblems.com/http-status/429',
title: 'Te veel verzoeken',
status: 429,
detail: 'Maximaal aantal verzoeken overschreden. Probeer het later opnieuw.',
instance: request.url,
})
}
app.log.error(error)
return reply.status(status).type('application/problem+json').send({
type: `https://httpproblems.com/http-status/${status}`,
title: status >= 500 ? 'Interne serverfout' : 'Verzoek mislukt',
status,
detail: status >= 500 ? 'Er is een onverwachte fout opgetreden.' : (error.message || 'Onbekende fout'),
instance: request.url,
})
})
await app.register(projectRoutes, { prefix: '/api/v1/projects' })
await app.register(memberRoutes, { prefix: '/api/v1/projects' })
await app.register(assessmentRoutes, { prefix: '/api/v1/assessments' })
await app.register(commentRoutes, { prefix: '/api/v1/assessments' })
await app.register(syncRoutes, { prefix: '/api/v1/assessments' })
app.get('/api/health', { schema: { hide: true } }, async () => ({ status: 'ok' }))
app.get('/.well-known/security.txt', { schema: { hide: true } }, async (_request, reply) => {
return reply.redirect('https://www.ncsc.nl/.well-known/security.txt', 301)
})
if (exposeApiDocs) {
app.get('/api/openapi.json', { schema: { hide: true } }, async (_request, reply) => {
return reply.send(app.swagger())
})
}
return app
}