hostr/api/app.js

87 lines
2.1 KiB
JavaScript
Raw Normal View History

2015-08-22 16:16:15 +01:00
import Router from 'koa-router';
2018-06-02 15:50:39 +00:00
import stats from '../lib/koa-statsd';
2015-07-09 23:01:43 +01:00
import cors from 'kcors';
2015-08-22 16:16:15 +01:00
import StatsD from 'statsy';
2015-07-09 23:01:43 +01:00
import auth from './lib/auth';
import * as user from './routes/user';
import * as file from './routes/file';
2016-08-07 14:38:05 +01:00
import * as pro from './routes/pro';
2015-07-09 23:01:43 +01:00
import debugname from 'debug';
const debug = debugname('hostr-api');
2015-08-22 16:16:15 +01:00
const router = new Router();
2015-07-09 23:01:43 +01:00
2016-06-06 15:37:00 +01:00
const statsdOpts = { prefix: 'hostr-api', host: process.env.STATSD_HOST };
2015-08-22 18:24:39 +01:00
router.use(stats(statsdOpts));
2015-08-23 22:12:32 +01:00
const statsd = new StatsD(statsdOpts);
2018-06-02 15:50:39 +00:00
router.use(async (ctx, next) => {
ctx.statsd = statsd;
await next();
2015-08-09 17:21:39 +01:00
});
2015-08-09 13:46:09 +01:00
2015-08-22 16:16:15 +01:00
router.use(cors({
2015-07-09 23:01:43 +01:00
origin: '*',
2015-08-23 22:12:32 +01:00
credentials: true,
2015-07-09 23:01:43 +01:00
}));
2018-06-02 15:50:39 +00:00
router.use(async (ctx, next) => {
2015-07-09 23:01:43 +01:00
try {
2018-06-02 15:50:39 +00:00
await next();
if (ctx.response.status === 404 && !ctx.response.body) {
ctx.throw(404);
2015-07-09 23:01:43 +01:00
}
} catch (err) {
if (err.status === 401) {
2018-06-02 15:50:39 +00:00
ctx.statsd.incr('auth.failure', 1);
ctx.set('WWW-Authenticate', 'Basic');
ctx.status = 401;
ctx.body = err.message;
2015-08-23 22:12:32 +01:00
} else if (err.status === 404) {
2018-06-02 15:50:39 +00:00
ctx.status = 404;
ctx.body = {
2015-07-09 23:01:43 +01:00
error: {
message: 'File not found',
2015-08-23 22:12:32 +01:00
code: 604,
},
2015-07-09 23:01:43 +01:00
};
} else {
if (!err.status) {
debug(err);
2018-06-02 15:50:39 +00:00
if (ctx.raven) {
ctx.raven.captureError(err);
2016-05-25 21:03:07 +01:00
}
2015-07-09 23:01:43 +01:00
throw err;
} else {
2018-06-02 15:50:39 +00:00
ctx.status = err.status;
ctx.body = err.message;
2015-07-09 23:01:43 +01:00
}
}
}
2018-06-02 15:50:39 +00:00
ctx.type = 'application/json';
2015-07-09 23:01:43 +01:00
});
2018-06-02 15:50:39 +00:00
router.delete('/file/:id', auth, file.del);
2015-08-22 16:16:15 +01:00
router.get('/user', auth, user.get);
router.get('/user/token', auth, user.token);
router.get('/token', auth, user.token);
router.get('/user/transaction', auth, user.transaction);
router.post('/user/settings', auth, user.settings);
2016-08-07 14:38:05 +01:00
router.post('/user/pro', auth, pro.create);
router.delete('/user/pro', auth, pro.cancel);
2015-08-22 16:16:15 +01:00
router.get('/file', auth, file.list);
router.post('/file', auth, file.post);
router.get('/file/:id', file.get);
2018-06-02 15:50:39 +00:00
2015-08-22 16:16:15 +01:00
// Hack, if no route matches here, router does not dispatch at all
2018-06-02 15:50:39 +00:00
router.get('/(.*)', async (ctx) => {
ctx.throw(404);
2015-08-22 16:16:15 +01:00
});
2015-07-09 23:01:43 +01:00
2015-08-23 01:05:20 +01:00
export const ws = new Router();
ws.all('/user', user.events);
2015-08-22 16:16:15 +01:00
export default router;