unpkg/modules/createRouter.js

106 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-12-17 17:38:05 +00:00
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
2018-02-17 00:00:06 +00:00
function route(setup) {
const app = express.Router();
setup(app);
return app;
}
function createRouter() {
const app = express.Router();
2018-12-17 17:38:05 +00:00
app.get('/', require('./actions/serveRootPage'));
2018-02-17 00:00:06 +00:00
app.use(cors());
app.use(bodyParser.json());
2018-12-17 17:38:05 +00:00
app.use(require('./middleware/userToken'));
2018-02-17 00:00:06 +00:00
2018-09-01 13:37:48 +00:00
app.use(
2018-12-17 17:38:05 +00:00
'/api',
2018-09-01 13:37:48 +00:00
route(app => {
2018-12-17 17:38:05 +00:00
app.get('/publicKey', require('./actions/showPublicKey'));
2018-09-01 13:37:48 +00:00
2018-12-17 17:38:05 +00:00
app.post('/auth', require('./actions/createAuth'));
app.get('/auth', require('./actions/showAuth'));
2018-09-01 13:37:48 +00:00
app.post(
2018-12-17 17:38:05 +00:00
'/blacklist',
require('./middleware/requireAuth')('blacklist.add'),
require('./actions/addToBlacklist')
2018-09-01 13:37:48 +00:00
);
app.get(
2018-12-17 17:38:05 +00:00
'/blacklist',
require('./middleware/requireAuth')('blacklist.read'),
require('./actions/showBlacklist')
2018-09-01 13:37:48 +00:00
);
app.delete(
2018-12-17 17:38:05 +00:00
'/blacklist',
require('./middleware/requireAuth')('blacklist.remove'),
require('./actions/removeFromBlacklist')
2018-09-01 13:37:48 +00:00
);
2018-12-17 17:38:05 +00:00
if (process.env.NODE_ENV !== 'test') {
app.get('/stats', require('./actions/showStats'));
2018-09-01 13:37:48 +00:00
}
})
);
// TODO: Remove
2018-12-17 17:38:05 +00:00
app.get('/_publicKey', require('./actions/showPublicKey'));
2018-02-17 00:00:06 +00:00
2018-09-01 13:37:48 +00:00
// TODO: Remove
2018-02-17 00:00:06 +00:00
app.use(
2018-12-17 17:38:05 +00:00
'/_auth',
2018-02-17 00:00:06 +00:00
route(app => {
2018-12-17 17:38:05 +00:00
app.post('/', require('./actions/createAuth'));
app.get('/', require('./actions/showAuth'));
2018-02-17 00:00:06 +00:00
})
);
2018-09-01 13:37:48 +00:00
// TODO: Remove
2018-02-17 00:00:06 +00:00
app.use(
2018-12-17 17:38:05 +00:00
'/_blacklist',
2018-02-17 00:00:06 +00:00
route(app => {
app.post(
2018-12-17 17:38:05 +00:00
'/',
require('./middleware/requireAuth')('blacklist.add'),
require('./actions/addToBlacklist')
2018-02-17 00:00:06 +00:00
);
app.get(
2018-12-17 17:38:05 +00:00
'/',
require('./middleware/requireAuth')('blacklist.read'),
require('./actions/showBlacklist')
2018-02-17 00:00:06 +00:00
);
app.delete(
2018-12-17 17:38:05 +00:00
'*',
require('./middleware/requireAuth')('blacklist.remove'),
require('./middleware/validatePackageURL'),
require('./actions/removeFromBlacklist')
2018-02-17 00:00:06 +00:00
);
})
);
2018-09-01 13:37:48 +00:00
// TODO: Remove
2018-12-17 17:38:05 +00:00
if (process.env.NODE_ENV !== 'test') {
app.get('/_stats', require('./actions/showStats'));
2018-02-17 00:00:06 +00:00
}
app.get(
2018-12-17 17:38:05 +00:00
'*',
require('./middleware/redirectLegacyURLs'),
require('./middleware/validatePackageURL'),
require('./middleware/validatePackageName'),
require('./middleware/validateQuery'),
require('./middleware/checkBlacklist'),
require('./middleware/fetchPackage'),
require('./middleware/findFile'),
require('./actions/serveFile')
2018-02-17 00:00:06 +00:00
);
return app;
}
module.exports = createRouter;