import { mkdir } from "node:fs/promises"; import { dirname } from "node:path"; import { BufferedAnalyticsSink } from "./analytics"; import { RegistryApi } from "./api"; import { createApplication } from "./app"; import { PlatformAuth } from "./auth"; import { readConfig } from "./config"; import { clientIp } from "./origins"; import { SlingshotReader } from "./records"; import { SqliteStore } from "./store"; const config = readConfig(); const auth = new PlatformAuth(config.credentials); const reader = new SlingshotReader(config.slingshotUrl); await mkdir(dirname(config.databasePath), { recursive: true }); const store = new SqliteStore(config.databasePath); const originPolicy = { baseUrl: config.baseUrl }; const api = new RegistryApi(store, reader, auth, originPolicy); const analytics = config.analyticsSecret ? new BufferedAnalyticsSink(store, config.retentionDays) : undefined; analytics?.start(); const application = createApplication(api, { store, originPolicy, analytics, analyticsSecret: config.analyticsSecret, maxAgeMs: config.maxAgeMs, }); let reconciling: Promise | undefined; function reconcile(): void { if (reconciling) return; reconciling = api.reconcile(config.freshnessMs) .catch(() => console.error("registry reconciliation failed")) .finally(() => { reconciling = undefined; }); } reconcile(); const reconciliationTimer = setInterval(reconcile, 30_000); reconciliationTimer.unref(); const server = Bun.serve({ port: config.port, hostname: config.hostname, idleTimeout: 15, maxRequestBodySize: 16_384, fetch(request, server) { const ip = clientIp(request, server.requestIP(request)?.address, config.trustedProxies); return application(request, ip); }, }); let stopping = false; async function shutdown(): Promise { if (stopping) return; stopping = true; clearInterval(reconciliationTimer); await server.stop(); await reconciling; analytics?.close(); store.close(); } process.on("SIGTERM", () => { void shutdown(); }); process.on("SIGINT", () => { void shutdown(); }); console.log(`pod.place registry and resolver listening on ${server.url}`);