diff --git a/package.json b/package.json index e8647d6..6e7bec5 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,7 @@ "remote:install": "./scripts/remote-install.sh", "remote:start": "./scripts/remote-start.sh", "remote:stop": "./scripts/remote-stop.sh", - "remote:restart": "./scripts/remote-restart.sh", - "generate-hosts": "./scripts/generate-hosts.sh" + "remote:restart": "./scripts/remote-restart.sh" }, "alias": { "@utils": "./src/utils.tsx", diff --git a/scripts/generate-hosts.ts b/scripts/generate-hosts.ts deleted file mode 100644 index 40bd7e9..0000000 --- a/scripts/generate-hosts.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { $ } from "bun" -import { apps } from "../src/webapp" - -const { stdout: ipRaw } = await $`hostname -I | awk '{print $1}'`.quiet() -const { stdout: hostRaw } = await $`hostname`.quiet() - -const ip = ipRaw.toString().trim() -const host = hostRaw.toString().trim() - -console.log(apps().map(app => `avahi-publish -a ${app}.${host}.local -R ${ip}`).join("\n")) - -const procs = apps().map(app => - Bun.spawn(["avahi-publish", "-a", `${app}.${host}.local`, "-R", ip]) -) - -for (const p of procs) - p.exited.then(code => - console.log(`avahi-publish (${p.pid}) exited with code ${code}`) - ) - - -await new Promise(() => { }) \ No newline at end of file diff --git a/src/server.tsx b/src/server.tsx index 1331022..992fa29 100644 --- a/src/server.tsx +++ b/src/server.tsx @@ -5,7 +5,7 @@ import color from "kleur" import { NOSE_ICON, NOSE_DIR, NOSE_BIN, NOSE_WWW } from "./config" import { transpile, isFile, tilde } from "./utils" -import { apps, serveApp } from "./webapp" +import { apps, serveApp, publishDNS } from "./webapp" import { Layout } from "./components/layout" import { Terminal } from "./components/terminal" @@ -97,6 +97,8 @@ if (process.env.BUN_HOT) { process.exit() }) } +} else { + publishDNS() } diff --git a/src/webapp.ts b/src/webapp.ts index 3b6dc1b..27b5f16 100644 --- a/src/webapp.ts +++ b/src/webapp.ts @@ -68,4 +68,24 @@ export function toResponse(source: string | Child | Response): Response { "Content-Type": "text/html; charset=utf-8" } }) +} + +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]) + ) + + const signals = ["SIGINT", "SIGTERM"] + signals.forEach(sig => + process.on(sig, () => { + procs.forEach(p => p.kill("SIGTERM")) + process.exit(0) + }) + ) } \ No newline at end of file