-
Notifications
You must be signed in to change notification settings - Fork 585
/
serverCore.js
105 lines (90 loc) · 4.55 KB
/
serverCore.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
103
104
105
const express = require("express");
const path = require("path");
const APICore = require("./apiCore");
const Utils = require("./utils");
const ServerCore = {
latestOutput: null,
/**
* Initializes the server with the given configuration.
* Sets up the Express application, serves static files, sets up API routes,
* schedules simulation generation, and starts the server.
*
* @param {Object} synthBTC - The main synthBTC object containing core functionalities.
* @param {Object} config - The configuration object.
* @param {Object} config.simulationConfig - The simulation configuration.
* @param {Object} config.webConfig - The web server configuration.
*/
init: function(synthBTC, { simulationConfig, webConfig }) {
const app = express();
let port = webConfig.serverPort;
// Serve static files from the clientPublicDir directory
app.use("/assets", express.static(path.join(__dirname, "..", synthBTC.clientPublicDir)));
// Serve the home page
app.get(webConfig.homeEndpoint, async (req, res) => {
try {
res.sendFile(path.join(__dirname, "..", synthBTC.clientPublicDir, webConfig.htmlFilePath));
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Setup API routes
const api = APICore(synthBTC);
api.setupRoutes(app);
// Schedule simulation generation based on the interval in minutes
const defineIntervalConfig = simulationConfig.simulationInterval;
setInterval(async () => {
this.latestOutput = await synthBTC.getSimulationData(simulationConfig);
}, defineIntervalConfig * 60 * 1000);
/**
* Starts the server on the given port.
* If the port is already in use, it increments the port number and tries again.
*
* @param {number} port - The port number to start the server on.
*/
function runServer(port) {
const server = app.listen(port);
server.on("error", (error) => {
if (error.code === "EADDRINUSE") {
runServer(port + 1);
} else {
console.error(`\x1b[0m- \x1b[31mERROR\x1b[0m | \x1b[37mServer error: ${error.message}\x1b[0m`);
process.exit(1);
}
});
server.on("listening", async () => {
console.clear();
const steps = [
{ message: "Initializing synthBTC", duration: 250 },
{ message: "Loading configuration", duration: 1000 },
{ message: "Setting up API routes", duration: 1200 },
{ message: "Preparing Simulation Engine", duration: 1800 },
{ message: "Starting Server", duration: 4000 }
];
// Display loading animation for each step
for (const step of steps) {
await Utils.animateLoading(step.message, step.duration);
}
setTimeout(async () => {
console.clear();
console.log("\x1b[32m%s\x1b[0m", "🟢 synthBTC Server is now running!");
console.log("\x1b[37m%s\x1b[0m", "----------------------------------------");
console.log("\x1b[36m%s\x1b[0m", `🏠 Home (Endpoint): \x1b[4mhttp://localhost:${port}${webConfig.homeEndpoint}\x1b[0m`);
console.log("\x1b[36m%s\x1b[0m", `🌐 API (Endpoint): \x1b[4mhttp://localhost:${port}/api\x1b[0m`);
console.log("\x1b[37m%s\x1b[0m", "----------------------------------------");
console.log("\x1b[32m%s\x1b[0m", "Welcome to synthBTC!");
console.log("\x1b[37m%s\x1b[0m", "Enjoy your simulation experience.");
console.log("\x1b[37m%s\x1b[0m", "---------------------------");
console.log("\x1b[37m%s\x1b[0m", "2024 Created by Jose Pino ©");
console.log("\x1b[37m%s\x1b[0m", "---------------------------");
// Generate initial simulations if not already generated
if (!this.latestOutput) {
this.latestOutput = await synthBTC.getSimulationData(simulationConfig);
}
}, 6000);
});
}
// Start the server with the initial port
runServer(port);
}
};
module.exports = ServerCore;