2024-12-15 02:53:26 -05:00
|
|
|
import { perform } from "./email";
|
2024-12-15 16:05:50 -05:00
|
|
|
import { redactJob, type EmailJob } from "./job";
|
2024-12-15 02:53:26 -05:00
|
|
|
import { ConsoleLogger } from "./logger";
|
|
|
|
|
|
|
|
export const main = (port: number) => {
|
|
|
|
const server = Bun.serve({
|
|
|
|
port,
|
|
|
|
async fetch(req) {
|
2024-12-15 16:05:50 -05:00
|
|
|
ConsoleLogger.info(`Received request: ${req.url}`)();
|
2024-12-15 02:53:26 -05:00
|
|
|
|
|
|
|
const url = new URL(req.url);
|
|
|
|
if (req.method === "POST" && url.pathname === "/api/email") {
|
2024-12-15 05:30:42 -05:00
|
|
|
const job: EmailJob = await req.json();
|
2024-12-15 16:05:50 -05:00
|
|
|
const jobInsensitive = redactJob(job);
|
2024-12-15 02:53:26 -05:00
|
|
|
|
2024-12-15 03:00:32 -05:00
|
|
|
const uuid = crypto.randomUUID();
|
2024-12-15 16:05:50 -05:00
|
|
|
ConsoleLogger.info(
|
2024-12-15 03:00:32 -05:00
|
|
|
`[${uuid}] Received email job: ${JSON.stringify(jobInsensitive)}`,
|
2024-12-15 02:53:26 -05:00
|
|
|
)();
|
|
|
|
|
|
|
|
const performEmailTest = perform(job)();
|
2024-12-15 03:00:32 -05:00
|
|
|
return performEmailTest
|
|
|
|
.then((result) => {
|
|
|
|
if (result._tag === "Left") {
|
|
|
|
const error = result.left;
|
2024-12-15 16:05:50 -05:00
|
|
|
ConsoleLogger.warn(
|
2024-12-15 03:00:32 -05:00
|
|
|
`[${uuid}] job failure due to ${error.message}`,
|
|
|
|
)();
|
|
|
|
return new Response(error.message, {
|
|
|
|
status: 400,
|
|
|
|
});
|
|
|
|
}
|
2024-12-15 16:05:50 -05:00
|
|
|
ConsoleLogger.info(`[${uuid}] success`)();
|
2024-12-15 02:53:26 -05:00
|
|
|
return Response.json({ success: true });
|
|
|
|
})
|
2024-12-15 03:00:32 -05:00
|
|
|
.catch((e) => {
|
2024-12-15 16:05:50 -05:00
|
|
|
ConsoleLogger.error(`[${uuid}] internal failure due to ${e}`)();
|
2024-12-15 03:00:32 -05:00
|
|
|
return new Response(e.message, {
|
|
|
|
status: 500,
|
2024-12-15 02:53:26 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return new Response("404!", { status: 404 });
|
|
|
|
},
|
|
|
|
});
|
2024-12-15 16:05:50 -05:00
|
|
|
ConsoleLogger.info(`Listening on port ${port}`)();
|
2024-12-15 02:53:26 -05:00
|
|
|
return server;
|
|
|
|
};
|