Update stuff
This commit is contained in:
parent
0254e42b9c
commit
553ba9db9a
40 changed files with 7343 additions and 717 deletions
47
api/app.js
47
api/app.js
|
@ -1,5 +1,5 @@
|
|||
import Router from 'koa-router';
|
||||
import stats from 'koa-statsd';
|
||||
import stats from '../lib/koa-statsd';
|
||||
import cors from 'kcors';
|
||||
import StatsD from 'statsy';
|
||||
import auth from './lib/auth';
|
||||
|
@ -14,9 +14,9 @@ const router = new Router();
|
|||
const statsdOpts = { prefix: 'hostr-api', host: process.env.STATSD_HOST };
|
||||
router.use(stats(statsdOpts));
|
||||
const statsd = new StatsD(statsdOpts);
|
||||
router.use(function* statsMiddleware(next) {
|
||||
this.statsd = statsd;
|
||||
yield next;
|
||||
router.use(async (ctx, next) => {
|
||||
ctx.statsd = statsd;
|
||||
await next();
|
||||
});
|
||||
|
||||
router.use(cors({
|
||||
|
@ -24,21 +24,22 @@ router.use(cors({
|
|||
credentials: true,
|
||||
}));
|
||||
|
||||
router.use('*', function* authMiddleware(next) {
|
||||
router.use(async (ctx, next) => {
|
||||
try {
|
||||
yield next;
|
||||
if (this.response.status === 404 && !this.response.body) {
|
||||
this.throw(404);
|
||||
await next();
|
||||
|
||||
if (ctx.response.status === 404 && !ctx.response.body) {
|
||||
ctx.throw(404);
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.status === 401) {
|
||||
this.statsd.incr('auth.failure', 1);
|
||||
this.set('WWW-Authenticate', 'Basic');
|
||||
this.status = 401;
|
||||
this.body = err.message;
|
||||
ctx.statsd.incr('auth.failure', 1);
|
||||
ctx.set('WWW-Authenticate', 'Basic');
|
||||
ctx.status = 401;
|
||||
ctx.body = err.message;
|
||||
} else if (err.status === 404) {
|
||||
this.status = 404;
|
||||
this.body = {
|
||||
ctx.status = 404;
|
||||
ctx.body = {
|
||||
error: {
|
||||
message: 'File not found',
|
||||
code: 604,
|
||||
|
@ -47,19 +48,20 @@ router.use('*', function* authMiddleware(next) {
|
|||
} else {
|
||||
if (!err.status) {
|
||||
debug(err);
|
||||
if (this.raven) {
|
||||
this.raven.captureError(err);
|
||||
if (ctx.raven) {
|
||||
ctx.raven.captureError(err);
|
||||
}
|
||||
throw err;
|
||||
} else {
|
||||
this.status = err.status;
|
||||
this.body = err.message;
|
||||
ctx.status = err.status;
|
||||
ctx.body = err.message;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.type = 'application/json';
|
||||
ctx.type = 'application/json';
|
||||
});
|
||||
|
||||
router.delete('/file/:id', auth, file.del);
|
||||
router.get('/user', auth, user.get);
|
||||
router.get('/user/token', auth, user.token);
|
||||
router.get('/token', auth, user.token);
|
||||
|
@ -70,12 +72,11 @@ router.delete('/user/pro', auth, pro.cancel);
|
|||
router.get('/file', auth, file.list);
|
||||
router.post('/file', auth, file.post);
|
||||
router.get('/file/:id', file.get);
|
||||
router.delete('/file/:id', auth, file.del);
|
||||
router.delete('/file/:id', auth, file.del);
|
||||
|
||||
|
||||
// Hack, if no route matches here, router does not dispatch at all
|
||||
router.get('/(.*)', function* errorMiddleware() {
|
||||
this.throw(404);
|
||||
router.get('/(.*)', async (ctx) => {
|
||||
ctx.throw(404);
|
||||
});
|
||||
|
||||
export const ws = new Router();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue