-
Notifications
You must be signed in to change notification settings - Fork 167
/
start.js
executable file
·214 lines (175 loc) · 4.62 KB
/
start.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#! /usr/bin/env node
'use strict'
require('dotenv').config()
const isDocker = require('is-docker')
const closeWithGrace = require('close-with-grace')
const deepmerge = require('@fastify/deepmerge')({
cloneProtoObject (obj) { return obj }
})
const listenAddressDocker = '0.0.0.0'
const watch = require('./lib/watch')
const parseArgs = require('./args')
const {
exit,
requireModule,
requireESModule,
requireFastifyForModule,
requireServerPluginFromPath,
showHelpForCommand,
isKubernetes
} = require('./util')
const fp = require('fastify-plugin')
let Fastify = null
function loadModules (opts) {
try {
const { module: fastifyModule } = requireFastifyForModule(opts._[0])
Fastify = fastifyModule
} catch (e) {
module.exports.stop(e)
}
}
async function start (args) {
const opts = parseArgs(args)
if (opts.help) {
return showHelpForCommand('start')
}
if (opts._.length !== 1) {
console.error('Missing the required file parameter\n')
return showHelpForCommand('start')
}
loadModules(opts)
if (opts.watch) {
return watch(args, opts.ignoreWatch, opts.verboseWatch)
}
return runFastify(args)
}
function stop (message) {
exit(message)
}
function preloadCJSModules (opts) {
if (typeof opts.require === 'string') {
opts.require = [opts.require]
}
try {
opts.require.forEach(module => {
if (module) {
/* This check ensures we ignore `-r ""`, trailing `-r`, or
* other silly things the user might (inadvertently) be doing.
*/
requireModule(module)
}
})
} catch (e) {
module.exports.stop(e)
}
}
async function preloadESModules (opts) {
if (typeof opts.import === 'string') {
opts.import = [opts.import]
}
opts.import.forEach(async (m) => {
if (m) {
/* This check ensures we ignore `-i ""`, trailing `-i`, or
* other silly things the user might (inadvertently) be doing.
*/
try {
await requireESModule(m)
} catch (e) {
module.exports.stop(e)
}
}
})
}
async function runFastify (args, additionalOptions, serverOptions) {
const opts = parseArgs(args)
if (opts.require) {
preloadCJSModules(opts)
}
if (opts.import) {
await preloadESModules(opts)
}
opts.port = opts.port || process.env.PORT || 3000
loadModules(opts)
let file = null
try {
file = await requireServerPluginFromPath(opts._[0])
} catch (e) {
return module.exports.stop(e)
}
let logger
if (opts.loggingModule) {
try {
logger = requireModule(opts.loggingModule)
} catch (e) {
module.exports.stop(e)
}
}
const defaultLogger = {
level: opts.logLevel
}
let options = {
logger: logger || defaultLogger,
pluginTimeout: opts.pluginTimeout
}
if (opts.bodyLimit) {
options.bodyLimit = opts.bodyLimit
}
if (opts.prettyLogs) {
options.logger.transport = {
target: 'pino-pretty'
}
}
if (opts.debug) {
if (process.version.match(/v[0-6]\..*/g)) {
stop('Fastify debug mode not compatible with Node.js version < 6')
} else {
require('node:inspector').open(
opts.debugPort,
opts.debugHost || isDocker() || isKubernetes() ? listenAddressDocker : undefined
)
}
}
if (serverOptions) {
options = deepmerge(options, serverOptions)
}
if (opts.options && file.options) {
options = deepmerge(options, file.options)
}
const fastify = Fastify(options)
if (opts.prefix) {
opts.pluginOptions.prefix = opts.prefix
}
const appConfig = Object.assign({}, opts.options ? file.options : {}, opts.pluginOptions, additionalOptions)
const appFn = file.default || file
const appPlugin = appConfig.skipOverride ? fp(appFn) : appFn
await fastify.register(appPlugin, appConfig)
const closeListeners = closeWithGrace({ delay: opts.closeGraceDelay }, async function ({ signal, err, manual }) {
if (err) {
fastify.log.error(err)
}
await fastify.close()
})
await fastify.addHook('onClose', (instance, done) => {
closeListeners.uninstall()
done()
})
if (additionalOptions && additionalOptions.ready) {
await fastify.ready()
} else if (opts.address) {
await fastify.listen({ port: opts.port, host: opts.address })
} else if (opts.socket) {
await fastify.listen({ path: opts.socket })
} else if (isDocker() || isKubernetes()) {
await fastify.listen({ port: opts.port, host: listenAddressDocker })
} else {
await fastify.listen({ port: opts.port })
}
return fastify
}
function cli (args) {
start(args)
}
module.exports = { start, stop, runFastify, cli }
if (require.main === module) {
cli(process.argv.slice(2))
}