-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathroosevelt.js
More file actions
executable file
·365 lines (303 loc) · 15.8 KB
/
roosevelt.js
File metadata and controls
executable file
·365 lines (303 loc) · 15.8 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
require('@colors/colors')
const express = require('express')
const express4 = require('express4')
const path = require('path')
const fs = require('fs-extra')
const appModulePath = require('app-module-path')
const Logger = require('roosevelt-logger')
const certsGenerator = require('./lib/scripts/certsGenerator.js')
const sessionSecretGenerator = require('./lib/scripts/sessionSecretGenerator.js')
const roosevelt = (options = {}, schema) => {
options.appDir = options.appDir || path.dirname(module.parent.filename) // appDir is either specified by the user or sourced from the parent require
const connections = {}
let httpServer
let httpsServer
let initialized = false
let checkConnectionsTimeout
let persistProcess
let logger
let appName
let appEnv
// source user-supplied params
const params = require('./lib/sourceParams')(options, schema)
const appDir = params.appDir
const pkg = params.pkg
const app = params.expressVersion !== 5 ? express4() : express() // express 5 is the default; if it's not set to 5, it is presumed the user wants express 4; only express 5 and 4 are supported
const router = express.Router() // initialize router
app.set('params', params) // expose app configuration
if (params.deprecationChecks === 'development-mode') require('./lib/deprecationChecker.js')(options, params)
// utility functions
async function initServer (args) {
if (args) throw new Error('Roosevelt\'s initServer method does not take arguments. You may have meant to pass parameters to Roosevelt\'s constructor instead.')
if (initialized) return
initialized = true
// configure logger with params
logger = new Logger(params.logging)
// expose express variables
app.set('express', express)
app.set('router', router)
app.set('env', params.mode === 'production-proxy' ? 'production' : params.mode)
app.set('logger', logger) // expose instance of roosevelt-logger module
app.set('appDir', appDir)
app.set('package', pkg) // expose contents of package.json if any
app.set('appName', pkg.name || 'Roosevelt Express')
app.set('appVersion', pkg.version)
app.set('routePrefix', params.routePrefix)
app.set('modelsPath', params.modelsPath)
app.set('viewsPath', params.viewsPath)
app.set('preprocessedViewsPath', params.preprocessedViewsPath)
app.set('preprocessedStaticsPath', params.preprocessedStaticsPath)
app.set('controllersPath', params.controllersPath)
app.set('staticsRoot', params.staticsRoot)
app.set('htmlPath', params.html.sourcePath)
app.set('htmlModels', params.html.models)
app.set('cssPath', params.css.sourcePath)
app.set('jsPath', params.js.sourcePath)
app.set('htmlRenderedOutput', params.html.output)
app.set('cssCompiledOutput', params.css.output)
app.set('buildFolder', params.buildFolder)
app.set('clientControllersBundledOutput', params.clientControllers.output)
app.set('clientViewsBundledOutput', params.clientViews.output)
app.set('publicFolder', params.unversionedPublic)
// make the app directory requirable
appModulePath.addPath(appDir)
// make the models directory requirable
appModulePath.addPath(path.join(params.modelsPath, '../'))
appModulePath.addPath(params.html.sourcePath) // make any js files in the staticsRoot html sourcePath requirable as well so you can have a models folder on static sites as well
// make the controllers directory requirable
appModulePath.addPath(path.join(params.controllersPath, '../'))
appName = app.get('appName')
appEnv = app.get('env')
// app starting message
if (params.makeBuildArtifacts === 'staticsOnly') logger.info('💭', `Building ${appName} static site in ${appEnv} mode...`.bold)
else logger.info('💭', `Starting ${appName} in ${appEnv} mode...`.bold)
// generate express session secret
if (params.expressSession && params.makeBuildArtifacts !== 'staticsOnly' && !fs.pathExistsSync(path.join(params.secretsPath, 'sessionSecret.json'))) sessionSecretGenerator(params.secretsPath)
// assign individual keys to connections when opened so they can be destroyed gracefully
function mapConnections (conn) {
const key = conn.remoteAddress + ':' + conn.remotePort
connections[key] = conn
conn.on('close', function () {
delete connections[key]
if (app.get('roosevelt:state') === 'disconnecting') Object.keys(connections).length === 0 && closeServer() // this will close the server if there are no connections
})
}
// setup http server
if (params.http.enable) {
httpServer = require('http').createServer(app)
httpServer.on('connection', mapConnections)
}
// setup https server
if (params.https.enable) {
const httpsOptions = params.https.options
// auto generate certs if in dev mode, autoCert is enabled, the cert configuration points at file paths, and those cert files don't exist already
if (app.get('env') === 'development' && params.https.autoCert && params.makeBuildArtifacts !== 'staticsOnly') {
if (await certParamIsPath(httpsOptions.cert) && await certParamIsPath(httpsOptions.key)) {
if (!fs.pathExistsSync(httpsOptions.key) && !fs.pathExistsSync(httpsOptions.cert)) {
certsGenerator(params.secretsPath, httpsOptions)
}
}
}
// add support for supplying file paths to some https options
if (httpsOptions.ca) httpsOptions.ca = await preprocessCertParams(httpsOptions.ca)
if (httpsOptions.cert) httpsOptions.cert = await preprocessCertParams(httpsOptions.cert)
if (httpsOptions.key) httpsOptions.key = await preprocessCertParams(httpsOptions.key)
if (httpsOptions.pfx) httpsOptions.pfx = await preprocessCertParams(httpsOptions.pfx)
// if a given cert param is a file path replace it with the contents of the cert file
// cert params natively support passing strings, buffers, and arrays of strings and/or buffers
async function preprocessCertParams (certParam) {
if (Array.isArray(certParam)) {
// this type of loop allows redefining the values in the array
for (let i = 0; i < certParam.length; i++) {
if (await certParamIsPath(certParam[i], true)) certParam[i] = await fs.readFile(path.join(params.secretsPath, certParam[i]))
}
} else {
if (await certParamIsPath(certParam, true)) certParam = await fs.readFile(path.join(params.secretsPath, certParam))
}
return certParam
}
async function certParamIsPath (certParam, checkIfExists) {
if (typeof certParam !== 'string') return false
if (checkIfExists) {
const certPath = path.join(params.secretsPath, certParam)
try {
const stats = await fs.lstat(certPath)
if (stats.isFile()) return true
else return false
} catch {
return false
}
} else {
if (certParam.endsWith('.pem')) return true
else return false
}
}
try {
httpsServer = require('https').createServer(httpsOptions, app)
} catch (error) {
if (error.code === 'ERR_OSSL_PEM_NO_START_LINE') {
logger.error('Cannot start HTTPS server because the HTTPS cert appears to be empty. This error happens most commonly when you forget to run `npm run generate-secrets` first before starting the server.')
process.exit()
} else logger.error(error)
}
httpsServer.on('connection', mapConnections)
}
// expose http server(s) to the user via express var
app.set('httpServer', httpServer)
app.set('httpsServer', httpsServer)
// fire user-defined onBeforeMiddleware event
if (params.onBeforeMiddleware && typeof params.onBeforeMiddleware === 'function') await Promise.resolve(params.onBeforeMiddleware(app))
// enable gzip compression
app.use(require('compression')())
// enable favicon support
if (params.favicon !== 'none' && params.favicon !== null) {
const faviconPath = path.join(params.staticsRoot, params.favicon)
if (fs.pathExistsSync(faviconPath)) app.use(require('serve-favicon')(faviconPath))
else logger.warn(`Favicon ${params.favicon} does not exist. Please ensure the "favicon" param is configured correctly.`)
}
require('./lib/setExpressConfigs')(app)
require('./lib/generateSymlinks')(app)
require('./lib/copyFiles')(app)
require('./lib/htmlMinifier')(app)
if (app.get('env') === 'development' && params.htmlValidator.enable) {
// instantiate the validator if it's installed
try {
require('express-html-validator')(app, params.htmlValidator)
} catch { }
}
require('./lib/injectReload')(app)
// fire user-defined onBeforeControllers event
if (params.onBeforeControllers && typeof params.onBeforeControllers === 'function') await Promise.resolve(params.onBeforeControllers(app))
await require('./lib/mapRoutes')(app)
// fire user-defined onBeforeStatics event
if (params.onBeforeStatics && typeof params.onBeforeStatics === 'function') await Promise.resolve(params.onBeforeStatics(app))
await require('./lib/preprocessViewsAndStatics')(app)
await require('./lib/preprocessStaticPages')(app)
await require('./lib/preprocessCss')(app)
await require('./lib/controllersBundler')(app)
await require('./lib/viewsBundler')(app)
await require('./lib/jsBundler')(app)
// fire user-defined onServerInit event
if (params.onServerInit && typeof params.onServerInit === 'function') await Promise.resolve(params.onServerInit(app))
}
async function startServer (args) {
if (args) throw new Error('Roosevelt\'s startServer method does not take arguments. You may have meant to pass parameters to Roosevelt\'s constructor instead.')
await initServer()
let listeningServers = 0
let numberOfServers = 0
if (params.http.enable) numberOfServers++
if (params.https.enable) numberOfServers++
await new Promise((resolve, reject) => {
function startupCallback (proto, port) {
return async function () {
logger.info('🎧', `${appName} ${proto} server listening on port ${port} (${appEnv} mode) ➡️ ${proto.toLowerCase()}://localhost:${port} (${proto.toLowerCase()}://${require('ip').address()}:${port})`.bold)
if (params.localhostOnly) logger.warn(`${appName} will only respond to requests coming from localhost. If you wish to override this behavior and have it respond to requests coming from outside of localhost, then set "localhostOnly" to false. See the Roosevelt documentation for more information: https://github.com/rooseveltframework/roosevelt`)
if (!params.hostPublic) logger.warn('Hosting of public folder is disabled. Your CSS, JS, images, and other files served via your public folder will not load unless you serve them via another web server. If you wish to override this behavior and have Roosevelt host your public folder even in production mode, then set "hostPublic" to true. See the Roosevelt documentation for more information: https://github.com/rooseveltframework/roosevelt')
listeningServers++
// fire user-defined onServerStart event if all servers are started
if (listeningServers === numberOfServers) {
if (params.onServerStart && typeof params.onServerStart === 'function') await Promise.resolve(params.onServerStart(app))
resolve() // resolve the promise when all servers are started
}
}
}
if (params.makeBuildArtifacts !== 'staticsOnly') {
if (!numberOfServers) {
logger.warn('You called startServer but both http and https are disabled, so no servers are starting.')
resolve() // no servers to start, resolve immediately
}
if (params.http.enable) {
const server = httpServer
.listen(params.http.port, params.localhostOnly ? 'localhost' : null, startupCallback('HTTP', params.http.port))
.on('error', err => {
logger.error(err)
logger.error(`Another process is using port ${params.http.port}. Either kill that process or change this app's port number.`.bold)
reject(err)
})
if (appEnv === 'development' && params.frontendReload.enable) require('express-browser-reload')(app.get('router'), server, params?.frontendReload?.expressBrowserReloadParams)
}
if (params.https.enable) {
const server = httpsServer
.listen(params.https.port, params.localhostOnly ? 'localhost' : null, startupCallback('HTTPS', params.https.port))
.on('error', err => {
logger.error(err)
logger.error(`Another process is using port ${params.https.port}. Either kill that process or change this app's port number.`.bold)
reject(err)
})
if (appEnv === 'development' && params.frontendReload.enable) require('express-browser-reload')(app.get('router'), server, params?.frontendReload?.expressBrowserReloadParams)
}
}
})
process.on('SIGTERM', shutdownGracefully)
process.on('SIGINT', shutdownGracefully)
}
// shut down all servers, connections, and threads that the roosevelt app is using
async function shutdownGracefully (args) {
persistProcess = args?.persistProcess
// return a promise to make shutdownGracefully awaitable
return new Promise((resolve, reject) => {
// fire user-defined onAppExit event
if (params.onAppExit && typeof params.onAppExit === 'function') params.onAppExit(app)
// force destroy connections if the server takes too long to shut down
checkConnectionsTimeout = setTimeout(() => {
logger.error(`${appName} could not close all connections in time; forcefully shutting down`)
for (const key in connections) connections[key].destroy()
if (persistProcess) {
if (httpServer) httpServer.close()
if (httpsServer) httpsServer.close()
} else {
process.exit()
}
resolve() // resolve the promise after forceful shutdown
}, params.shutdownTimeout)
app.set('roosevelt:state', 'disconnecting')
logger.info('\n💭 ', `${appName} received kill signal, attempting to shut down gracefully.`.magenta)
// if the app is in development mode, kill all connections instantly and exit
if (appEnv === 'development') {
for (const key in connections) connections[key].destroy()
closeServer(resolve) // pass resolve to closeServer to resolve the promise
} else {
// else do the normal procedure of seeing if there are still connections before closing
if (Object.keys(connections).length === 0) closeServer(resolve) // pass resolve to closeServer to resolve the promise
}
})
}
function closeServer (resolve) {
clearTimeout(checkConnectionsTimeout)
logger.info('✅', `${appName} successfully closed all connections and shut down gracefully.`.green)
let serversToClose = 0
let closedServers = 0
function onServerClosed () {
closedServers++
if (closedServers === serversToClose) {
app.set('roosevelt:state', null)
if (resolve) resolve() // resolve the promise when all servers are closed
}
}
if (persistProcess) {
if (httpServer) {
serversToClose++
httpServer.close(() => onServerClosed())
}
if (httpsServer) {
serversToClose++
httpsServer.close(() => onServerClosed())
}
} else process.exit()
// if no servers are active, resolve immediately
if (serversToClose === 0) {
app.set('roosevelt:state', null)
if (resolve) resolve()
}
}
return {
expressApp: app,
initServer,
init: initServer,
startServer,
start: startServer,
stopServer: shutdownGracefully,
stop: shutdownGracefully
}
}
module.exports = roosevelt