Spaces:
Sleeping
Sleeping
| import './env.js'; | |
| import { createApp } from './app.js'; | |
| import { initDb, getSetting } from './db/index.js'; | |
| import { startHealthChecker } from './services/health.js'; | |
| import { applyProxyUrl, applyProxyEnabled, applyProxyBypass } from './lib/proxy.js'; | |
| import { startCatalogSync } from './services/catalog-sync.js'; | |
| import { installProcessSafetyNet } from './lib/process-safety-net.js'; | |
| const PORT = process.env.PORT ?? 3001; | |
| // Dual-stack ('::') by default so the dashboard is reachable over both IPv4 | |
| // and IPv6 (e.g. IPv6-enabled Docker networks β #180). Hosts with IPv6 | |
| // disabled fall back to IPv4-only below; HOST overrides the default outright. | |
| const HOST = process.env.HOST ?? '::'; | |
| async function main() { | |
| // Install first so a late provider socket reset (undici HTTP/2 error with no | |
| // listener) can't take the proxy down. Genuine bugs still exit 1. | |
| installProcessSafetyNet(); | |
| initDb(); | |
| // Load the persisted proxy settings from the DB (env var wins if set). | |
| // Must happen after initDb so the settings table is ready. | |
| applyProxyUrl(getSetting('proxy_url') ?? ''); | |
| applyProxyEnabled(getSetting('proxy_enabled') !== '0'); // default: enabled | |
| applyProxyBypass(getSetting('proxy_bypass') ?? ''); | |
| const app = createApp(); | |
| const onReady = (host: string) => () => { | |
| const display = host.includes(':') ? `[${host}]` : host; | |
| console.log(`Server running on http://${display}:${PORT}`); | |
| console.log(`Proxy endpoint: http://${display}:${PORT}/v1/chat/completions`); | |
| startHealthChecker(); | |
| startCatalogSync(); | |
| }; | |
| const server = app.listen(Number(PORT), HOST, onReady(HOST)); | |
| server.on('error', (err: NodeJS.ErrnoException) => { | |
| // The default '::' bind fails where IPv6 is disabled (kernel | |
| // ipv6.disable=1 and the like) β retry IPv4-only rather than dying. | |
| // Anything else (EADDRINUSE, an explicit HOST that can't bind) keeps the | |
| // fail-fast posture documented in main().catch below. | |
| if (!process.env.HOST && (err.code === 'EAFNOSUPPORT' || err.code === 'EADDRNOTAVAIL')) { | |
| console.warn('[server] IPv6 unavailable on this host β falling back to 0.0.0.0 (IPv4-only)'); | |
| app.listen(Number(PORT), '0.0.0.0', onReady('0.0.0.0')); | |
| return; | |
| } | |
| console.error('\n[server] Failed to start:\n ' + (err?.message ?? err) + '\n'); | |
| process.exit(1); | |
| }); | |
| } | |
| main().catch((err) => { | |
| // A boot failure (e.g. a missing production ENCRYPTION_KEY) must exit | |
| // non-zero rather than leaving a half-initialized process that never starts | |
| // listening β that silent state is what surfaces in the client as | |
| // "Can't reach the server". | |
| console.error('\n[server] Failed to start:\n ' + (err?.message ?? err) + '\n'); | |
| process.exit(1); | |
| }); | |