Fix so server doesn't close process on exit
This commit is contained in:
parent
586da219d1
commit
f2f7d9ef9d
1 changed files with 63 additions and 62 deletions
125
api/server.mjs
125
api/server.mjs
|
@ -7,75 +7,76 @@ import coremonitor from './core/coremonitor.mjs'
|
|||
import onConnection from './routerio.mjs'
|
||||
|
||||
export function run(config, db, log, core, http, port) {
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||
const staticRoot = path.join(__dirname,'../public')
|
||||
return new Promise(function(resolve, reject) {
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||
const staticRoot = path.join(__dirname,'../public')
|
||||
|
||||
const fileServer = new nStatic.Server(staticRoot)
|
||||
const server = http.createServer(function (req, res) {
|
||||
const child = log.child({})
|
||||
const fileServer = new nStatic.Server(staticRoot)
|
||||
const server = http.createServer(function (req, res) {
|
||||
const child = log.child({})
|
||||
|
||||
const d1 = new Date().getTime()
|
||||
const d1 = new Date().getTime()
|
||||
|
||||
let finishedRequest = false
|
||||
var done = function () {
|
||||
if (finishedRequest) return
|
||||
finishedRequest = true
|
||||
if (req.url === '/main.css.map') return
|
||||
var requestTime = new Date().getTime() - d1
|
||||
let finishedRequest = false
|
||||
var done = function () {
|
||||
if (finishedRequest) return
|
||||
finishedRequest = true
|
||||
if (req.url === '/main.css.map') return
|
||||
var requestTime = new Date().getTime() - d1
|
||||
|
||||
let level = 'debug'
|
||||
if (res.statusCode >= 400) {
|
||||
level = 'warn'
|
||||
}
|
||||
if (res.statusCode >= 500) {
|
||||
level = 'error'
|
||||
}
|
||||
|
||||
let status = ''
|
||||
if (res.statusCode >= 400) {
|
||||
status = res.statusCode + ' '
|
||||
}
|
||||
|
||||
child[level]({
|
||||
duration: requestTime,
|
||||
status: res.statusCode,
|
||||
}, `<-- ${status}${req.method} ${req.url}`)
|
||||
}
|
||||
|
||||
res.addListener('finish', done);
|
||||
res.addListener('close', done);
|
||||
|
||||
req.addListener('end', function () {
|
||||
if (req.url === '/') {
|
||||
res.writeHead(302, { Location: '/index.html' })
|
||||
return res.end()
|
||||
}
|
||||
|
||||
fileServer.serve(req, res, function (err) {
|
||||
if (err) {
|
||||
if (err.status !== 404) {
|
||||
log.error(err, req.url);
|
||||
}
|
||||
|
||||
res.writeHead(err.status, err.headers);
|
||||
res.end(err.message);
|
||||
let level = 'debug'
|
||||
if (res.statusCode >= 400) {
|
||||
level = 'warn'
|
||||
}
|
||||
if (res.statusCode >= 500) {
|
||||
level = 'error'
|
||||
}
|
||||
});
|
||||
}).resume()
|
||||
})
|
||||
|
||||
const io = new socket(server)
|
||||
io.on('connection', onConnection.bind(this, io, config, db, log, core))
|
||||
let status = ''
|
||||
if (res.statusCode >= 400) {
|
||||
status = res.statusCode + ' '
|
||||
}
|
||||
|
||||
coremonitor(io, config, db, log, core)
|
||||
child[level]({
|
||||
duration: requestTime,
|
||||
status: res.statusCode,
|
||||
}, `<-- ${status}${req.method} ${req.url}`)
|
||||
}
|
||||
|
||||
server.listen(port, '0.0.0.0', function(err) {
|
||||
if (err) {
|
||||
log.fatal(err)
|
||||
log.event.error('Error starting server: ' + err.message)
|
||||
return process.exit(2)
|
||||
}
|
||||
log.event.info(`Server is listening on ${port} serving files on ${staticRoot}`)
|
||||
log.info(`Server is listening on ${port} serving files on ${staticRoot}`)
|
||||
res.addListener('finish', done);
|
||||
res.addListener('close', done);
|
||||
|
||||
req.addListener('end', function () {
|
||||
if (req.url === '/') {
|
||||
res.writeHead(302, { Location: '/index.html' })
|
||||
return res.end()
|
||||
}
|
||||
|
||||
fileServer.serve(req, res, function (err) {
|
||||
if (err) {
|
||||
if (err.status !== 404) {
|
||||
log.error(err, req.url);
|
||||
}
|
||||
|
||||
res.writeHead(err.status, err.headers);
|
||||
res.end(err.message);
|
||||
}
|
||||
});
|
||||
}).resume()
|
||||
})
|
||||
|
||||
const io = new socket(server)
|
||||
io.on('connection', onConnection.bind(this, io, config, db, log, core))
|
||||
|
||||
coremonitor(io, config, db, log, core)
|
||||
|
||||
server.listen(port, '0.0.0.0', function(err) {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
log.event.info(`Server is listening on ${port} serving files on ${staticRoot}`)
|
||||
log.info(`Server is listening on ${port} serving files on ${staticRoot}`)
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue