-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver.ts
77 lines (65 loc) · 1.81 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
68
69
70
71
72
73
74
75
76
77
/**
* This file is here only to show you how to proceed if you would
* like to run your application as a standalone executable.
*
* You can launch it with the command `npm run standalone`
*/
import Fastify from 'fastify'
import fp from 'fastify-plugin'
// Import library to exit fastify process, gracefully (if possible)
import closeWithGrace from 'close-with-grace'
// Import your application as a normal plugin.
import serviceApp from './app.js'
/**
* Do not use NODE_ENV to determine what logger (or any env related feature) to use
* @see {@link https://www.youtube.com/watch?v=HMM7GJC5E2o}
*/
function getLoggerOptions () {
// Only if the program is running in an interactive terminal
if (process.stdout.isTTY) {
return {
level: 'info',
transport: {
target: 'pino-pretty',
options: {
translateTime: 'HH:MM:ss Z',
ignore: 'pid,hostname'
}
}
}
}
return { level: process.env.LOG_LEVEL ?? 'silent' }
}
const app = Fastify({
logger: getLoggerOptions(),
ajv: {
customOptions: {
coerceTypes: 'array', // change type of data to match type keyword
removeAdditional: 'all' // Remove additional body properties
}
}
})
async function init () {
// Register your application as a normal plugin.
// fp must be used to override default error handler
app.register(fp(serviceApp))
// Delay is the number of milliseconds for the graceful close to finish
closeWithGrace(
{ delay: process.env.FASTIFY_CLOSE_GRACE_DELAY ?? 500 },
async ({ err }) => {
if (err != null) {
app.log.error(err)
}
await app.close()
}
)
await app.ready()
try {
// Start listening.
await app.listen({ port: process.env.PORT ?? 3000 })
} catch (err) {
app.log.error(err)
process.exit(1)
}
}
init()