-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathmain.js
More file actions
50 lines (42 loc) · 1.55 KB
/
main.js
File metadata and controls
50 lines (42 loc) · 1.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
/**
* File: main.js
* Description: Main entry file that initializes and starts the AIStudio To API proxy server system
*
* Author: Ellinav, iBenzene, bbbugg
*/
// Load environment variables based on NODE_ENV
const path = require("path");
const envFile = process.env.NODE_ENV === "production" ? ".env" : ".env.development";
require("dotenv").config({ path: path.resolve(__dirname, envFile) });
const ProxyServerSystem = require("./src/core/ProxyServerSystem");
/**
* Initialize and start the server
*/
const initializeServer = async () => {
const initialAuthIndex = parseInt(process.env.INITIAL_AUTH_INDEX, 10) || null;
try {
const serverSystem = new ProxyServerSystem();
await serverSystem.start(initialAuthIndex);
// Handle graceful shutdown
const shutdownHandler = async signal => {
console.log(`\n${signal} received, shutting down gracefully...`);
try {
await serverSystem.shutdown();
process.exit(0);
} catch (error) {
console.error("Error during shutdown:", error);
process.exit(1);
}
};
process.on("SIGTERM", () => shutdownHandler("SIGTERM"));
process.on("SIGINT", () => shutdownHandler("SIGINT"));
} catch (error) {
console.error("❌ Server startup failed:", error.message);
process.exit(1);
}
};
// If this file is run directly, start the server
if (require.main === module) {
initializeServer();
}
module.exports = { initializeServer, ProxyServerSystem };