Bläddra i källkod

update

master
Daniel Ledda 2 månader sedan
förälder
incheckning
3efd2ff30d
1 ändrade filer med 30 tillägg och 9 borttagningar
  1. +30
    -9
      main.ts

+ 30
- 9
main.ts Visa fil

@@ -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<Response> {
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<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({
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();



Laddar…
Avbryt
Spara