hostr/app.js

76 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-08-22 16:16:15 +01:00
import path from 'path';
2018-06-02 15:50:39 +00:00
import Koa from 'koa';
2015-08-22 16:16:15 +01:00
import logger from 'koa-logger';
import serve from 'koa-static';
import favicon from 'koa-favicon';
import compress from 'koa-compress';
import bodyparser from 'koa-bodyparser';
2015-08-08 20:37:49 +01:00
import websockify from 'koa-websocket';
2015-08-22 18:24:39 +01:00
import helmet from 'koa-helmet';
2018-06-02 15:50:39 +00:00
import session from 'koa-session';
2015-08-22 16:16:15 +01:00
import raven from 'raven';
2018-06-02 18:07:00 +00:00
import debugname from 'debug';
2015-08-22 18:24:39 +01:00
import * as redis from './lib/redis';
2016-06-06 15:37:00 +01:00
import api, { ws } from './api/app';
2015-07-09 23:01:43 +01:00
import web from './web/app';
const debug = debugname('hostr');
2018-06-02 15:50:39 +00:00
const app = websockify(new Koa());
2015-08-30 18:35:05 +02:00
app.keys = [process.env.COOKIE_KEY];
2015-08-23 01:05:20 +01:00
2015-08-22 16:16:15 +01:00
if (process.env.SENTRY_DSN) {
const ravenClient = new raven.Client(process.env.SENTRY_DSN);
ravenClient.patchGlobal();
2018-06-02 15:50:39 +00:00
app.use(async (ctx, next) => {
2015-08-23 01:05:20 +01:00
this.raven = ravenClient;
2018-06-02 15:50:39 +00:00
await next();
2015-08-23 01:05:20 +01:00
});
2018-06-02 15:50:39 +00:00
app.ws.use(async (ctx, next) => {
2015-08-23 01:05:20 +01:00
this.raven = ravenClient;
2018-06-02 15:50:39 +00:00
await next();
2015-08-23 01:05:20 +01:00
});
2015-08-22 16:16:15 +01:00
}
2015-07-09 23:01:43 +01:00
2015-08-22 18:24:39 +01:00
app.use(helmet());
2018-06-02 15:50:39 +00:00
app.use(async (ctx, next) => {
ctx.set('Server', 'Nintendo 64');
if (ctx.req.headers['x-forwarded-proto'] === 'http') {
ctx.redirect(`https://${this.req.headers.host}${this.req.url}`);
2016-06-06 15:37:00 +01:00
return;
2015-08-22 16:16:15 +01:00
}
2015-08-23 01:05:20 +01:00
try {
2018-06-02 15:50:39 +00:00
await next();
2015-08-23 01:05:20 +01:00
} catch (err) {
2018-06-02 15:50:39 +00:00
if (!err.statusCode && ctx.raven) {
ctx.raven.captureError(err);
2015-08-23 01:05:20 +01:00
}
throw err;
}
2015-08-22 16:16:15 +01:00
});
2018-06-02 15:50:39 +00:00
app.use(session(app));
2015-08-22 18:24:39 +01:00
app.use(redis.middleware());
2015-08-22 16:16:15 +01:00
app.use(logger());
app.use(compress());
app.use(bodyparser());
app.use(favicon(path.join(__dirname, 'web/public/images/favicon.png')));
2016-06-06 15:37:00 +01:00
app.use(serve(path.join(__dirname, 'web/public/'), { maxage: 31536000000 }));
2015-08-22 16:16:15 +01:00
app.use(api.prefix('/api').routes());
app.use(web.prefix('').routes());
2015-07-09 23:01:43 +01:00
2015-08-23 01:05:20 +01:00
app.ws.use(redis.middleware());
app.ws.use(ws.prefix('/api').routes());
2015-07-09 23:01:43 +01:00
if (!module.parent) {
2016-06-06 15:37:00 +01:00
app.listen(process.env.PORT || 4040, () => {
debug('Koa HTTP server listening on port ', (process.env.PORT || 4040));
2015-08-08 20:37:49 +01:00
});
2015-07-09 23:01:43 +01:00
}
2015-08-11 21:54:55 +01:00
export default app;