-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkastan.js
More file actions
37 lines (33 loc) · 1.05 KB
/
kastan.js
File metadata and controls
37 lines (33 loc) · 1.05 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
'use strict';
const { loadRestApplication } = require('@leonid-shutov/uncommonjs');
const fastify = require('fastify')({ logger: true });
const cors = require('@fastify/cors');
class Server {
constructor(router) {
for (const [path, methods] of Object.entries(router)) {
for (const [method, handler] of Object.entries(methods)) {
fastify[method](`/api${path}`, async (request, reply) => {
const { params, query, body, headers } = request;
const { status, body: responseBody } = await handler({ path: params, query, body, headers });
return reply.status(status).send(responseBody);
});
}
}
}
async start(port = 3000) {
await fastify.register(cors, {
origin: (origin, cb) => {
cb(null, true);
return;
},
});
await fastify.listen({ port, host: '0.0.0.0' });
}
}
(async () => {
console.time('Loading');
const router = await loadRestApplication({ console, process });
console.timeEnd('Loading');
const server = new Server(router);
server.start();
})();