hostr/app.js

88 lines
2.1 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';
2019-01-14 21:37:03 +00:00
import * as Sentry from '@sentry/node';
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) {
2019-01-14 21:53:36 +00:00
Sentry.init({
dsn: process.env.SENTRY_DSN,
2019-01-14 22:06:27 +00:00
release: process.env.GIT_REV,
2019-01-14 21:53:36 +00:00
});
2019-01-14 21:37:03 +00:00
app.on('error', (err) => {
if (err.statusCode === 404) return;
Sentry.captureException(err, (_err, eventId) => {
debug('Reported error', eventId);
2019-01-13 22:32:48 +00:00
});
});
2018-06-02 15:50:39 +00:00
app.use(async (ctx, next) => {
2019-01-14 21:37:03 +00:00
ctx.Sentry = Sentry;
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) => {
2019-01-14 21:37:03 +00:00
ctx.Sentry = Sentry;
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') {
2018-06-02 19:42:45 +00:00
ctx.redirect(`https://${ctx.req.headers.host}${ctx.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 19:01:31 +00:00
if (!err.statusCode && process.env.SENTRY_DSN) {
2019-01-14 21:37:03 +00:00
Sentry.captureException(err, (_err, eventId) => {
debug('Reported error', eventId);
});
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());
2019-01-13 17:48:27 +00:00
if (process.env.DEBUG === 'true') {
app.use(logger());
}
2015-08-22 16:16:15 +01:00
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;