-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (48 loc) · 1.41 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
const path = require('path');
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const compress = require('koa-compress');
const conditional = require('koa-conditional-get');
const etag = require('koa-etag');
const hbs = require('koa-hbs');
const mount = require('koa-mount');
const route = require('koa-route');
const serve = require('koa-static');
const UserController = require('./controllers/user.js');
const KindController = require('./controllers/kind.js');
const QueueController = require('./controllers/queue.js');
const app = new Koa();
app.use(compress());
app.use(conditional());
app.use(etag());
app.use(bodyParser());
app.use(hbs.middleware({
viewPath: path.resolve(__dirname, './views'),
}));
app.use(mount('/assets', serve('./public')));
app.use(async (ctx, next) => {
await next();
ctx.set('Cache-Control', 'max-age=86400');
ctx.set('Access-Control-Allow-Origin', '*');
});
app.use(async (ctx, next) => {
try {
await next();
if (ctx.status === 404) {
ctx.throw(404);
}
} catch (err) {
console.error(err);
ctx.status = err.status || 500;
ctx.body = {
error: {
code: ctx.status,
message: err.message,
},
};
}
});
app.use(route.get('/users/:username', UserController.getHTML));
app.use(route.get('/users/:username/:kind/:type.:ext', KindController.get));
app.use(route.get('/queues', QueueController.get));
module.exports = app;