-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
212 lines (176 loc) · 7.49 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const express = require('express');
const fs = require('fs');
const path = require('path');
const toml = require('toml');
const smolToml = require('smol-toml');
const app = express();
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.get('/', async (req, res) => {
try {
const versionData = await fs.promises.readFile(path.join(__dirname, 'public/version.json'), 'utf8');
const version = JSON.parse(versionData).version;
const currentYear = new Date().getFullYear();
const config = await readConfig();
res.render('layout', {
title: 'BeamNG.cluster | Home',
version: version,
body: 'index',
year: currentYear
});
} catch (err) {
console.error('Error fetching data:', err);
res.status(500).send('Internal Server Error');
}
});
app.get('/servers', async (req, res) => {
try {
const versionData = await fs.promises.readFile(path.join(__dirname, 'public/version.json'), 'utf8');
const version = JSON.parse(versionData).version;
const currentYear = new Date().getFullYear();
const config = await readConfig();
res.render('layout', {
title: 'BeamNG.cluster | Servers',
version: version,
body: 'servers',
year: currentYear
});
} catch (err) {
console.error('Error fetching data:', err);
res.status(500).send('Internal Server Error');
}
});
app.get('/settings', async (req, res) => {
try {
const versionData = await fs.promises.readFile(path.join(__dirname, 'public/version.json'), 'utf8');
const version = JSON.parse(versionData).version;
const currentYear = new Date().getFullYear();
const config = await readConfig();
res.render('layout', {
title: 'BeamNG.cluster | Settings',
version: version,
body: 'settings',
serverPath: config.serverPath || '/servers/',
environment: config.environment || 'prod',
year: currentYear
});
} catch (err) {
console.error('Error fetching settings:', err);
res.status(500).send('Internal Server Error');
}
});
app.get('/about', async (req, res) => {
try {
const versionData = await fs.promises.readFile(path.join(__dirname, 'public/version.json'), 'utf8');
const version = JSON.parse(versionData).version;
const currentYear = new Date().getFullYear();
res.render('layout', {
title: 'BeamNG.cluster | About',
version: version,
body: 'about',
year: currentYear
});
} catch (err) {
console.error('Error fetching settings:', err);
res.status(500).send('Internal Server Error');
}
});
app.get('/api/servers', async (req, res) => {
try {
const config = await readConfig();
const serverPath = path.join(__dirname, config.serverPath);
const servers = await fs.promises.readdir(serverPath, { withFileTypes: true });
const serverData = await Promise.all(servers
.filter(dirent => dirent.isDirectory())
.map(async (dirent) => {
const serverDir = path.join(serverPath, dirent.name);
const serverConfigPath = path.join(serverDir, 'ServerConfig.toml');
try {
const serverConfig = await fs.promises.readFile(serverConfigPath, 'utf8');
const configData = toml.parse(serverConfig);
const mapPath = configData.General.Map || 'Unknown';
const mapNameMatch = mapPath.match(/^\/levels\/([^\/]+)\//);
const mapName = mapNameMatch ? mapNameMatch[1] : 'Unknown';
const country = configData.General.Country || 'Unknown';
return {
name: configData.General.Name || 'Unknown',
port: configData.General.Port || 'Unknown',
authKey: configData.General.AuthKey || 'Unknown', // sensitive data
maxPlayers: configData.General.MaxPlayers || 'Unknown',
map: mapName,
country: country,
path: serverDir
};
} catch (err) {
console.error(`Error reading server config for ${dirent.name}:`, err);
return null;
}
}));
const filteredServers = serverData.filter(server => server !== null);
res.json(filteredServers);
} catch (err) {
console.error('Error fetching servers:', err);
res.status(500).json({ error: 'Error fetching servers' });
}
});
// Work in progress - Currently not passing path properly on post
app.post('/api/save-servers', async (req, res) => {
try {
const serverData = req.body;
console.log('serverData:', serverData);
if (!Array.isArray(serverData)) {
throw new Error('Expected serverData to be an array.');
}
await Promise.all(serverData.map(async (server) => {
const serverConfigPath = path.join(server.path, 'ServerConfig.toml');
console.log('Saving server config to path:', serverConfigPath);
try {
const existingConfig = await fs.promises.readFile(serverConfigPath, 'utf8');
const configData = toml.parse(existingConfig);
// Update configData with new values
configData.General.Name = server.name;
configData.General.Port = server.port;
configData.General.AuthKey = server.authKey;
configData.General.MaxPlayers = server.maxPlayers;
configData.General.Map = `/levels/${server.map}`;
configData.General.Country = server.country.trim();
const updatedConfig = smolToml.stringify(configData);
await fs.promises.writeFile(serverConfigPath, updatedConfig, 'utf8');
} catch (err) {
console.error(`Error updating server config for ${server.name}:`, err);
throw new Error(`Failed to update config for ${server.name}`);
}
}));
res.json({ success: true });
} catch (err) {
console.error('Error saving servers:', err);
res.status(500).json({ success: false, error: err.message });
}
});
app.post('/update-settings', async (req, res) => {
try {
const configPath = path.join(__dirname, 'config.json');
const config = JSON.parse(await fs.promises.readFile(configPath, 'utf8'));
console.log('Form data:', req.body);
config.serverPath = req.body.serverPath || config.serverPath;
await fs.promises.writeFile(configPath, JSON.stringify(config, null, 2), 'utf8');
res.redirect('/settings');
} catch (err) {
console.error('Error updating settings:', err);
res.status(500).send('Internal Server Error');
}
});
const readConfig = () => {
return fs.promises.readFile(path.join(__dirname, 'config.json'), 'utf8')
.then(data => JSON.parse(data))
.catch(err => {
console.error('Error reading config file:', err);
throw err;
});
};
app.listen(8000, () => {
console.log('Starting development server..');
console.log('Server is running on http://localhost:8000');
});