import { perform } from "./email"; import { redactJob, type EmailJob } from "./job"; import { ConsoleLogger } from "./logger"; export const main = (port: number) => { const server = Bun.serve({ port, async fetch(req) { ConsoleLogger.info(`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 = redactJob(job); const uuid = crypto.randomUUID(); ConsoleLogger.info( `[${uuid}] Received email job: ${JSON.stringify(jobInsensitive)}`, )(); const performEmailTest = perform(job)(); return performEmailTest .then((result) => { if (result._tag === "Left") { const error = result.left; ConsoleLogger.warn( `[${uuid}] job failure due to ${error.message}`, )(); return new Response(error.message, { status: 400, }); } ConsoleLogger.info(`[${uuid}] success`)(); return Response.json({ success: true }); }) .catch((e) => { ConsoleLogger.error(`[${uuid}] internal failure due to ${e}`)(); return new Response(e.message, { status: 500, }); }); } return new Response("404!", { status: 404 }); }, }); ConsoleLogger.info(`Listening on port ${port}`)(); return server; };