hostr/app.js

75 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-08-22 16:16:15 +01:00
import path from 'path';
2015-07-09 23:01:43 +01: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';
2015-08-22 16:16:15 +01:00
import raven from 'raven';
import mongo from './lib/mongo';
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';
import debugname from 'debug';
const debug = debugname('hostr');
2015-08-23 01:05:20 +01:00
const app = websockify(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();
2015-08-23 22:12:32 +01:00
app.use(function* ravenMiddleware(next) {
2015-08-23 01:05:20 +01:00
this.raven = ravenClient;
yield next;
});
2015-08-23 22:12:32 +01:00
app.ws.use(function* ravenWsMiddleware(next) {
2015-08-23 01:05:20 +01:00
this.raven = ravenClient;
yield next;
});
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());
2015-08-23 22:12:32 +01:00
app.use(function* errorMiddleware(next) {
2015-08-22 16:16:15 +01:00
this.set('Server', 'Nintendo 64');
2015-08-23 22:12:32 +01:00
if (this.req.headers['x-forwarded-proto'] === 'http') {
2016-06-06 15:37:00 +01:00
this.redirect(`https://${this.req.headers.host}${this.req.url}`);
return;
2015-08-22 16:16:15 +01:00
}
2015-08-23 01:05:20 +01:00
try {
yield next;
} catch (err) {
2016-05-24 18:03:10 +01:00
if (!err.statusCode && this.raven) {
2015-08-23 01:05:20 +01:00
this.raven.captureError(err);
}
throw err;
}
2015-08-22 16:16:15 +01:00
});
app.use(mongo());
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;