-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrouter.js
53 lines (46 loc) · 1.51 KB
/
router.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
const Router = require('koa-router');
const searchController = require('../controllers/SearchController');
const locationController = require('../controllers/LocationController');
const router = new Router();
// @todo
// temporary hack
// check if permitted request
const { API_KEYS } = process.env;
const allowedKeys = API_KEYS ? API_KEYS.split(',') : [];
router.use(async (ctx, next) => {
const { key } = ctx.query;
if (!allowedKeys.includes(key)) {
const error = 'Invalid API key';
return ctx.abortJson({}, error);
}
await next();
});
// @todo
// temporary hack
// don't allow post and put requests in production
// to avoid abuse
async function disallowRouteInProd(ctx, next) {
const production = process.env.NODE_ENV === 'production';
const { method } = ctx;
const error = 'Route not allowed';
if (production && method !== 'GET') return ctx.abortJson({}, error);
await next();
}
router.use(['/location', '/location/:id'], disallowRouteInProd);
// =================
// index
// =================
router.get('/', ctx => (ctx.body = 'Welcome to the GoVote Api'));
// =================
// search
// =================
router.get('/search', searchController.search);
// =================
// locations
// =================
router.get('/location', locationController.index);
router.post('/location', locationController.store);
router.get('/location/:id', locationController.show);
router.put('/location/:id', locationController.update);
router.del('/location/:id', locationController.destroy);
module.exports = router;