#!/usr/bin/env node // Writes docker/.env with fresh secrets for docker-compose.yml. Run once: // node docker/setup.mjs --url http://192.168.1.10:3000 // No Node on the server? docker run --rm -v "$PWD":/w -w /w node:22-alpine node docker/setup.mjs --url http://... import { createHmac, randomBytes } from "node:crypto"; import { existsSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; const file = join(dirname(fileURLToPath(import.meta.url)), ".env"); const arg = (name) => { const i = process.argv.indexOf(name); return i > 0 ? process.argv[i + 1] : undefined; }; const url = (arg("--url") ?? "").replace(/\/$/, ""); if (!/^https?:\/\/[^/]+$/.test(url)) { console.error("Give the address you will open broke on, with the port: node docker/setup.mjs --url http://192.168.1.10:3000"); process.exit(1); } // broke's server reaches Supabase through this address too, and inside a container localhost is the container itself. if (/^https?:\/\/(localhost|127\.|\[::1\]|0\.0\.0\.0)/.test(url)) { console.error("Use the server's LAN address or name (like http://192.168.1.10:3000), not localhost: the containers can't reach localhost."); process.exit(1); } if (existsSync(file) && !process.argv.includes("--force")) { console.error(`${file} already exists. Its secrets protect your data, so it is left alone. Pass --force to replace it (only before first start).`); process.exit(1); } const hex = (n) => randomBytes(n).toString("hex"); const b64url = (s) => Buffer.from(s).toString("base64url"); const jwtSecret = randomBytes(32).toString("base64url"); const jwt = (role) => { const now = Math.floor(Date.now() / 1000); const body = `${b64url(JSON.stringify({ alg: "HS256", typ: "JWT" }))}.${b64url(JSON.stringify({ role, iss: "supabase", iat: now, exp: now + 10 * 365 * 24 * 3600 }))}`; return `${body}.${createHmac("sha256", jwtSecret).update(body).digest("base64url")}`; }; const port = new URL(url).port || (url.startsWith("https") ? "443" : "80"); writeFileSync(file, `# Made by docker/setup.mjs. Keep it private and backed up: losing it means losing access to the database. # The address you open broke on. Built into the app: change it and rebuild (up -d --build). PUBLIC_URL=${url} HTTP_PORT=${port} # Where the database (and Ollama models) live. On Unraid, for example /mnt/user/appdata/broke DATA_DIR=./data POSTGRES_PASSWORD=${hex(24)} JWT_SECRET=${jwtSecret} ANON_KEY=${jwt("anon")} SERVICE_ROLE_KEY=${jwt("service_role")} # Sign-in emails. Mailpit's page is on MAIL_UI_PORT, user "broke". MAIL_UI_PORT=8025 MAIL_PASSWORD=${hex(12)} # Or a real mail server: # SMTP_HOST= # SMTP_PORT=587 # SMTP_USER= # SMTP_PASS= # SMTP_FROM= # After your first sign-in, set this to true and run up -d again. DISABLE_SIGNUP=false # Local AI (docker compose --profile ai). Empty model = the first one installed. LOCAL_LLM_BASE_URL=http://ollama:11434/v1 LOCAL_LLM_MODEL= # Optional, US/Canada banks only. CSV import needs none of this. # PLAID_CLIENT_ID= # PLAID_SECRET= # PLAID_ENV=sandbox `, { mode: 0o600 }); console.log(`Wrote ${file}\nNext: docker compose -f docker/docker-compose.yml up -d --build\nThen open ${url}`);