server: Remove node-static and use Flaska instead

This commit is contained in:
Jonatan Nilsson 2022-03-29 17:15:30 +00:00
parent 6a2cfcfb26
commit 24bcea69a2
2 changed files with 61 additions and 62 deletions

View file

@ -1,75 +1,75 @@
import path from 'path'
import fs from 'fs/promises'
import socket from 'socket.io-serveronly' import socket from 'socket.io-serveronly'
import nStatic from 'node-static' import { Flaska, FileResponse } from 'flaska'
import coremonitor from './core/coremonitor.mjs' import coremonitor from './core/coremonitor.mjs'
import onConnection from './routerio.mjs' import onConnection from './routerio.mjs'
export function run(http, port, ctx) { export function run(http, port, orgCtx) {
let localUtil = new ctx.sc.Util(import.meta.url) let localUtil = new orgCtx.sc.Util(import.meta.url)
const staticRoot = localUtil.getPathFromRoot('../public') const staticRoot = localUtil.getPathFromRoot('../public')
const fileServer = new nStatic.Server(staticRoot) const flaska = new Flaska({
const server = http.createServer(function (req, res) { log: orgCtx.log,
const child = ctx.log.child({}) }, http)
const d1 = new Date().getTime() flaska.before(function(ctx) {
ctx.state.started = 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 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) {
ctx.log.error(err, req.url);
}
res.writeHead(err.status, err.headers);
res.end(err.message);
}
});
}).resume()
}) })
const io = new socket(server) flaska.onreqerror(function(err, ctx) {
io.on('connection', onConnection.bind(this, io, ctx)) if (ctx.aborted) {
flaska.log.info('Request aborted')
} else {
flaska.log.error(err)
}
})
coremonitor(io, ctx) flaska.after(function(ctx) {
let ended = new Date().getTime()
var requestTime = ended - ctx.state.started
return server.listenAsync(port) let status = ''
.then(function() { let level = 'debug'
ctx.log.info('Server is listening on port ' + port) if (ctx.status >= 400) {
status = ctx.status + ' '
level = 'warn'
}
if (ctx.status >= 500) {
level = 'error'
}
ctx.log[level]({
duration: requestTime,
status: ctx.status,
}, `<-- ${status}${ctx.method} ${ctx.url}`)
})
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
}
return fs.stat(file).then(function(stat) {
if (!stat) {
ctx.status = 404
ctx.body = 'HTTP 404 Error'
return
}
ctx.body = new FileResponse(file, stat)
}) })
})
return flaska.listenAsync(port).then(function() {
orgCtx.log.info('Server is listening on port ' + port)
const io = new socket(flaska.server)
io.on('connection', onConnection.bind(this, io, orgCtx))
coremonitor(io, orgCtx)
})
} }

View file

@ -34,7 +34,6 @@
"license": "WTFPL", "license": "WTFPL",
"dependencies": { "dependencies": {
"flaska": "^1.2.1", "flaska": "^1.2.1",
"node-static": "^0.7.11",
"socket.io-serveronly": "^2.3.0" "socket.io-serveronly": "^2.3.0"
}, },
"devDependencies": { "devDependencies": {