mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-06-16 02:19:08 +00:00
chore: remove semi
This commit is contained in:
parent
4b75eaa0ab
commit
8fcc0dcbf9
64 changed files with 1073 additions and 1073 deletions
136
Server/index.js
136
Server/index.js
|
@ -1,96 +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
|
||||
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));
|
||||
};
|
||||
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();
|
||||
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 {
|
||||
|
@ -98,7 +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) => {
|
||||
|
@ -123,29 +123,29 @@ 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.',
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
let timeElapsed = new Date() - timeStart;
|
||||
let timeElapsed = new Date() - timeStart
|
||||
|
||||
log.request(
|
||||
`${ctx.status} ${ctx.method} ${ctx.url} - ${ctx.ip} - took ${timeElapsed}ms`
|
||||
);
|
||||
)
|
||||
// return null
|
||||
});
|
||||
})
|
||||
|
||||
const session = require('koa-session');
|
||||
const session = require('koa-session')
|
||||
app.use(
|
||||
session(
|
||||
{
|
||||
|
@ -154,21 +154,21 @@ async function start() {
|
|||
siteOnly: true,
|
||||
store: M.ctx.sessions,
|
||||
genid: () => {
|
||||
return ksuid.randomSync().string;
|
||||
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)
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue