Luigit
repositories / will

will

owned by admin

src/service/config.ts

Raw
import { existsSync } from 'node:fs'
import { join } from 'node:path'
import { envInt, envOpt, envStr } from '../shared/env.ts'

export interface ServiceConfig {
  dataDir: string
  publicAddr: string
  internalAddr: string
  webDist: string
  docsDist: string
  mediaMaxBytes: number
  workerUrl: string
  /** Deployment configuration (spec AC13): free-space thresholds in percent. */
  storageWarnPct: number
  storageStopPct: number
}

export function resolveWorkerUrl(defaultDir: string): string {
  const override = envOpt('WILL_SERVICE_WORKER_URL')
  if (override) return override
  const ts = join(defaultDir, 'worker.ts')
  const js = join(defaultDir, 'service-worker.js')
  if (existsSync(js)) return js
  if (existsSync(ts)) return ts
  throw new Error(`no service worker found near ${defaultDir}`)
}

export function loadServiceConfig(defaults: { distRoot: string }): ServiceConfig {
  return {
    dataDir: envStr('WILL_SERVICE_DATA_DIR', '/srv/will-data'),
    publicAddr: envStr('WILL_SERVICE_PUBLIC_ADDR', '0.0.0.0:3000'),
    internalAddr: envStr('WILL_SERVICE_INTERNAL_ADDR', '127.0.0.1:3001'),
    webDist: envStr('WILL_SERVICE_WEB_DIST', join(defaults.distRoot, 'web-dist')),
    docsDist: envStr('WILL_SERVICE_DOCS_DIST', join(defaults.distRoot, 'docs-dist')),
    mediaMaxBytes: envInt('WILL_SERVICE_MEDIA_MAX_BYTES', 100 << 20),
    workerUrl: resolveWorkerUrl(defaults.distRoot),
    storageWarnPct: envInt('WILL_STORAGE_WARN_PCT', 20),
    storageStopPct: envInt('WILL_STORAGE_STOP_PCT', 10),
  }
}