2020-12-08 11:09:46 +00:00
|
|
|
import path from 'path'
|
|
|
|
import { fileURLToPath } from 'url'
|
2020-04-07 10:36:11 +00:00
|
|
|
import socket from 'socket.io-serveronly'
|
2020-12-08 11:09:46 +00:00
|
|
|
import nStatic from 'node-static-lib'
|
2020-04-06 22:47:58 +00:00
|
|
|
|
2020-12-08 11:09:46 +00:00
|
|
|
import * as casparcg from './casparcg/client.mjs'
|
2020-04-06 22:47:58 +00:00
|
|
|
import onConnection from './routerio.mjs'
|
|
|
|
|
2020-12-08 11:09:46 +00:00
|
|
|
export function run(config, db, log, core, http, orgPort) {
|
|
|
|
log.info('Server: Opening database db.json')
|
|
|
|
|
|
|
|
db.defaults({
|
|
|
|
graphics: [],
|
|
|
|
presets: [],
|
|
|
|
playing: [],
|
|
|
|
schedule: [],
|
|
|
|
settings: {
|
|
|
|
casparplayhost: 'localhost:3000',
|
|
|
|
casparhost: 'localhost',
|
|
|
|
},
|
|
|
|
version: 1,
|
|
|
|
trash: [],
|
|
|
|
})
|
|
|
|
.write()
|
|
|
|
.then(
|
|
|
|
function() { },
|
|
|
|
function(e) { log.error(e, 'Error writing defaults to lowdb') }
|
|
|
|
)
|
|
|
|
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
|
|
const staticRoot = path.join(__dirname,'../public')
|
|
|
|
const fileServer = new nStatic.Server(staticRoot)
|
2020-04-06 22:47:58 +00:00
|
|
|
|
2020-04-07 10:36:11 +00:00
|
|
|
const server = http.createServer(function (req, res) {
|
|
|
|
const child = log.child({})
|
|
|
|
|
|
|
|
const d1 = new Date().getTime()
|
|
|
|
|
2020-12-08 11:09:46 +00:00
|
|
|
let isFinished = false
|
|
|
|
|
2020-04-07 10:36:11 +00:00
|
|
|
var done = function () {
|
2020-12-08 11:09:46 +00:00
|
|
|
if (isFinished) return
|
|
|
|
isFinished = true
|
2020-04-07 10:36:11 +00:00
|
|
|
var requestTime = new Date().getTime() - d1
|
|
|
|
|
2020-12-08 11:09:46 +00:00
|
|
|
let level = 'debug'
|
|
|
|
if (res.statusCode >= 400) {
|
2020-04-07 10:36:11 +00:00
|
|
|
level = 'warn'
|
|
|
|
}
|
2020-12-08 11:09:46 +00:00
|
|
|
if (res.statusCode >= 500) {
|
2020-04-07 10:36:11 +00:00
|
|
|
level = 'error'
|
|
|
|
}
|
|
|
|
|
|
|
|
child[level]({
|
2020-04-07 10:57:43 +00:00
|
|
|
duration: requestTime,
|
2020-04-07 10:36:11 +00:00
|
|
|
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) {
|
2020-12-08 11:09:46 +00:00
|
|
|
if (err.status !== 404) {
|
|
|
|
log.error(err, req.url);
|
|
|
|
}
|
2020-04-07 10:36:11 +00:00
|
|
|
|
|
|
|
res.writeHead(err.status, err.headers);
|
|
|
|
res.end(err.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).resume()
|
|
|
|
})
|
|
|
|
|
|
|
|
const io = new socket(server)
|
2020-12-08 11:09:46 +00:00
|
|
|
io.on('connection', onConnection.bind(this, io, db, log))
|
2020-04-07 10:36:11 +00:00
|
|
|
|
|
|
|
casparcg.initialise(log, db, io)
|
|
|
|
|
2020-12-08 11:09:46 +00:00
|
|
|
let port = orgPort || 3000
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
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()
|
|
|
|
})
|
2020-04-07 10:36:11 +00:00
|
|
|
})
|
2020-12-08 11:09:46 +00:00
|
|
|
}
|