mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-06-17 02:29:10 +00:00
lerna: split up bulk of packages
This commit is contained in:
parent
cb0b1d2410
commit
47a2e5694e
90 changed files with 0 additions and 0 deletions
81
packages/roleypoly-server/index.js
Normal file
81
packages/roleypoly-server/index.js
Normal file
|
@ -0,0 +1,81 @@
|
|||
// @flow
|
||||
import 'dotenv/config'
|
||||
import logger from './logger'
|
||||
import http from 'http'
|
||||
import Koa from 'koa'
|
||||
import SocketIO from 'socket.io'
|
||||
import Roleypoly from './Roleypoly'
|
||||
import ksuid from 'ksuid'
|
||||
import bodyParser from 'koa-bodyparser'
|
||||
import compress from 'kompression'
|
||||
import session from 'koa-session'
|
||||
import invariant from 'invariant'
|
||||
import Keygrip from 'keygrip'
|
||||
|
||||
const log = logger(__filename)
|
||||
const app = new Koa()
|
||||
|
||||
// Create the server and socket.io server
|
||||
const server = http.createServer(app.callback())
|
||||
const io = SocketIO(server, { transports: ['websocket'], path: '/api/socket.io' })
|
||||
|
||||
const M = new Roleypoly(io, app) // eslint-disable-line no-unused-vars
|
||||
|
||||
const appKey = process.env.APP_KEY
|
||||
if (appKey == null) {
|
||||
throw invariant(false, '')
|
||||
}
|
||||
app.keys = new Keygrip([ appKey ])
|
||||
|
||||
const DEVEL = process.env.NODE_ENV === 'development'
|
||||
|
||||
async function start () {
|
||||
await M.awaitServices()
|
||||
|
||||
// body parser
|
||||
app.use(bodyParser())
|
||||
|
||||
// Compress
|
||||
app.use(compress())
|
||||
|
||||
// Request logger
|
||||
app.use(async (ctx, next) => {
|
||||
let timeStart = new Date()
|
||||
try {
|
||||
await next()
|
||||
} catch (e) {
|
||||
log.error(e)
|
||||
ctx.status = ctx.status || 500
|
||||
if (DEVEL) {
|
||||
ctx.body = ctx.body || e.stack
|
||||
} else {
|
||||
ctx.body = {
|
||||
err: 'something terrible happened.'
|
||||
}
|
||||
}
|
||||
}
|
||||
let timeElapsed = new Date() - timeStart
|
||||
|
||||
log.request(`${ctx.status} ${ctx.method} ${ctx.url} - ${ctx.ip} - took ${timeElapsed}ms`)
|
||||
// return null
|
||||
})
|
||||
|
||||
app.use(session({
|
||||
key: 'roleypoly:sess',
|
||||
maxAge: 'session',
|
||||
siteOnly: true,
|
||||
store: M.ctx.sessions,
|
||||
genid: () => { return ksuid.randomSync().string }
|
||||
}, app))
|
||||
|
||||
await M.mountRoutes()
|
||||
|
||||
// SPA server
|
||||
|
||||
log.info(`starting HTTP server on ${process.env.APP_PORT || 6769}`)
|
||||
server.listen(process.env.APP_PORT || 6769)
|
||||
}
|
||||
|
||||
start().catch(e => {
|
||||
log.fatal('app failed to start', e)
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue