forked from fastify/fastify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
executable file
·179 lines (145 loc) · 3.83 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
#! /usr/bin/env node
'use strict'
require('dotenv').config()
const isDocker = require('is-docker')
const closeWithGrace = require('close-with-grace')
const listenAddressDocker = '0.0.0.0'
const watch = require('./lib/watch')
const parseArgs = require('./args')
const {
exit,
requireModule,
requireFastifyForModule,
requireServerPluginFromPath,
showHelpForCommand
} = require('./util')
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')
}
// we start crashing on unhandledRejection
require('make-promises-safe')
loadModules(opts)
if (opts.watch) {
return watch(args, opts.ignoreWatch, opts.verboseWatch)
}
return runFastify(args)
}
function stop (message) {
exit(message)
}
async function runFastify (args, additionalOptions, serverOptions) {
const opts = parseArgs(args)
if (opts.require) {
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)
}
}
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
}
const 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('inspector').open(
opts.debugPort,
opts.debugHost || isDocker() ? listenAddressDocker : undefined
)
}
}
if (serverOptions) {
Object.assign(options, serverOptions)
}
const fastify = Fastify(
opts.options ? Object.assign(options, file.options) : options
)
if (opts.prefix) {
opts.pluginOptions.prefix = opts.prefix
}
const appConfig = Object.assign({}, opts.pluginOptions, additionalOptions)
await fastify.register(file.default || file, 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()) {
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))
}