unpkg/modules/createRouter.js

104 lines
2.7 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() {
2018-12-17 21:58:59 +00:00
return route(app => {
app.get('/', require('./actions/serveRootPage'));
2018-02-17 00:00:06 +00:00
2018-12-17 21:58:59 +00:00
app.use(cors());
app.use(bodyParser.json());
app.use(require('./middleware/userToken'));
2018-02-17 00:00:06 +00:00
2018-12-17 21:58:59 +00:00
app.use(
'/api',
route(app => {
app.get('/publicKey', require('./actions/showPublicKey'));
2018-09-01 13:37:48 +00:00
2018-12-17 21:58:59 +00:00
app.post('/auth', require('./actions/createAuth'));
app.get('/auth', require('./actions/showAuth'));
2018-09-01 13:37:48 +00:00
2018-12-17 21:58:59 +00:00
app.post(
'/blacklist',
require('./middleware/requireAuth')('blacklist.add'),
require('./actions/addToBlacklist')
);
app.get(
'/blacklist',
require('./middleware/requireAuth')('blacklist.read'),
require('./actions/showBlacklist')
);
app.delete(
'/blacklist',
require('./middleware/requireAuth')('blacklist.remove'),
require('./actions/removeFromBlacklist')
);
2018-09-01 13:37:48 +00:00
2018-12-17 21:58:59 +00:00
if (process.env.NODE_ENV !== 'test') {
app.get('/stats', require('./actions/showStats'));
}
})
);
2018-09-01 13:37:48 +00:00
2018-12-17 21:58:59 +00:00
// TODO: Remove
app.get('/_publicKey', require('./actions/showPublicKey'));
2018-02-17 00:00:06 +00:00
2018-12-17 21:58:59 +00:00
// TODO: Remove
app.use(
'/_auth',
route(app => {
app.post('/', require('./actions/createAuth'));
app.get('/', require('./actions/showAuth'));
})
);
2018-02-17 00:00:06 +00:00
2018-12-17 21:58:59 +00:00
// TODO: Remove
app.use(
'/_blacklist',
route(app => {
app.post(
'/',
require('./middleware/requireAuth')('blacklist.add'),
require('./actions/addToBlacklist')
);
app.get(
'/',
require('./middleware/requireAuth')('blacklist.read'),
require('./actions/showBlacklist')
);
app.delete(
'*',
require('./middleware/requireAuth')('blacklist.remove'),
require('./middleware/validatePackageURL'),
require('./actions/removeFromBlacklist')
);
})
);
2018-02-17 00:00:06 +00:00
2018-12-17 21:58:59 +00:00
// TODO: Remove
if (process.env.NODE_ENV !== 'test') {
app.get('/_stats', require('./actions/showStats'));
}
2018-02-17 00:00:06 +00:00
2018-12-17 21:58:59 +00:00
app.get(
'*',
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
}
module.exports = createRouter;