-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (53 loc) · 1.93 KB
/
Copy pathindex.js
File metadata and controls
65 lines (53 loc) · 1.93 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
import express from 'express';
import { SERVER_CONFIG, validateConfig, logConfigStatus } from './src/config/index.js';
import { initMongo } from './src/database/connection.js';
import routes from './src/routes/index.js';
import { errorHandler, requestLogger } from './src/middleware/index.js';
// Initialize Express app
const app = express();
// Add middleware to parse incoming POST and JSON data
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(requestLogger);
// Use routes
app.use(routes);
// Error handling middleware (must be last)
app.use(errorHandler);
// Start server
async function startServer() {
console.log(`=== SERVER STARTING ===`);
console.log(`Port: ${SERVER_CONFIG.port}`);
console.log(`Base URL: ${SERVER_CONFIG.baseUrl}`);
// Validate configuration
if (!validateConfig()) {
console.error('❌ CRITICAL: Required environment variables are missing! Server will not start.');
process.exit(1);
}
logConfigStatus();
try {
await initMongo();
console.log('✅ Database initialization completed');
} catch (error) {
console.error('❌ Database initialization failed:', error);
}
app.listen(SERVER_CONFIG.port, "0.0.0.0", () => {
console.log(`✅ Server running successfully on port ${SERVER_CONFIG.port}`);
console.log(`Dashboard available at: ${SERVER_CONFIG.baseUrl}/dashboard`);
console.log(`Health check at: ${SERVER_CONFIG.baseUrl}/health`);
console.log(`=== SERVER READY ===`);
});
}
// Handle graceful shutdown
process.on('SIGTERM', () => {
console.log('Received SIGTERM, shutting down gracefully...');
process.exit(0);
});
process.on('SIGINT', () => {
console.log('Received SIGINT, shutting down gracefully...');
process.exit(0);
});
// Start the server
startServer().catch(error => {
console.error('Failed to start server:', error);
process.exit(1);
});