-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
72 lines (59 loc) · 2.39 KB
/
app.ts
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
import express from 'express';
import { microservicesProd, microservicesDev } from './config/microservices';
import { LoggingTool } from './src/services/loggingTool';
import addRoutesToApp from './src/services/AddRoutesToApp';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
let microservices: Object;
const environment = process.env.ENV || 'production';
if (environment === 'production') {
LoggingTool.info('Using Production Microservices');
microservices = microservicesProd;
} else {
LoggingTool.info('Using Development Microservices');
microservices = microservicesDev;
}
const retryDelay = 10 * 1000; // Retry every 15 seconds
const maxRetryAttempts = 15; // Maximum number of retry attempts
const startServer = async () => {
// Retry until all microservices are running and registered
let retryAttempts = 0;
let allMicroservicesRunning = false;
// Wait 30 seconds for all microservices to start
while (!allMicroservicesRunning && retryAttempts < maxRetryAttempts) {
try {
allMicroservicesRunning = true; // Assume all microservices are running
for (const [service, serviceURL] of Object.entries(microservices)) {
LoggingTool.info(`Retrieving ${service} Information from ${serviceURL}`);
const response = await fetch(serviceURL);
const data = await response.json();
if (data.status !== 'running') {
allMicroservicesRunning = false; // Set flag to false if any microservice is not running
LoggingTool.warning(`Service ${service} is not running yet`);
} else {
LoggingTool.success(`Service ${service} is running`);
await addRoutesToApp(app, service, serviceURL, data.routes);
}
}
} catch (error: any) {
allMicroservicesRunning = false; // Set flag to false if an error occurs
LoggingTool.error(error);
}
if (!allMicroservicesRunning) {
retryAttempts++;
LoggingTool.info(`Retry attempt ${retryAttempts}/${maxRetryAttempts}`);
await new Promise(resolve => setTimeout(resolve, retryDelay));
}
}
if (allMicroservicesRunning) {
// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
LoggingTool.info(`API Gateway listening at http://localhost:${port}`);
});
} else {
LoggingTool.error('Failed to start the server. Maximum number of retry attempts reached.');
}
};
startServer();