eris/api/mod.ts

48 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-10-05 09:00:51 +00:00
import { route } from "reroute";
import { serveSpa } from "serve_spa";
import { api } from "./serveApi.ts";
2023-10-19 21:37:03 +00:00
import { fromFileUrl } from "std/path/mod.ts";
2023-10-05 09:00:51 +00:00
// Export the Webhook Route function so it can be used in bot/mod.ts
export async function handleWebhook(req: Request): Promise<Response> {
if (req.method === "POST" && new URL(req.url).pathname === "/webhook") {
try {
const body = await req.json();
console.log("Received webhook data:", JSON.stringify(body, null, 2));
// Log before processing update
console.log("Processing update through handleUpdate...");
await bot.handleUpdate(body); // Process the update
// Log after processing update
console.log("Update processed successfully.");
return new Response(JSON.stringify({ status: 'ok' }), { headers: { 'Content-Type': 'application/json' } });
} catch (error) {
// Detailed error logging
console.error("Error in handleRequest or handleUpdate:", error);
return new Response("Error", { status: 500 });
}
}
return new Response("Not Found", { status: 404 });
}
2023-10-05 09:00:51 +00:00
export async function serveUi() {
const server = Deno.serve({ port: 8443 }, (request) =>
2023-10-05 09:00:51 +00:00
route(request, {
"/api/*": (request) => api.fetch(request),
"/webhook": handleWebhook, // Create the Webhook Route handle
2023-10-05 09:00:51 +00:00
"/*": (request) =>
serveSpa(request, {
2023-10-13 18:56:16 +00:00
fsRoot: fromFileUrl(new URL("../ui/", import.meta.url)),
2023-10-05 09:00:51 +00:00
indexFallback: true,
importMapFile: "../deno.json",
aliasMap: {
"/utils/*": "../utils/",
2023-09-26 10:43:36 +00:00
},
2023-10-09 19:03:31 +00:00
log: (_request, response) => response.status >= 400,
2023-10-05 09:00:51 +00:00
}),
}));
2023-09-26 10:43:36 +00:00
await server.finished;
}