-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (41 loc) · 1.25 KB
/
server.js
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
import 'dotenv/config';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import Fastify from 'fastify';
import FastifyCors from '@fastify/cors';
import FastifyStatic from '@fastify/static';
import FastifyAutoload from '@fastify/autoload';
import FastifyRateLimit from '@fastify/rate-limit';
import FastifySSEPlugin from 'fastify-sse-v2';
import { db } from './lib/db.js';
import { createLogger } from './lib/logger.js';
import { RATE_LIMIT_ENABLED, PORT } from './config.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const fastify = Fastify({ logger: createLogger, pluginTimeout: 0 });
if (RATE_LIMIT_ENABLED) {
fastify.register(FastifyRateLimit, {
global: false,
redis: db,
});
}
fastify.register(FastifyStatic, {
root: join(__dirname, 'public'),
});
fastify.register(FastifySSEPlugin);
fastify.register(FastifyCors);
fastify.register(FastifyAutoload, {
dir: join(__dirname, 'routes'),
options: { prefix: '/api' },
});
/**
* Start Server
*/
async function start() {
const address = await fastify.listen({ port: PORT, host: '0.0.0.0' });
fastify.log.info(`Server listening on ${address}`);
}
start().catch((err) => {
fastify.log.error(err);
process.exit(1);
});