hostr/app.js

79 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';
import mount from 'koa-mount';
2015-08-08 20:37:49 +01:00
import route from 'koa-route';
2015-08-22 16:16:15 +01:00
import logger from 'koa-logger';
import Router from 'koa-router';
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';
2015-08-08 20:37:49 +01:00
import co from 'co';
2015-07-09 23:01:43 +01:00
import api from './api/app';
2015-08-23 01:05:20 +01:00
import { 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());
app.keys = [process.env.KEYS || 'INSECURE'];
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 01:05:20 +01:00
app.use(function* (next) {
this.raven = ravenClient;
yield next;
});
app.ws.use(function* (next) {
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-22 16:16:15 +01:00
app.use(function* (next){
this.set('Server', 'Nintendo 64');
if(this.req.headers['x-forwarded-proto'] === 'http'){
return this.redirect('https://' + this.req.headers.host + this.req.url);
}
2015-08-23 01:05:20 +01:00
try {
yield next;
} catch (err) {
if (!err.statusCode) {
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')));
app.use(serve(path.join(__dirname, 'web/public/'), {maxage: 31536000000}));
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) {
2015-08-08 20:37:49 +01:00
app.listen(process.env.PORT || 4040, function() {
debug('Koa HTTP server listening on port ' + (process.env.PORT || 4040));
});
2015-07-09 23:01:43 +01:00
}
2015-08-11 21:54:55 +01:00
export default app;