index.ts
1,826 bytes
| 1 | import { existsSync, mkdirSync, readFileSync } from 'node:fs' |
|---|---|
| 2 | import path from 'node:path' |
| 3 | import { buildApp } from './app' |
| 4 | import { configStatus } from './config/runtimeConfig' |
| 5 | import { applyDemoMode, isPublicDemo } from './runtimeMode' |
| 6 | |
| 7 | // .env wins over inherited shell values, so a stale MOCK_PROVIDERS=1 in some |
| 8 | // old terminal cannot silently force mock mode. |
| 9 | if (existsSync('.env')) { |
| 10 | for (const line of readFileSync('.env', 'utf8').split(/\r?\n/)) { |
| 11 | const match = line.match(/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$/) |
| 12 | if (!match) continue |
| 13 | const value = match[2].replace(/^(['"])(.*)\1$/, '$2') |
| 14 | if (value === '') delete process.env[match[1]] |
| 15 | else process.env[match[1]] = value |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | const demoLocked = applyDemoMode(process.argv, process.env) |
| 20 | |
| 21 | const PORT = Number(process.env.PORT ?? 3001) |
| 22 | |
| 23 | function startupMode(): string { |
| 24 | const status = configStatus(demoLocked) |
| 25 | if (status.mode === 'demo') return 'MOCK providers (offline demo)' |
| 26 | if (status.mode === 'real') return 'real providers' |
| 27 | return 'no provider keys yet: add them on the VoiceTask home screen, or pick the offline demo there' |
| 28 | } |
| 29 | |
| 30 | async function main() { |
| 31 | const publicDemo = isPublicDemo(process.env) |
| 32 | let sandboxRoot: string | undefined |
| 33 | if (publicDemo) { |
| 34 | sandboxRoot = process.env.VOICETASK_SANDBOX ?? path.join(process.cwd(), 'data', 'public-sessions') |
| 35 | mkdirSync(sandboxRoot, { recursive: true }) |
| 36 | } |
| 37 | const clientDir = process.env.VOICETASK_CLIENT_DIR |
| 38 | |
| 39 | const app = buildApp({ demoLocked, publicDemo, sandboxRoot, clientDir }) |
| 40 | await app.listen({ port: PORT, host: '0.0.0.0' }) |
| 41 | const shape = publicDemo ? 'public demo (sandboxed, read-only settings)' : startupMode() |
| 42 | console.log(`VoiceTask server listening on :${PORT} using ${shape}`) |
| 43 | } |
| 44 | |
| 45 | main().catch((err) => { |
| 46 | console.error(err) |
| 47 | process.exit(1) |
| 48 | }) |
| 49 | |