2022-03-29 17:15:30 +00:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs/promises'
|
2020-09-08 07:53:42 +00:00
|
|
|
import socket from 'socket.io-serveronly'
|
2022-03-29 17:15:30 +00:00
|
|
|
import { Flaska, FileResponse } from 'flaska'
|
2022-03-29 10:25:25 +00:00
|
|
|
import coremonitor from './core/coremonitor.mjs'
|
2020-09-08 07:53:42 +00:00
|
|
|
|
|
|
|
import onConnection from './routerio.mjs'
|
|
|
|
|
2022-03-29 17:15:30 +00:00
|
|
|
export function run(http, port, orgCtx) {
|
|
|
|
let localUtil = new orgCtx.sc.Util(import.meta.url)
|
2022-03-28 07:27:18 +00:00
|
|
|
const staticRoot = localUtil.getPathFromRoot('../public')
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 17:15:30 +00:00
|
|
|
const flaska = new Flaska({
|
|
|
|
log: orgCtx.log,
|
|
|
|
}, http)
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 17:15:30 +00:00
|
|
|
flaska.before(function(ctx) {
|
|
|
|
ctx.state.started = new Date().getTime()
|
|
|
|
})
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 17:15:30 +00:00
|
|
|
flaska.onreqerror(function(err, ctx) {
|
|
|
|
if (ctx.aborted) {
|
|
|
|
flaska.log.info('Request aborted')
|
|
|
|
} else {
|
|
|
|
flaska.log.error(err)
|
|
|
|
}
|
|
|
|
})
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 17:15:30 +00:00
|
|
|
flaska.after(function(ctx) {
|
|
|
|
let ended = new Date().getTime()
|
|
|
|
var requestTime = ended - ctx.state.started
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 17:15:30 +00:00
|
|
|
let status = ''
|
|
|
|
let level = 'debug'
|
|
|
|
if (ctx.status >= 400) {
|
|
|
|
status = ctx.status + ' '
|
|
|
|
level = 'warn'
|
|
|
|
}
|
|
|
|
if (ctx.status >= 500) {
|
|
|
|
level = 'error'
|
2022-03-28 07:27:18 +00:00
|
|
|
}
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 17:15:30 +00:00
|
|
|
ctx.log[level]({
|
|
|
|
duration: requestTime,
|
|
|
|
status: ctx.status,
|
|
|
|
}, `<-- ${status}${ctx.method} ${ctx.url}`)
|
|
|
|
})
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 17:15:30 +00:00
|
|
|
flaska.on404(function(ctx) {
|
|
|
|
let file = path.resolve(path.join(staticRoot, ctx.url === '/' ? 'index.html' : ctx.url))
|
|
|
|
if (!file.startsWith(staticRoot)) {
|
|
|
|
ctx.status = 404
|
|
|
|
ctx.body = 'HTTP 404 Error'
|
|
|
|
return
|
|
|
|
}
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 17:15:30 +00:00
|
|
|
return fs.stat(file).then(function(stat) {
|
|
|
|
if (!stat) {
|
|
|
|
ctx.status = 404
|
|
|
|
ctx.body = 'HTTP 404 Error'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.body = new FileResponse(file, stat)
|
|
|
|
})
|
2022-03-28 07:27:18 +00:00
|
|
|
})
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 17:15:30 +00:00
|
|
|
return flaska.listenAsync(port).then(function() {
|
|
|
|
orgCtx.log.info('Server is listening on port ' + port)
|
2020-09-08 07:53:42 +00:00
|
|
|
|
2022-03-29 17:15:30 +00:00
|
|
|
const io = new socket(flaska.server)
|
|
|
|
io.on('connection', onConnection.bind(this, io, orgCtx))
|
2022-03-28 07:27:18 +00:00
|
|
|
|
2022-03-29 17:15:30 +00:00
|
|
|
coremonitor(io, orgCtx)
|
|
|
|
})
|
2020-09-08 07:53:42 +00:00
|
|
|
}
|