add cdn redirects

This commit is contained in:
zkldi
2021-07-02 16:20:27 +01:00
parent 1606fefdc0
commit 32bca09f4b
3 changed files with 43 additions and 0 deletions
@@ -0,0 +1,20 @@
import t from "tap";
import { CDNStoreOrOverwrite } from "../../../lib/cdn/cdn";
import { CloseAllConnections } from "../../../test-utils/close-connections";
import mockApi from "../../../test-utils/mock-api";
t.test("GET /cdn", (t) => {
t.test("Should return this content at the CDN.", async (t) => {
await CDNStoreOrOverwrite("/foo", "hello world");
const res = await mockApi.get("/cdn/foo");
t.equal(res.body.toString(), "hello world");
t.end();
});
t.end();
});
t.teardown(CloseAllConnections);
+21
View File
@@ -0,0 +1,21 @@
import { Router } from "express";
import { CDNRedirect } from "../../../lib/cdn/cdn";
const router: Router = Router({ mergeParams: true });
/**
* Redirect all /cdn/ requests to this servers' CDN. This is so the client can refer to
* (url)/cdn, and still have it resolve to our CDN internally.
*
* @example <script src="/cdn/foo"></script> instead of <script src= "{{CDN_URL}}/foo"></script>
*
* @name GET /cdn/*
*/
router.get("*", (req, res) => {
console.log(req.originalUrl);
const url = req.originalUrl.replace(/^\/cdn/u, "");
console.log(url);
return CDNRedirect(res, url);
});
export default router;
+2
View File
@@ -3,6 +3,7 @@ import { SetRequestPermissions } from "../middleware/auth";
import { RateLimitMiddleware } from "../middleware/rate-limiter";
import apiRouterV1 from "./api/v1/router";
import irRouter from "./ir/router";
import cdnReRouter from "./cdn/router";
const router: Router = Router({ mergeParams: true });
@@ -15,5 +16,6 @@ router.use("/ir", irRouter);
// but also may require custom authentication.
router.use(SetRequestPermissions);
router.use("/api/v1", apiRouterV1);
router.use("/cdn", cdnReRouter);
export default router;