-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
67 lines (60 loc) · 1.61 KB
/
server.ts
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
import type { ErrorObject } from 'ajv';
import Fastify from 'fastify';
import { Logger, LogLevel } from 'types/Logger';
const fastify = Fastify({
logger: {
level: 'warn',
},
});
async function main() {
Logger.info('Config', `Loading server configs...`);
await fastify.register(import('fastify-graceful-shutdown'));
fastify.gracefulShutdown((signal, next) => {
console.log(signal);
next();
});
await fastify.register(import('@fastify/sensible'));
await fastify.register(import('@fastify/cors'));
await fastify.register(import('app/config'));
fastify.register(import('app/src/app'), fastify.config);
fastify.listen(
{
host: fastify.config.server.host,
port: fastify.config.server.port,
},
(err: any) => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
},
);
// noinspection HttpUrlsUsage
Logger.info(
'Config',
`Server is listening on ` +
LogLevel.info(
`http://${fastify.config.server.host}:${fastify.config.server.port}`,
),
);
}
main().catch((errors) => {
if ((errors as Array<ErrorObject>)[0].instancePath) {
errors.forEach((error: ErrorObject) => {
Logger.error(
'Config',
'Invalid config item',
LogLevel.warning(`${error.message}`) +
(error.instancePath
? ' at ' + LogLevel.link(error.instancePath)
: ''),
);
});
} else {
console.log(errors.join('\n'));
}
fastify.close().then(
() => Logger.success('Server', 'Successfully closed'),
(err) => Logger.error('Server', 'Cannot close', err.message),
);
});