filo_caspar/api/server.js

36 lines
858 B
JavaScript
Raw Normal View History

2017-12-03 11:34:43 +00:00
import Koa from 'koa'
import serve from 'koa-better-serve'
import socket from 'koa-socket'
2018-06-26 18:35:12 +00:00
import * as casparcg from './casparcg/client'
2017-12-03 11:34:43 +00:00
import config from '../config'
import log from '../log'
import onConnection from './routerio'
import { bunyanLogger, errorHandler } from './middlewares'
const app = new Koa()
const io = new socket()
io.attach(app)
io.on('connection', onConnection.bind(this, io))
2018-06-26 18:35:12 +00:00
casparcg.initialise(log, io).catch(e => {
log.error(e, 'Critical error initialising casparcg')
})
2017-12-03 11:34:43 +00:00
app.use(bunyanLogger(log))
app.use(errorHandler())
2018-06-26 18:35:12 +00:00
app.use(async (ctx, next) => {
if (ctx.url === '/') {
return ctx.redirect('/index.html')
}
await next()
})
app.use(serve('./public', ''))
2017-12-03 11:34:43 +00:00
app.listen(config.get('server:port'), err => {
if (err) return log.critical(err)
log.info(`Server is listening on ${config.get('server:port')}`)
})