103 lines
2.5 KiB
JavaScript
103 lines
2.5 KiB
JavaScript
// import Koa from 'koa'
|
|
// import serve from 'koa-better-serve'
|
|
// import socket from 'koa-socket'
|
|
import socket from 'socket.io-serveronly'
|
|
import http from 'http'
|
|
import nStatic from 'node-static'
|
|
import * as casparcg from './casparcg/client.mjs'
|
|
|
|
import lowdb from './db.mjs'
|
|
import config from './config.mjs'
|
|
import log from './log.mjs'
|
|
import onConnection from './routerio.mjs'
|
|
import { bunyanLogger, errorHandler } from './middlewares.mjs'
|
|
|
|
log.info('Server: Opening database db.json')
|
|
|
|
lowdb().then(function(db) {
|
|
const fileServer = new nStatic.Server('./public')
|
|
const server = http.createServer(function (req, res) {
|
|
const child = log.child({})
|
|
|
|
const d1 = new Date().getTime()
|
|
|
|
var done = function () {
|
|
var requestTime = new Date().getTime() - d1
|
|
|
|
let level = 'info'
|
|
if (res.status >= 400) {
|
|
level = 'warn'
|
|
}
|
|
if (res.status >= 500) {
|
|
level = 'error'
|
|
}
|
|
|
|
child[level]({
|
|
duration: requestTime,
|
|
status: res.statusCode,
|
|
}, `<-- ${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) {
|
|
logger.error(err);
|
|
|
|
res.writeHead(err.status, err.headers);
|
|
res.end(err.message);
|
|
}
|
|
});
|
|
}).resume()
|
|
})
|
|
|
|
const io = new socket(server)
|
|
io.on('connection', onConnection.bind(this, io, db))
|
|
|
|
casparcg.initialise(log, db, io)
|
|
|
|
server.listen(config.get('server:port'), '0.0.0.0', function(err) {
|
|
if (err) {
|
|
log.fatal(err)
|
|
return process.exit(2)
|
|
}
|
|
log.info(`Server is listening on ${config.get('server:port')}`)
|
|
})
|
|
/*
|
|
const app = new Koa()
|
|
const io = new socket()
|
|
|
|
io.attach(app)
|
|
|
|
io.on('connection', onConnection.bind(this, io, db))
|
|
|
|
casparcg.initialise(log, db, io)
|
|
|
|
app.use(bunyanLogger(log))
|
|
app.use(errorHandler())
|
|
app.use(async (ctx, next) => {
|
|
if (ctx.url === '/') {
|
|
return ctx.redirect('/index.html')
|
|
}
|
|
await next()
|
|
})
|
|
app.use(serve('./public', ''))
|
|
|
|
app.listen(config.get('server:port'), err => {
|
|
if (err) return log.fatal(err)
|
|
log.info(`Server is listening on ${config.get('server:port')}`)
|
|
})*/
|
|
}, function(e) {
|
|
log.fatal(e, 'Critical error loading database')
|
|
process.exit(1)
|
|
}).catch(function(e) {
|
|
log.fatal(e, 'Critical error starting server')
|
|
process.exit(1)
|
|
})
|