-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathindex.ts
More file actions
72 lines (61 loc) · 2.01 KB
/
index.ts
File metadata and controls
72 lines (61 loc) · 2.01 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
69
70
71
72
// src/index.ts
import 'reflect-metadata';
import express from 'express';
import { HealthController } from './src/health/health.controller';
const app = express();
const port = process.env.PORT || 3000;
// Middleware
app.use(express.json());
// Health endpoints
const healthController = new HealthController();
app.get('/health', (req, res) => healthController.checkHealth(req, res));
app.get('/health/metrics', (req, res) => healthController.getMetrics(req, res));
app.get('/health/connections', (req, res) =>
healthController.getConnectionStats(req, res),
);
// Graceful shutdown
process.on('SIGTERM', gracefulShutdown);
process.on('SIGINT', gracefulShutdown);
async function gracefulShutdown(signal: string) {
console.log(`Received ${signal}. Starting graceful shutdown...`);
try {
const databaseService = DatabaseService.getInstance();
await databaseService.close();
console.log('Database connections closed');
process.exit(0);
} catch (error) {
console.error('Error during shutdown:', error);
process.exit(1);
}
}
async function startServer() {
try {
// Initialize database
const databaseService = DatabaseService.getInstance();
await databaseService.initialize();
// Start server
app.listen(port, () => {
console.log(`Server running on port ${port}`);
console.log(`Health check: http://localhost:${port}/health`);
console.log(`Metrics: http://localhost:${port}/health/metrics`);
});
} catch (error) {
console.error('Failed to start server:', error);
// Retry connection
try {
const databaseService = DatabaseService.getInstance();
await databaseService.retryConnection();
console.log('Database connection retry successful');
app.listen(port, () => {
console.log(`Server running on port ${port} (after retry)`);
});
} catch (retryError) {
console.error(
'Failed to establish database connection after retries:',
retryError,
);
process.exit(1);
}
}
}
startServer();