diff options
Diffstat (limited to 'services/docker/mail-server/relay/app.ts')
-rw-r--r-- | services/docker/mail-server/relay/app.ts | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/services/docker/mail-server/relay/app.ts b/services/docker/mail-server/relay/app.ts new file mode 100644 index 0000000..e5417d6 --- /dev/null +++ b/services/docker/mail-server/relay/app.ts @@ -0,0 +1,63 @@ +import { Hono } from "hono"; + +import { Logger, setLogger } from "./logger.ts"; +import { Config, setConfig } from "./config.ts"; +import { DbService } from "./db.ts"; +import { MailDeliverer } from "./mail.ts"; +import { DovecotMailDeliverer } from "./dovecot.ts"; +import { CronTask, CronTaskConfig } from "./cron.ts"; + +export abstract class AppBase { + protected readonly logger; + protected readonly config; + protected readonly db: DbService; + protected readonly inboundDeliverer: MailDeliverer; + protected readonly crons: CronTask[] = []; + protected readonly routes: Hono[] = []; + readonly cliCommands: Record<string, () => Promise<void>> = { + "serve": () => this.serve(), + }; + + protected abstract readonly outboundDeliverer: MailDeliverer; + + constructor() { + this.config = new Config(); + setConfig(this.config); + + const dataPath = this.config.getValue("dataPath"); + Deno.mkdirSync(dataPath, { recursive: true }); + + this.logger = new Logger(`${dataPath}/log`); + setLogger(this.logger); + + this.logger.log(this.config); + + this.db = new DbService(`${dataPath}/db.sqlite`); + this.inboundDeliverer = new DovecotMailDeliverer(); + + 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); + } + + createCron(config: CronTaskConfig): CronTask { + const cron = new CronTask(config); + this.crons.push(cron); + return cron; + } + + async serve(): Promise<void> { + const hono = new Hono(); + this.routes.forEach((h) => hono.route("/", h)); + + const server = Deno.serve({ + hostname: "0.0.0.0", + }, hono.fetch); + await server.finished; + } +} |