diff options
Diffstat (limited to 'services/docker/mail-server/relay/aws/app.ts')
-rw-r--r-- | services/docker/mail-server/relay/aws/app.ts | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/services/docker/mail-server/relay/aws/app.ts b/services/docker/mail-server/relay/aws/app.ts index 51da795..4b60853 100644 --- a/services/docker/mail-server/relay/aws/app.ts +++ b/services/docker/mail-server/relay/aws/app.ts @@ -2,12 +2,12 @@ import { parseArgs } from "@std/cli"; import { z } from "zod"; import { zValidator } from "@hono/zod-validator"; -import { error, log } from "../logger.ts"; +import log from "../log.ts"; +import config from "../config.ts"; import { AppBase } from "../app.ts"; import { AwsContext } from "./context.ts"; import { AwsMailDeliverer } from "./deliver.ts"; import { AwsMailRetriever } from "./retriever.ts"; -import config from "../config.ts"; export class AwsRelayApp extends AppBase { readonly #aws = new AwsContext(); @@ -19,20 +19,25 @@ export class AwsRelayApp extends AppBase { this.#retriever = new AwsMailRetriever(this.#aws, this.inboundDeliverer); this.hono.post( - "/receive/s3", + `/${config.get("awsInboundPath")}`, + async (ctx, next) => { + const auth = ctx.req.header("Authorization"); + if (auth !== config.get("awsInboundKey")) { + return ctx.json({ "msg": "Bad auth!" }, 403); + } + await next(); + }, zValidator( "json", z.object({ key: z.string(), + recipients: z.optional(z.array(z.string())), }), ), async (ctx) => { - await this.#retriever.deliverS3Mail( - ctx.req.valid("json").key, - ); - return ctx.json({ - "msg": "Done!", - }); + const { key, recipients } = ctx.req.valid("json"); + await this.#retriever.deliverS3Mail(key, recipients); + return ctx.json({ "msg": "Done!" }); }, ); } @@ -52,13 +57,13 @@ export class AwsRelayApp extends AppBase { readonly cli = { "init": (_: unknown) => { - log("Just init!"); + log.info("Just init!"); return Promise.resolve(); }, "list-lives": async (_: unknown) => { const liveMails = await this.#retriever.listLiveMails(); - log(`Total ${liveMails.length}:`); - log(liveMails.join("\n")); + log.info(`Total ${liveMails.length}:`); + log.info(liveMails.join("\n")); }, "recycle-lives": async (_: unknown) => { await this.#retriever.recycleLiveMails(); @@ -87,9 +92,8 @@ const nonServerCli = { body: text, }, ); - const logger = res.ok ? log : error; - logger(res); - logger("Body\n" + await res.text()); + log.infoOrError(!res.ok, res); + log.infoOrError(!res.ok, "Body\n" + await res.text()); if (!res.ok) Deno.exit(-1); }, } as const; @@ -104,14 +108,14 @@ if (import.meta.main) { const command = args._[0]; if (command in nonServerCli) { - log(`Run non-server command ${command}.`); + log.info(`Run non-server command ${command}.`); await nonServerCli[command as keyof typeof nonServerCli](args); Deno.exit(0); } const app = new AwsRelayApp(); if (command in app.cli) { - log(`Run command ${command}.`); + log.info(`Run command ${command}.`); await app.cli[command as keyof AwsRelayApp["cli"]](args); Deno.exit(0); } else { |