Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
103 lines (81 loc) · 2.53 KB
/
Copy pathmain.js
File metadata and controls
103 lines (81 loc) · 2.53 KB
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
const express = require('express');
const path = require('path');
const { spawn } = require('child_process');
const fs = require('fs');
let serverProcess;
// Path to the NestJS server executable
const getServerPath = () => {
return path.join(__dirname, 'dist', 'apps', 'hotwallet', 'main.js');
};
// Path to the frontend build directory
const getFrontendPath = () => {
return path.join(__dirname, 'webapp', 'dist');
};
// Create and setup the Express server to serve the frontend
function startFrontEnd() {
const frontendServer = express();
const frontendPort = process.env.FRONTEND_PORT || 4000;
// Serve static files from the frontend build directory
frontendServer.use(express.static(getFrontendPath()));
// For any other route, serve the index.html (for SPA routing)
frontendServer.get('*', (req, res) => {
res.sendFile(path.join(getFrontendPath(), 'index.html'));
});
// Start the frontend server
frontendServer.listen(frontendPort, () => {
console.log(`Frontend server running on http://localhost:${frontendPort}`);
});
return frontendPort;
}
// Start the NestJS server
function startServer() {
const serverPath = getServerPath();
// Check if the server executable exists
if (!fs.existsSync(serverPath)) {
console.error(`Server executable not found at: ${serverPath}`);
process.exit(1);
}
console.log(`Starting server from: ${serverPath}`);
// Spawn the server process
serverProcess = spawn('node', [serverPath], {
env: {
...process.env,
NODE_ENV: process.env.NODE_ENV || 'production'
},
stdio: 'pipe'
});
// Log server output
serverProcess.stdout.on('data', (data) => {
console.log(`Server: ${data}`);
});
serverProcess.stderr.on('data', (data) => {
console.error(`Server Error: ${data}`);
});
serverProcess.on('close', (code) => {
console.log(`Server process exited with code ${code}`);
process.exit(code);
});
return serverProcess;
}
// Handle process termination
process.on('SIGINT', () => {
console.log('Received SIGINT. Graceful shutdown...');
if (serverProcess) {
serverProcess.kill();
}
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('Received SIGTERM. Graceful shutdown...');
if (serverProcess) {
serverProcess.kill();
}
process.exit(0);
});
// Start the application
console.log('Starting Runepool DEX server...');
// Start the NestJS server first
startServer();
// Setup Express server to serve the frontend
startFrontEnd();
console.log('Runepool DEX server is running');