chore: prettier on server

This commit is contained in:
Katie Thornhill 2019-11-19 23:02:54 -05:00
parent 3b0d249e41
commit 912b40c383
No known key found for this signature in database
GPG key ID: F76EDC6541A99644
21 changed files with 589 additions and 501 deletions

View file

@ -1,93 +1,96 @@
require('dotenv').config({silent: true})
const log = new (require('./logger'))('index')
require('dotenv').config({ silent: true });
const log = new (require('./logger'))('index');
const http = require('http')
const Koa = require('koa')
const app = new Koa()
const _io = require('socket.io')
const fs = require('fs')
const path = require('path')
const router = require('koa-better-router')().loadMethods()
const Roleypoly = require('./Roleypoly')
const ksuid = require('ksuid')
const http = require('http');
const Koa = require('koa');
const app = new Koa();
const _io = require('socket.io');
const fs = require('fs');
const path = require('path');
const router = require('koa-better-router')().loadMethods();
const Roleypoly = require('./Roleypoly');
const ksuid = require('ksuid');
// monkey patch async-reduce because F U T U R E
Array.prototype.areduce = async function (predicate, acc = []) { // eslint-disable-line
Array.prototype.areduce = async function(predicate, acc = []) {
// eslint-disable-line
for (let i of this) {
acc = await predicate(acc, i)
acc = await predicate(acc, i);
}
return acc
}
return acc;
};
Array.prototype.filterNot = Array.prototype.filterNot || function (predicate) {
return this.filter(v => !predicate(v))
}
Array.prototype.filterNot =
Array.prototype.filterNot ||
function(predicate) {
return this.filter(v => !predicate(v));
};
// Create the server and socket.io server
const server = http.createServer(app.callback())
const io = _io(server, { transports: ['websocket'], path: '/api/socket.io' })
const server = http.createServer(app.callback());
const io = _io(server, { transports: ['websocket'], path: '/api/socket.io' });
const M = new Roleypoly(router, io, app) // eslint-disable-line no-unused-vars
const M = new Roleypoly(router, io, app); // eslint-disable-line no-unused-vars
app.keys = [ process.env.APP_KEY ]
app.keys = [process.env.APP_KEY];
const DEVEL = process.env.NODE_ENV === 'development'
const DEVEL = process.env.NODE_ENV === 'development';
async function start () {
await M.awaitServices()
async function start() {
await M.awaitServices();
// body parser
const bodyParser = require('koa-bodyparser')
app.use(bodyParser({ types: ['json'] }))
const bodyParser = require('koa-bodyparser');
app.use(bodyParser({ types: ['json'] }));
// Compress
const compress = require('koa-compress')
app.use(compress())
const compress = require('koa-compress');
app.use(compress());
// SPA + Static
if (process.env.NODE_ENV === 'production') {
const pub = path.resolve(path.join(__dirname, 'public'))
log.info('public path', pub)
const pub = path.resolve(path.join(__dirname, 'public'));
log.info('public path', pub);
const staticFiles = require('koa-static')
const staticFiles = require('koa-static');
// app.use(staticFiles(pub, { defer: true, gzip: true, br: true }))
const send = require('koa-send')
const send = require('koa-send');
app.use(async (ctx, next) => {
if (ctx.path.startsWith('/api')) {
log.info('api breakout')
return next()
log.info('api breakout');
return next();
}
const chkPath = path.resolve(path.join(pub, ctx.path))
log.info('chkPath', chkPath)
const chkPath = path.resolve(path.join(pub, ctx.path));
log.info('chkPath', chkPath);
if (!chkPath.startsWith(pub)) {
return next()
return next();
}
try {
fs.statSync(chkPath)
log.info('sync pass')
ctx.body = fs.readFileSync(chkPath)
ctx.type = path.extname(ctx.path)
log.info('body sent')
ctx.status = 200
return next()
fs.statSync(chkPath);
log.info('sync pass');
ctx.body = fs.readFileSync(chkPath);
ctx.type = path.extname(ctx.path);
log.info('body sent');
ctx.status = 200;
return next();
} catch (e) {
log.warn('failed')
log.warn('failed');
if (ctx.path.startsWith('/static/')) {
return next()
return next();
}
try {
ctx.body = fs.readFileSync(path.join(pub, 'index.html'), { encoding: 'utf-8' })
ctx.body = fs.readFileSync(path.join(pub, 'index.html'), { encoding: 'utf-8' });
} catch (e) {
ctx.body = e.stack || e.trace
ctx.status = 500
ctx.body = e.stack || e.trace;
ctx.status = 500;
}
log.info('index sent')
ctx.status = 200
return next()
log.info('index sent');
ctx.status = 200;
return next();
}
// try {
@ -95,8 +98,7 @@ async function start () {
// } catch (e) {
// send(ctx, 'index.html', { root: pub })
// }
})
});
// const sendOpts = {root: pub, index: 'index.html'}
// // const sendOpts = {}
// app.use(async (ctx, next) => {
@ -121,43 +123,52 @@ async function start () {
// Request logger
app.use(async (ctx, next) => {
let timeStart = new Date()
let timeStart = new Date();
try {
await next()
await next();
} catch (e) {
log.error(e)
ctx.status = ctx.status || 500
log.error(e);
ctx.status = ctx.status || 500;
if (DEVEL) {
ctx.body = ctx.body || e.stack
ctx.body = ctx.body || e.stack;
} else {
ctx.body = {
err: 'something terrible happened.'
}
err: 'something terrible happened.',
};
}
}
let timeElapsed = new Date() - timeStart
let timeElapsed = new Date() - timeStart;
log.request(`${ctx.status} ${ctx.method} ${ctx.url} - ${ctx.ip} - took ${timeElapsed}ms`)
log.request(
`${ctx.status} ${ctx.method} ${ctx.url} - ${ctx.ip} - took ${timeElapsed}ms`
);
// return null
})
});
const session = require('koa-session')
app.use(session({
key: 'roleypoly:sess',
maxAge: 'session',
siteOnly: true,
store: M.ctx.sessions,
genid: () => { return ksuid.randomSync().string }
}, app))
const session = require('koa-session');
app.use(
session(
{
key: 'roleypoly:sess',
maxAge: 'session',
siteOnly: true,
store: M.ctx.sessions,
genid: () => {
return ksuid.randomSync().string;
},
},
app
)
);
await M.mountRoutes()
await M.mountRoutes();
// SPA server
log.info(`starting HTTP server on ${process.env.APP_PORT || 6769}`)
server.listen(process.env.APP_PORT || 6769)
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)
})
log.fatal('app failed to start', e);
});