-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
102 lines (90 loc) · 2.44 KB
/
server.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const fs = require('fs').promises;
const express = require('express');
const ejs = require('ejs');
const path = require('path');
const moment = require('moment-timezone');
const chalk = require('chalk');
const markdownIt = require('markdown-it')();
const statusMonitor = require('express-status-monitor');
const appstateHandler = require('./src/api/fbstateApi');
const cookiesExtractorHandler = require('./src/api/cookiesExtractor');
const app = express();
const appPort = process.env.APP_PORT || 6057;
app.use(express.json());
app.use(
statusMonitor({
title: 'System Status',
path: '/status',
spans: [
{ interval: 1, retention: 60 },
{ interval: 5, retention: 60 },
{ interval: 15, retention: 60 },
],
chartVisibility: {
cpu: true,
mem: true,
load: true,
eventLoop: true,
heap: true,
responseTime: true,
rps: true,
statusCodes: true,
},
})
);
app.get('/', async (req, res) => {
const html = await ejs.renderFile(
path.join(__dirname, 'views', 'index.ejs'),
{}
);
res.send(html);
});
app.get('/README.md', async (req, res) => {
try {
const readmeContent = await fs.readFile(
path.join(__dirname, 'docs', 'README.md'),
'utf8'
);
const htmlContent = markdownIt.render(readmeContent);
res.type('text/html').send(htmlContent);
} catch (err) {
res.status(404).send('README.md not found');
}
});
app.get('/api/appstate', appstateHandler);
app.post('/api/extract-cookies', cookiesExtractorHandler);
app.get('/health', (req, res) => {
const uptime = moment.duration(process.uptime(), 'seconds').humanize();
const timestamp = new Date().toLocaleString();
res.json({
status: 'ok',
uptime: uptime,
message: 'Server is running smoothly',
timestamp: timestamp,
});
});
moment.tz('Asia/Manila').format('YYYY-MM-DD HH:mm:ss');
const findAvailablePort = (port) => {
return new Promise((resolve, reject) => {
const server = app.listen(port, () => {
server.close();
resolve(port);
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
resolve(findAvailablePort(port + 1));
} else {
reject(err);
}
});
});
};
findAvailablePort(appPort)
.then((availablePort) => {
app.listen(availablePort, () => {
console.log(`Server is running on port ${availablePort}`);
});
})
.catch((err) => {
console.error('Failed to start server:', err);
});