-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (61 loc) · 2.55 KB
/
Copy pathindex.js
File metadata and controls
68 lines (61 loc) · 2.55 KB
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
63
64
65
66
67
68
// Entry point: load config, credentials, warm up fingerprints, start server.
import config from './config.js';
import log from './logger.js';
import { CredentialPool } from './src/credPool.js';
import { warmup } from './src/fingerprint.js';
import { createServer } from './src/openaiServer.js';
import { init as initModels, getModelIds } from './src/modelProvider.js';
async function main() {
if (!config.AUTH_TOKEN) {
log.warn('[startup] AUTH_TOKEN not set — local authentication is DISABLED. Set AUTH_TOKEN to protect this endpoint.');
}
const credPool = new CredentialPool();
try {
credPool.load();
} catch (e) {
log.error(`[startup] ${e.message}`);
process.exit(1);
}
// Warm up fingerprints according to config.WARMUP_MODE:
// 'startup' -> fan out warmup for every enabled credential now (legacy).
// 'lazy' / 'lazy-async' -> warm per credential on first rotation
// (handled in openaiServer via ensureWarmed).
// 'off' -> no warmup at all.
switch (config.WARMUP_MODE) {
case 'lazy':
case 'lazy-async':
log.info(`[startup] warmup mode=${config.WARMUP_MODE} (on-demand at first rotation; ${config.WARMUP_MODE === 'lazy' ? 'blocking first request' : 'non-blocking'})`);
break;
case 'off':
log.info('[startup] warmup mode=off (no fingerprint warmup)');
break;
case 'startup':
default: {
const warmupCreds = credPool.creds.filter(c => c.enabled);
log.info(`[startup] warmup mode=startup, warming up ${warmupCreds.length} credential(s) in background...`);
Promise.allSettled(warmupCreds.map(c => warmup(c.token, c.name)))
.then(() => log.info('[startup] warmup complete'));
break;
}
}
// Fetch the upstream model list (best-effort; non-fatal on failure) and
// start the periodic background refresher.
await initModels();
const modelIds = getModelIds();
log.info(`[startup] models: ${modelIds.length ? modelIds.join(', ') : '(upstream fetch pending — passthrough active)'}`);
const server = createServer(credPool);
server.listen(config.PORT, config.HOST, () => {
log.info(`[startup] listening on ${config.HOST}:${config.PORT}`);
});
const shutdown = (sig) => {
log.info(`[shutdown] ${sig} received, closing server`);
server.close(() => process.exit(0));
setTimeout(() => process.exit(0), 3000).unref();
};
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('SIGTERM', () => shutdown('SIGTERM'));
}
main().catch(e => {
log.error(`[startup] fatal: ${e?.stack || e}`);
process.exit(1);
});