Skip to content

Commit

Permalink
add server debugging for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhouweling committed Nov 5, 2024
1 parent 888d94c commit 1daec86
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions src/server.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,46 @@ import { getCommands } from "./commands/commands.mjs";
const router = AutoRouter();

router.get("/", (_request, env: Env) => {
return new Response(`👋 ${env.DISCORD_APP_ID}`);
return new Response(`👋 ${env.DISCORD_APP_ID} 🚀`);
});

router.post("/interactions", async (request, env: Env) => {
const services = installServices({ env });
const { discordService } = services;
const commands = getCommands(services);
discordService.setCommands(commands);

const { isValid, interaction } = await discordService.verifyDiscordRequest(request);
if (!isValid || !interaction) {
return new Response("Bad request signature.", { status: 401 });
router.get("/test", async (_request, env: Env) => {
try {
console.log("test called, awaiting timeout...");
await new Promise((resolve) => {
setTimeout(() => {
resolve(undefined);
}, 5000);
});
console.log("timeout completed, returning response");
return new Response(`👋 ${env.DISCORD_APP_ID}`);
} catch (error) {
console.error(error);

return new Response("Internal error", { status: 500 });
}
});

router.post("/interactions", async (request, env: Env) => {
try {
const services = installServices({ env });
const { discordService } = services;
const commands = getCommands(services);
discordService.setCommands(commands);

const response = await discordService.handleInteraction(interaction);
const { isValid, interaction } = await discordService.verifyDiscordRequest(request);
if (!isValid || !interaction) {
return new Response("Bad request signature.", { status: 401 });
}

return response;
const response = await discordService.handleInteraction(interaction);

return response;
} catch (error) {
console.error(error);

return new Response("Internal error", { status: 500 });
}
});
router.all("*", () => new Response("Not Found.", { status: 404 }));

Expand Down

0 comments on commit 1daec86

Please sign in to comment.