diff --git a/main.ts b/main.ts index cfc50a8..683d582 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,5 @@ import { serveFile } from "jsr:@std/http/file-server"; +import { STATUS_TEXT } from "jsr:@std/http/status"; import { type App, toValue } from "vue"; import { type Router } from "vue-router"; import { renderToString } from "vue/server-renderer"; @@ -92,14 +93,13 @@ async function getAPIResponse(apiReq: Request): Promise { result.push(metadata); } jsonResponse = result; - console.log(result); } - } - if (!jsonResponse) { - jsonResponse = { error: `API route ${ apiPath } not found.` }; - status = 404; - } + if (!jsonResponse) { + jsonResponse = { error: `API route ${ apiPath } not found.` }; + status = 404; + } + } const headers = new Headers(); headers.set("Content-Type", "application/json"); @@ -109,6 +109,15 @@ async function getAPIResponse(apiReq: Request): Promise { }); } +const redirects = { + '/generative-energy/rp-deutsch': { + target: '/generative-energy/raypeat-deutsch', + code: 301, + }, +} as const; + +const redirectPaths = Object.keys(redirects) as (keyof typeof redirects)[]; + Deno.serve({ port: 8080, hostname: "0.0.0.0", @@ -125,12 +134,22 @@ Deno.serve({ if (req.method === "GET") { const pathname = url?.pathname ?? "/"; - if (pathname.startsWith("/api/")) { + // Redirects + const redirect = redirectPaths.find(_ => pathname.startsWith(_)) + if (response === null && redirect) { + const entry = redirects[redirect]; + const headers = new Headers(); + headers.set('Location', entry.target); + response = new Response(STATUS_TEXT[entry.code], { headers, status: entry.code }); + } + + // API + if (response === null && pathname.startsWith("/api/")) { response = await getAPIResponse(req); } // Public/static files - if (!response) { + if (response === null) { let filepath = join(".", "public", pathname); if (filepath.endsWith("/")) { filepath = join(filepath, "index.html"); @@ -191,7 +210,9 @@ Deno.serve({ response = new Response("Only GET allowed.", { status: 500 }); } - response ??= new Response("Not found.", { status: 404 }) + if (response === null) { + response = new Response("Not found.", { status: 404 }) + } const timeEnd = new Date().getTime();