Spaces:
Sleeping
Sleeping
File size: 2,740 Bytes
ed57015 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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);
});
|