Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
59 lines (50 loc) · 1.7 KB
/
Copy pathapp.js
File metadata and controls
59 lines (50 loc) · 1.7 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
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict'
const path = require('node:path')
const AutoLoad = require('@fastify/autoload')
const closeWithGrace = require('close-with-grace')
module.exports = async function (fastify, opts) {
try {
// Register base schema
await require('./routes/auth/schemas/loader').authSchemasLoader(fastify)
await require('./routes/notes/schemas/loader').noteSchemasLoader(fastify)
require('./routes/users/schemas/loader').loadUserSchemas(fastify)
// Load Config
await fastify.register(require('./plugins/config'), opts)
// Load Plugins
await fastify.register(AutoLoad, {
dir: path.join(__dirname, 'plugins'),
ignorePattern: /.*.no-load\.js/,
indexPattern: /^no$/i,
options: Object.assign({}, opts),
})
// Load Routes
await fastify.register(AutoLoad, {
dir: path.join(__dirname, 'routes'),
indexPattern: /.*routes(\.js|\.cjs)$/i,
ignorePattern: /(?:^|\/)(?:schemas|utils)(?:\/|$).*\.js/,
autoHooksPattern: /.*hooks(\.js|\.cjs)$/i,
autoHooks: true,
cascadeHooks: true,
options: Object.assign({}, opts),
})
const closeListeners = closeWithGrace(
{ delay: process.env.FASTIFY_CLOSE_GRACE_DELAY || 500 },
async function ({ signal, err }) {
if (err) {
fastify.log.error({ err }, 'Server closing due to error')
} else {
fastify.log.info(`${signal} received, gracefully shutting down server...`)
}
await fastify.close()
},
)
fastify.addHook('onClose', (instance, done) => {
closeListeners.uninstall()
done()
})
} catch (err) {
fastify.log.error(err)
throw err
}
}
module.exports.options = require('./configs/server-options')