-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
54 lines (48 loc) · 1.9 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
50
51
52
53
54
import fastify from 'fastify';
import dotenv from 'dotenv';
import path from 'path';
import fastifyStatic from '@fastify/static';
import eventsPlugin from './plugins/events.js';
import prismaPlugin from './plugins/prisma.js';
import bcryptPlugin from './plugins/bcrypt.js';
import regionsPlugin from './plugins/regions.js';
import storagePlugin from './plugins/storage.js';
import coreService from './core/index.js';
const main = async function () {
dotenv.config();
const server = fastify({
logger: {
transport: process.env.NODE_ENV === 'dev' ? {
target: 'pino-pretty'
} : undefined,
level: process.env.NODE_ENV === 'production' ? 'warn' : 'debug'
},
});
// plugins
await server.register(eventsPlugin); // global
await server.register(bcryptPlugin); // global
await server.register(prismaPlugin); // global
await server.register(storagePlugin, { base: './public/uploads', urlPrefix: '/public/uploads' }); // global
await server.register(regionsPlugin, { web: true }); // global
await server.register(fastifyStatic, { root: path.join(process.cwd(), './public'), prefix: '/public', }); // static service
await server.register(coreService); // our service
server.ready(() => {
console.log(server.printPlugins());
console.log(server.printRoutes({ commonPrefix: false }));
});
// add shutdown hook
const shutdownHook = async () => {
await server.close();
server.log.info('server is closed.');
};
process.on('SIGINT', shutdownHook);
process.on('SIGQUIT', shutdownHook);
process.on('SIGTERM', shutdownHook);
server.listen({ port: process.env.SERVER_PORT || 5600, host: '0.0.0.0' })
.then((address) => console.log(`server listening on ${address}`))
.catch(err => {
server.log.error(err);
process.exit(1);
});
};
main();