From 9aee30f0d1da047df749e5dfbefa246974c82789 Mon Sep 17 00:00:00 2001 From: Daniel McCrystal Date: Sat, 29 Nov 2025 21:01:14 -0500 Subject: [PATCH] smarter caddy --- .vscode/settings.json | 3 +++ package.json | 6 +++--- src/buildSelf.ts | 13 ------------- src/caddy.ts | 23 +++++++++++++++++++++++ src/deploy.ts | 3 +++ src/{ => scripts}/deploySelf.ts | 2 +- src/scripts/reloadCaddy.ts | 3 +++ 7 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 .vscode/settings.json delete mode 100644 src/buildSelf.ts create mode 100644 src/caddy.ts rename src/{ => scripts}/deploySelf.ts (61%) create mode 100644 src/scripts/reloadCaddy.ts diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9cdf9d2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "workbench.colorTheme": "Userscape Contrast (rainglow)" +} diff --git a/package.json b/package.json index 89c5188..a719a44 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "deployer2", "scripts": { - "build": "bun run src/buildSelf.ts", + "deploy-self": "bun run src/scripts/deploySelf.ts", "dev": "NODE_ENV=development PORT=6001 bun run --hot src/index.ts", - "start": "NODE_ENV=production bun run src/index.ts", - "deploy-self": "bun run src/deploySelf.ts" + "build": "bun run src/scripts/reloadCaddy.ts", + "start": "NODE_ENV=production bun run src/index.ts" }, "dependencies": { "@types/object-hash": "^3.0.6", diff --git a/src/buildSelf.ts b/src/buildSelf.ts deleted file mode 100644 index 8b99ddd..0000000 --- a/src/buildSelf.ts +++ /dev/null @@ -1,13 +0,0 @@ -import fs from "fs"; -import { indexedConfig } from "./config"; - -const Caddyfile = Object.values(indexedConfig) - .map( - ({ host, port }) => ` -${host}.drm.dev { - reverse_proxy localhost:${port} -}` - ) - .join("\n"); - -fs.writeFileSync("Caddyfile", Caddyfile); diff --git a/src/caddy.ts b/src/caddy.ts new file mode 100644 index 0000000..5961709 --- /dev/null +++ b/src/caddy.ts @@ -0,0 +1,23 @@ +import fs from "fs"; +import { indexedConfig, config } from "./config"; +import path from "path"; + +export const reloadCaddyfile = () => { + const caddyfilePath = (host: string) => + path.join(config.directory, host, "src", "Caddyfile"); + + const Caddyfile = Object.values(indexedConfig) + .map( + ({ host, port }) => ` +${host}.drm.dev { + ${ + fs.existsSync(caddyfilePath(host)) + ? `import ${caddyfilePath(host)} ` // <-- space at end important + : `reverse_proxy localhost:` + }${port} +}` + ) + .join("\n"); + + fs.writeFileSync("Caddyfile", Caddyfile); +}; diff --git a/src/deploy.ts b/src/deploy.ts index 02cc269..db0fc8b 100644 --- a/src/deploy.ts +++ b/src/deploy.ts @@ -2,6 +2,8 @@ import hash from "object-hash"; import { $ } from "bun"; import path from "path"; import { config, indexedConfig } from "./config"; +import fs from "fs"; +import { reloadCaddyfile } from "./caddy"; type DeployInstance = { host: string; @@ -31,6 +33,7 @@ export const deploy = async ({ host, commitHash }: DeployInstance) => { // Build await $`make build`; + reloadCaddyfile(); // Register service const systemdServiceName = `deployer2-${host}.service`; diff --git a/src/deploySelf.ts b/src/scripts/deploySelf.ts similarity index 61% rename from src/deploySelf.ts rename to src/scripts/deploySelf.ts index dd7d0ac..30222b0 100644 --- a/src/deploySelf.ts +++ b/src/scripts/deploySelf.ts @@ -1,4 +1,4 @@ -import { deploy } from "./deploy"; +import { deploy } from "../deploy"; await deploy({ host: "deployer2", diff --git a/src/scripts/reloadCaddy.ts b/src/scripts/reloadCaddy.ts new file mode 100644 index 0000000..eb869ca --- /dev/null +++ b/src/scripts/reloadCaddy.ts @@ -0,0 +1,3 @@ +import { reloadCaddyfile } from "../caddy"; + +reloadCaddyfile();