unpkg/server/createRouter.js

72 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-02-17 00:00:06 +00:00
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
function route(setup) {
const app = express.Router();
setup(app);
return app;
}
function createRouter() {
const app = express.Router();
2018-07-09 21:03:53 +00:00
app.get("/", require("./actions/serveMainPage"));
2018-02-17 00:00:06 +00:00
app.use(cors());
app.use(bodyParser.json());
app.use(require("./middleware/userToken"));
app.get("/_publicKey", require("./actions/showPublicKey"));
app.use(
"/_auth",
route(app => {
app.post("/", require("./actions/createAuth"));
app.get("/", require("./actions/showAuth"));
})
);
app.use(
"/_blacklist",
route(app => {
app.post(
"/",
2018-05-19 15:34:36 +00:00
require("./middleware/requireAuth")("blacklist.add"),
2018-02-17 00:00:06 +00:00
require("./actions/addToBlacklist")
);
app.get(
"/",
2018-05-19 15:34:36 +00:00
require("./middleware/requireAuth")("blacklist.read"),
2018-02-17 00:00:06 +00:00
require("./actions/showBlacklist")
);
app.delete(
"*",
2018-05-19 15:34:36 +00:00
require("./middleware/requireAuth")("blacklist.remove"),
2018-02-17 00:00:06 +00:00
require("./middleware/validatePackageURL"),
require("./actions/removeFromBlacklist")
);
})
);
if (process.env.NODE_ENV !== "test") {
app.get("/_stats", require("./actions/showStats"));
}
app.get(
"*",
2018-05-19 15:34:36 +00:00
require("./middleware/redirectLegacyURLs"),
require("./middleware/validatePackageURL"),
require("./middleware/validatePackageName"),
require("./middleware/validateQuery"),
2018-02-17 00:00:06 +00:00
require("./middleware/checkBlacklist"),
2018-05-17 22:21:35 +00:00
require("./middleware/fetchPackage"),
require("./middleware/findFile"),
2018-02-17 00:00:06 +00:00
require("./actions/serveFile")
);
return app;
}
module.exports = createRouter;