aboutsummaryrefslogtreecommitdiff
path: root/services/docker/mail-server/relay/app.ts
diff options
context:
space:
mode:
Diffstat (limited to 'services/docker/mail-server/relay/app.ts')
-rw-r--r--services/docker/mail-server/relay/app.ts80
1 files changed, 58 insertions, 22 deletions
diff --git a/services/docker/mail-server/relay/app.ts b/services/docker/mail-server/relay/app.ts
index 83d7c4d..3a85996 100644
--- a/services/docker/mail-server/relay/app.ts
+++ b/services/docker/mail-server/relay/app.ts
@@ -1,42 +1,78 @@
+import { join } from "@std/path";
import { Hono } from "hono";
+import { logger as honoLogger } from "hono/logger";
-import { Logger, setLogger } from "./logger.ts";
-import { Config, getConfig, setConfig } from "./config.ts";
+import logger, { error } from "./logger.ts";
+import config from "./config.ts";
import { DbService } from "./db.ts";
-import { MailDeliverer } from "./mail.ts";
+import {
+ AliasFileMailHook,
+ FallbackReceiptsHook,
+ MailDeliverer,
+} from "./mail.ts";
import { DovecotMailDeliverer } from "./dovecot.ts";
+import { CronTask, CronTaskConfig } from "./cron.ts";
+import { DumbSMTPServer as DumbSmtpServer } from "./dumb-smtp-server.ts";
export abstract class AppBase {
protected readonly db: DbService;
- protected readonly inboundDeliverer: MailDeliverer;
+ protected readonly crons: CronTask[] = [];
protected readonly routes: Hono[] = [];
+ protected readonly inboundDeliverer: MailDeliverer;
+ protected readonly hono = new Hono();
protected abstract readonly outboundDeliverer: MailDeliverer;
constructor() {
- setLogger(new Logger("log"));
- setConfig(new Config());
+ const dataPath = config.getValue("dataPath");
+ Deno.mkdirSync(dataPath, { recursive: true });
+ logger.path = join(dataPath, "log");
+ logger.log(config);
- this.db = new DbService(`${getConfig().getValue("dataPath")}/db.sqlite`);
+ this.db = new DbService(join(dataPath, "db.sqlite"));
this.inboundDeliverer = new DovecotMailDeliverer();
+ this.inboundDeliverer.preHooks.push(
+ new FallbackReceiptsHook(new Set(config.getValueList("inboundFallback"))),
+ new AliasFileMailHook(join(dataPath, "aliases.csv")),
+ );
- const hono = new Hono()
- .post("/send/raw", async (context) => {
- this.outboundDeliverer.deliverRaw(await context.req.text());
- })
- .post("/receive/raw", async (context) => {
- this.inboundDeliverer.deliverRaw(await context.req.text());
- });
- this.routes.push(hono);
+ this.hono.onError((err, c) => {
+ error(err);
+ return c.json({ msg: "Server error." }, 500);
+ });
+
+ this.hono.use(honoLogger());
+ this.hono.post("/send/raw", async (context) => {
+ const body = await context.req.text();
+ if (body.trim().length === 0) {
+ return context.json({ msg: "Can't send an empty mail" }, 400);
+ } else {
+ const mail = await this.outboundDeliverer.deliverRaw(body);
+ return context.json({
+ messageId: mail.messageId,
+ awsMessageId: mail.awsMessageId,
+ });
+ }
+ });
+ this.hono.post("/receive/raw", async (context) => {
+ await this.inboundDeliverer.deliverRaw(await context.req.text());
+ return context.json({});
+ });
}
- async run(): Promise<void> {
- const hono = new Hono();
- this.routes.forEach((h) => hono.route("/", h));
+ createCron(config: CronTaskConfig): CronTask {
+ const cron = new CronTask(config);
+ this.crons.push(cron);
+ return cron;
+ }
- const server = Deno.serve({
- hostname: "0.0.0.0",
- }, hono.fetch);
- await server.finished;
+ serve(): { smtp: DumbSmtpServer; http: Deno.HttpServer } {
+ const smtp = new DumbSmtpServer(this.outboundDeliverer);
+ smtp.serve();
+ const http = Deno.serve({
+ hostname: config.HTTP_HOST,
+ port: config.HTTP_PORT,
+ }, this.hono.fetch);
+ return { smtp, http };
}
}