-
Notifications
You must be signed in to change notification settings - Fork 161
/
start.js
executable file
·146 lines (113 loc) · 3.01 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
#! /usr/bin/env node
'use strict'
require('dotenv').config()
const assert = require('assert')
const updateNotifier = require('update-notifier')
const PinoColada = require('pino-colada')
const pump = require('pump')
const isDocker = require('is-docker')
const listenAddressDocker = '0.0.0.0'
const watch = require('./lib/watch')
const parseArgs = require('./args')
const { exit, requireFastifyForModule, requireServerPluginFromPath, showHelpForCommand } = require('./util')
let Fastify = null
let fastifyPackageJSON = null
function loadModules (opts) {
try {
const { module: fastifyModule, pkg: fastifyPkg } = requireFastifyForModule(opts._[0])
Fastify = fastifyModule
fastifyPackageJSON = fastifyPkg
} catch (e) {
module.exports.stop(e)
}
}
function start (args, cb) {
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)
const notifier = updateNotifier({
pkg: {
name: 'fastify',
version: fastifyPackageJSON.version
},
updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week
})
notifier.notify({
isGlobal: false,
defer: false
})
if (opts.watch) {
return watch(args, opts.ignoreWatch)
}
return runFastify(args, cb)
}
function stop (message) {
exit(message)
}
function runFastify (args, cb) {
const opts = parseArgs(args)
opts.port = opts.port || process.env.PORT || 3000
cb = cb || assert.ifError
loadModules(opts)
let file = null
try {
file = requireServerPluginFromPath(opts._[0])
} catch (e) {
return module.exports.stop(e)
}
const options = {
logger: {
level: opts.logLevel
},
pluginTimeout: opts.pluginTimeout
}
if (opts.bodyLimit) {
options.bodyLimit = opts.bodyLimit
}
if (opts.prettyLogs) {
const pinoColada = PinoColada()
options.logger.stream = pinoColada
pump(pinoColada, process.stdout, assert.ifError)
}
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)
}
}
const fastify = Fastify(opts.options ? Object.assign(options, file.options) : options)
const pluginOptions = {}
if (opts.prefix) {
pluginOptions.prefix = opts.prefix
}
fastify.register(file, pluginOptions)
if (opts.address) {
fastify.listen(opts.port, opts.address, wrap)
} else if (opts.socket) {
fastify.listen(opts.socket, wrap)
} else if (isDocker()) {
fastify.listen(opts.port, listenAddressDocker, wrap)
} else {
fastify.listen(opts.port, wrap)
}
function wrap (err) {
cb(err, fastify)
}
return fastify
}
function cli (args) {
start(args)
}
module.exports = { start, stop, runFastify, cli }
if (require.main === module) {
cli(process.argv.slice(2))
}