-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
72 lines (60 loc) · 1.92 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
require('dotenv').config();
const { routes } = require('./src/routes');
const fs = require('fs');
const path = require('path');
const http = require('http');
// Retrieve the port from environment variables or default to 3000
const PORT = process.env.PORT || 3000;
// Load the private key from a file and set it in process.env
const loadPrivateKey = () => {
const privateKeyPath = path.join(__dirname, 'private.key'); // Adjust the path as needed
try {
const privateKey = fs.readFileSync(privateKeyPath, 'utf8');
process.env.PRIVATE_KEY = privateKey;
} catch (error) {
console.error(`Failed to load private key from ${privateKeyPath}:`, error);
process.exit(1); // Exit the application if the private key can't be loaded
}
};
let server;
/**
* Create and start the HTTP server.
*/
const startServer = () => {
server = http.createServer((req, res) => {
const { method, url } = req;
// Check if there's a handler for the current method and URL
const handler = routes[url] && routes[url][method];
if (handler) {
// If a handler is found, execute it
handler(req, res);
} else {
// Otherwise, send a 404 Not Found response
res.statusCode = 404;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ error: 'Not Found' }));
}
});
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
};
/**
* Restart the HTTP server.
*/
const restartServer = () => {
server.close(() => {
console.log('Server is restarting...');
startServer();
});
};
/**
* Handle uncaught exceptions by logging the error and restarting the server.
*/
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err);
restartServer();
});
// Start the server initially
loadPrivateKey();
startServer();