hostr/api/app.js

124 lines
3 KiB
JavaScript
Raw Normal View History

2015-07-09 23:01:43 +01:00
import koa from 'koa';
import route from 'koa-route';
2015-08-22 13:35:06 +01:00
import stats from 'koa-statsd';
2015-07-09 23:01:43 +01:00
import websockify from 'koa-websocket';
import logger from 'koa-logger';
import compress from 'koa-compress';
import bodyparser from 'koa-bodyparser';
import cors from 'kcors';
import co from 'co';
import raven from 'raven';
import auth from './lib/auth';
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 user from './routes/user';
import * as file from './routes/file';
import debugname from 'debug';
const debug = debugname('hostr-api');
2015-08-09 17:21:39 +01:00
import StatsD from 'statsy';
2015-07-09 23:01:43 +01:00
if (process.env.SENTRY_DSN) {
const ravenClient = new raven.Client(process.env.SENTRY_DSN);
ravenClient.patchGlobal();
}
const app = websockify(koa());
2015-08-22 13:57:19 +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);
}
yield next;
});
2015-07-09 23:01:43 +01:00
2015-08-09 17:21:39 +01:00
let statsdOpts = {prefix: 'hostr-api', 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(logger());
app.use(cors({
origin: '*',
credentials: true
}));
2015-08-22 13:35:06 +01:00
app.use(mongo());
2015-08-22 13:57:19 +01:00
app.use(redis());
app.ws.use(mongo());
app.ws.use(redis());
2015-07-09 23:01:43 +01:00
app.use(route.get('/', function* (){
this.status = 200;
this.body = '';
}));
app.use(function* (next){
try {
yield next;
if (this.response.status === 404 && !this.response.body) {
this.throw(404);
}
} catch (err) {
if (err.status === 401) {
2015-08-09 17:21:39 +01:00
this.statsd.incr('auth.failure', 1);
2015-07-09 23:01:43 +01:00
this.set('WWW-Authenticate', 'Basic');
this.status = 401;
this.body = err.message;
} else if(err.status === 404) {
this.status = 404;
this.body = {
error: {
message: 'File not found',
code: 604
}
};
} else {
if (!err.status) {
debug(err);
throw err;
} else {
this.status = err.status;
this.body = err.message;
}
}
}
this.type = 'application/json';
});
app.use(compress());
app.use(bodyparser());
app.ws.use(route.all('/file/:id', file.events));
app.ws.use(route.all('/user', user.events));
app.use(route.get('/file/:id', file.get));
// Run auth middleware before all other endpoints
app.use(auth);
app.use(route.get('/user', user.get));
app.use(route.get('/user/token', user.token));
2015-08-08 20:37:49 +01:00
app.use(route.get('/token', user.token));
2015-07-09 23:01:43 +01:00
app.use(route.get('/user/transaction', user.transaction));
app.use(route.post('/user/settings', user.settings));
app.use(route.get('/file', file.list));
app.use(route.post('/file', file.post));
app.use(route.put('/file/:id', file.put));
app.use(route.delete('/file/:id', file.del));
if (!module.parent) {
2015-08-09 17:21:39 +01:00
app.listen(process.env.PORT || 4042, function() {
debug('Koa HTTP server listening on port ' + (process.env.PORT || 4042));
});
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;