-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
70 lines (64 loc) · 1.75 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
require('dotenv').config();
const koa = require('koa');
const helmet = require('koa-helmet');
const logger = require('koa-logger');
const cors = require('kcors');
const bodyParser = require('koa-bodyparser');
const compress = require('koa-compress');
const monk = require('monk');
const app = (module.exports = new koa());
const routes = require('./router.js');
const db = monk(process.env.MONGOLAB_URI);
const User = db.get('users');
const Raven = require('raven');
Raven.config(process.env.SENTRY_DSN).install();
// Logger
app
.use(logger())
.use(helmet())
.use(cors())
.use(bodyParser())
.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.body = undefined;
switch (ctx.status) {
case 401:
ctx.app.emit('error', err, this);
break;
case 400:
ctx.app.emit('Resource error', err, this);
break;
case 404:
ctx.app.emit('Resource not found', err, this);
break;
default:
if (err.message) {
ctx.body = { errors: [err.message] };
}
ctx.app.emit('error', err, this);
}
}
})
.use(async (ctx, next) => {
let authorization = ctx.headers.authorization;
if (!authorization || authorization.split(' ')[0] != 'Bearer')
return await next();
ctx.token = authorization.split(' ')[1];
// console.log('authorization accessToken', ctx.token);
ctx.user = await User.findOne({ accessToken: ctx.token });
return await next();
});
routes(app);
// Compress
app.use(compress());
// Raven
app.on('error', async err => {
await Raven.captureException(err, (err, eventId) => {
//eslint-disable-next-line no-console
console.log(`Reported error ${eventId}`);
});
});
module.exports = app;