uptime/src/api.ts

41 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-12-15 02:53:26 -05:00
import { perform } from "./email";
import type { EmailJob } from "./job";
import { ConsoleLogger } from "./logger";
export const main = (port: number) => {
const server = Bun.serve({
port,
async fetch(req) {
ConsoleLogger.log(`Received request: ${req.url}`)();
const url = new URL(req.url);
if (req.method === "POST" && url.pathname === "/api/email") {
const job: EmailJob = await req.json();
const jobInsensitive = structuredClone(job);
jobInsensitive.from.username = "****REDACTED****";
jobInsensitive.from.password = "****REDACTED****";
jobInsensitive.to.username = "****REDACTED****";
jobInsensitive.to.password = "****REDACTED****";
ConsoleLogger.log(
`Received email job: ${JSON.stringify(jobInsensitive)}`,
)();
const performEmailTest = perform(job)();
return await performEmailTest
.then(() => {
return Response.json({ success: true });
})
.catch((error) => {
return new Response(error.message, {
status: 400,
});
});
}
return new Response("404!", { status: 404 });
},
});
ConsoleLogger.log(`Listening on port ${port}`)();
return server;
};