diff --git a/src/webapp.ts b/src/webapp.ts index 27b5f16..5fc6653 100644 --- a/src/webapp.ts +++ b/src/webapp.ts @@ -1,8 +1,8 @@ import { type Context, Hono } from "hono" import type { Child } from "hono/jsx" import { renderToString } from "hono/jsx/dom/server" -import { join } from "node:path" -import { readdirSync } from "node:fs" +import { join } from "path" +import { readdirSync, watch } from "fs" import { NOSE_WWW } from "./config" import { isFile } from "./utils" @@ -70,22 +70,39 @@ export function toResponse(source: string | Child | Response): Response { }) } +// +// dns nonsense +// + +const dnsEntries: Record = {} + +const { stdout: ipRaw } = await Bun.$`hostname -I | awk '{print $1}'`.quiet() +const { stdout: hostRaw } = await Bun.$`hostname`.quiet() + +const ip = ipRaw.toString().trim() +const host = hostRaw.toString().trim() + export async function publishDNS() { - const { stdout: ipRaw } = await Bun.$`hostname -I | awk '{print $1}'`.quiet() - const { stdout: hostRaw } = await Bun.$`hostname`.quiet() - - const ip = ipRaw.toString().trim() - const host = hostRaw.toString().trim() - - const procs = apps().map(app => - Bun.spawn(["avahi-publish", "-a", `${app}.${host}.local`, "-R", ip]) - ) + apps().forEach(publishAppDNS) const signals = ["SIGINT", "SIGTERM"] signals.forEach(sig => process.on(sig, () => { - procs.forEach(p => p.kill("SIGTERM")) - process.exit(0) + for (const name in dnsEntries) + dnsEntries[name].kill("SIGTERM") }) ) -} \ No newline at end of file +} + +function publishAppDNS(app: string) { + if (process.env.BUN_HOT) return + + if (!dnsEntries[app]) + dnsEntries[app] = Bun.spawn(["avahi-publish", "-a", `${app}.${host}.local`, "-R", ip]) + + return dnsEntries[app] +} + +const appWatcher = watch(NOSE_WWW, (event, filename) => { + apps().forEach(publishAppDNS) +}) \ No newline at end of file