forked from riccardoperra/codeimage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
46 lines (41 loc) · 1.02 KB
/
server.ts
File metadata and controls
46 lines (41 loc) · 1.02 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
// Read the .env file.
// Require library to exit fastify process, gracefully (if possible)
import * as closeWithGrace from 'close-with-grace';
import * as dotenv from 'dotenv';
// Require the framework
import Fastify from 'fastify';
dotenv.config();
// Instantiate Fastify with some config
const app = Fastify({
logger: true,
});
// Register your application as a normal plugin.
app.register(import('./app'));
// delay is the number of milliseconds for the graceful close to finish
const closeListeners = closeWithGrace({delay: 500}, async function ({
signal: _signal,
err,
manual: _manual,
}) {
if (err) {
app.log.error(err);
}
await app.close();
} as closeWithGrace.CloseWithGraceAsyncCallback);
app.addHook('onClose', async (instance, done) => {
closeListeners.uninstall();
done();
});
// Start listening.
app.listen(
{
host: process.env.HOST || '0.0.0.0',
port: parseInt(process.env.PORT as string) || 3000,
},
err => {
if (err) {
app.log.error(err);
process.exit(1);
}
},
);