hostr/web/app.js

132 lines
4 KiB
JavaScript
Raw Normal View History

2015-07-09 23:01:43 +01:00
import path from 'path';
import koa from 'koa';
import csrf from 'koa-csrf';
2015-07-09 23:01:43 +01:00
import route from 'koa-route';
import views from 'koa-views';
2015-08-22 13:35:06 +01:00
import stats from 'koa-statsd';
2015-07-09 23:01:43 +01:00
import logger from 'koa-logger';
import favicon from 'koa-favicon';
import redisStore from 'koa-redis';
import compress from 'koa-compress';
import bodyparser from 'koa-bodyparser';
import session from 'koa-generic-session';
import staticHandler from 'koa-file-server';
import co from 'co';
import raven from 'raven';
2015-08-22 13:35:06 +01:00
import StatsD from 'statsy';
2015-07-09 23:01:43 +01:00
// waiting for PR to be merged, can remove swig dependency when done
import errors from '../lib/koa-error';
2015-08-22 13:35:06 +01:00
import mongo from '../lib/mongo';
2015-08-22 13:57:19 +01:00
import redis from '../lib/redis';
2015-07-09 23:01:43 +01:00
import * as index from './routes/index';
import * as file from './routes/file';
import * as pro from './routes/pro';
import * as user from './routes/user';
2015-08-22 13:35:06 +01:00
2015-07-09 23:01:43 +01:00
import debugname from 'debug';
const debug = debugname('hostr-web');
if (process.env.SENTRY_DSN) {
const ravenClient = new raven.Client(process.env.SENTRY_DSN);
ravenClient.patchGlobal();
}
const redisUrl = process.env.REDIS_URL || process.env.REDISTOGO_URL || 'redis://localhost:6379';
const app = koa();
2015-08-09 17:21:39 +01:00
let statsdOpts = {prefix: 'hostr-web', host: process.env.STATSD_HOST || 'localhost'};
let statsd = new StatsD(statsdOpts);
app.use(function*(next) {
this.statsd = statsd;
yield next;
});
app.use(stats(statsdOpts));
2015-08-09 13:46:09 +01:00
2015-07-09 23:01:43 +01:00
app.use(errors({template: path.join(__dirname, 'public', '404.html')}));
app.use(function*(next){
this.set('Server', 'Nintendo 64');
if(this.req.headers['x-forwarded-proto'] === 'http'){
return this.redirect('https://' + this.request.headers.host + this.request.url);
}
yield next;
});
app.use(function*(next){
this.state = {
apiURL: process.env.API_URL,
baseURL: process.env.BASE_URL,
stripePublic: process.env.STRIPE_PUBLIC_KEY
};
yield next;
});
2015-08-22 13:35:06 +01:00
app.use(mongo());
2015-08-22 13:57:19 +01:00
app.use(redis());
2015-07-09 23:01:43 +01:00
app.use(compress());
app.use(bodyparser());
app.use(favicon(path.join(__dirname, 'public/images/favicon.png')));
app.use(staticHandler({root: path.join(__dirname, 'public'), maxage: 31536000000}));
app.use(logger());
app.use(views('views', {
default: 'ejs'
}));
app.keys = [process.env.KEYS || 'INSECURE'];
app.use(session({
2015-08-22 13:57:19 +01:00
store: redisStore({client: redis().client})
2015-07-09 23:01:43 +01:00
}));
app.use(route.get('/', index.main));
app.use(route.get('/account', index.main));
app.use(route.get('/billing', index.main));
app.use(route.get('/pro', index.main));
app.use(route.get('/signin', user.signin));
app.use(route.post('/signin', user.signin));
app.use(route.get('/signup', user.signup));
app.use(route.post('/signup', user.signup));
app.use(route.get('/logout', user.logout));
app.use(route.post('/logout', user.logout));
app.use(route.get('/forgot', user.forgot));
app.use(route.get('/forgot/:token', user.forgot));
app.use(route.post('/forgot/:token', user.forgot));
app.use(route.post('/forgot', user.forgot));
app.use(route.get('/activate/:code', user.activate));
app.use(route.get('/terms', index.staticPage));
app.use(route.get('/privacy', index.staticPage));
app.use(route.get('/pricing', index.staticPage));
app.use(route.get('/apps', index.staticPage));
app.use(route.get('/stats', index.staticPage));
app.use(route.post('/pro/create', pro.create));
app.use(route.post('/pro/cancel', pro.cancel));
app.use(route.get('/:id', file.landing));
2015-08-11 21:54:55 +01:00
app.use(route.get('/download/:id/:name', function* (id) {
2015-07-09 23:01:43 +01:00
this.redirect('/' + id);
}));
app.use(route.get('/file/:id/:name', file.get));
app.use(route.get('/files/:id/:name', file.get));
app.use(route.get('/file/:size/:id/:name', file.resized));
2015-08-11 21:54:55 +01:00
app.use(route.get('/updaters/mac', function* () {
2015-07-09 23:01:43 +01:00
this.redirect('/updaters/mac.xml');
}));
2015-08-11 21:54:55 +01:00
app.use(route.get('/updaters/mac/changelog', function* () {
yield this.render('mac-update-changelog');
2015-07-09 23:01:43 +01:00
}));
if (!module.parent) {
2015-08-08 20:37:49 +01:00
app.listen(process.env.PORT || 4041, function() {
debug('Koa HTTP server listening on port ' + (process.env.PORT || 4041));
});
2015-08-11 21:54:55 +01:00
setInterval(function() {
debug('%sMB', process.memoryUsage().rss / 1024 / 1024);
}, 10000);
2015-07-09 23:01:43 +01:00
}
export default app;