This commit is contained in:
Daniel Ledda
2024-11-01 21:52:22 +01:00
parent 20479958bd
commit 3efd2ff30d

39
main.ts
View File

@@ -1,4 +1,5 @@
import { serveFile } from "jsr:@std/http/file-server"; import { serveFile } from "jsr:@std/http/file-server";
import { STATUS_TEXT } from "jsr:@std/http/status";
import { type App, toValue } from "vue"; import { type App, toValue } from "vue";
import { type Router } from "vue-router"; import { type Router } from "vue-router";
import { renderToString } from "vue/server-renderer"; import { renderToString } from "vue/server-renderer";
@@ -92,15 +93,14 @@ async function getAPIResponse(apiReq: Request): Promise<Response> {
result.push(metadata); result.push(metadata);
} }
jsonResponse = result; 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(); const headers = new Headers();
headers.set("Content-Type", "application/json"); headers.set("Content-Type", "application/json");
return new Response(JSON.stringify(jsonResponse), { return new Response(JSON.stringify(jsonResponse), {
@@ -109,6 +109,15 @@ async function getAPIResponse(apiReq: Request): Promise<Response> {
}); });
} }
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({ Deno.serve({
port: 8080, port: 8080,
hostname: "0.0.0.0", hostname: "0.0.0.0",
@@ -125,12 +134,22 @@ Deno.serve({
if (req.method === "GET") { if (req.method === "GET") {
const pathname = url?.pathname ?? "/"; 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); response = await getAPIResponse(req);
} }
// Public/static files // Public/static files
if (!response) { if (response === null) {
let filepath = join(".", "public", pathname); let filepath = join(".", "public", pathname);
if (filepath.endsWith("/")) { if (filepath.endsWith("/")) {
filepath = join(filepath, "index.html"); filepath = join(filepath, "index.html");
@@ -191,7 +210,9 @@ Deno.serve({
response = new Response("Only GET allowed.", { status: 500 }); 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(); const timeEnd = new Date().getTime();