summaryrefslogtreecommitdiff
path: root/src/api.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/api.ts')
-rw-r--r--src/api.ts40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/api.ts b/src/api.ts
new file mode 100644
index 0000000..d8a3008
--- /dev/null
+++ b/src/api.ts
@@ -0,0 +1,40 @@
+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;
+};