From a2fbc45b8f5cc9ca6fb14168bd7c61a0a3a228d5 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Sun, 31 Dec 2023 14:20:51 +0100 Subject: [PATCH] Add script to redirect legacy urls --- scripts/legacyRedirector.ts | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 scripts/legacyRedirector.ts diff --git a/scripts/legacyRedirector.ts b/scripts/legacyRedirector.ts new file mode 100644 index 000000000..06b29df71 --- /dev/null +++ b/scripts/legacyRedirector.ts @@ -0,0 +1,45 @@ +import * as http from "node:http" + +/** + * Redirect people from + * "mapcomplete.osm.be/path?query=parameter#id" to "mapcomplete.org/path?query=parameter#id" + */ +const PORT = 8080 +const CORS = "http://localhost:1234,https://mapcomplete.org,https://dev.mapcomplete.org" + +async function redirect(req: http.IncomingMessage, res: http.ServerResponse) { + try { + console.log( + req.method + " " + req.url, + "from:", + req.headers.origin, + new Date().toISOString() + ) + res.setHeader( + "Access-Control-Allow-Headers", + "Origin, X-Requested-With, Content-Type, Accept" + ) + res.setHeader("Access-Control-Allow-Origin", req.headers.origin ?? "*") + if (req.method === "OPTIONS") { + res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, UPDATE") + res.writeHead(204, { "Content-Type": "text/html" }) + res.end() + return + } + + console.log("Request url:", req.url) + const oldUrl = new URL("https://127.0.0.1:8080" + req.url) + const newUrl = "https://mapcomplete.org" + oldUrl.pathname + oldUrl.search + oldUrl.hash + res.writeHead(301, { "Content-Type": "text/html", Location: newUrl }) + res.write("Moved permantently") + res.end() + } catch (e) { + console.error(e) + } +} + +http.createServer(redirect).listen(PORT) + +console.log( + `Server started at http://127.0.0.1:${PORT}/, the time is ${new Date().toISOString()}, version from package.json is` +)