-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
85 lines (70 loc) · 2.12 KB
/
app.js
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
73
74
75
76
77
78
79
80
81
82
83
84
const express = require('express');
const axios = require('axios');
const os = require('os');
const fs = require('fs');
const config = require('./config.json'); // Import configuration
const app = express();
const port = 3000;
const productsApiBaseUri = config.productsApiBaseUri;
app.set('view engine', 'ejs');
app.use(express.static('public'));
// Static Middleware
app.use('/static', express.static('public'));
// Endpoint to serve product data to client
app.get('/api/products', async (req, res) => {
try {
let response = await axios.get(`${productsApiBaseUri}/api/products`);
res.json(response.data);
} catch (error) {
console.error('Error fetching products:', error);
res.status(500).send('Error fetching products');
}
});
app.get('/', (req, res) => {
// Gather system info
const systemInfo = {
hostname: os.hostname(),
ipAddress: getIPAddress(),
isContainer: isContainer(),
isKubernetes: fs.existsSync('/var/run/secrets/kubernetes.io')
// ... any additional system info here
};
res.render('index', {
systemInfo: systemInfo
});
});
function getIPAddress() {
// Logic to fetch IP Address
const networkInterfaces = os.networkInterfaces();
return (networkInterfaces['eth0'] && networkInterfaces['eth0'][0].address) || 'IP not found';
}
function isContainer() {
// Logic to check if running in a container
try {
fs.readFileSync('/proc/1/cgroup');
return true;
} catch (e) {
return false;
}
}
app.get('/api/service-status', async (req, res) => {
try {
// Example of checking the status of the products service
const productServiceResponse = await axios.get(`${productsApiBaseUri}/api/products`);
// Additional checks for more services can be added similarly
// If code execution reaches here, the service(s) are up
res.json({
productService: 'up',
// otherService: 'up' or 'down'
});
} catch (error) {
console.error('Error:', error);
res.json({
productService: 'down',
// otherService: 'up' or 'down'
});
}
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});