Skip to content

Commit 8ac379d

Browse files
authored
chore: Merge pull request #5 from DEVUCP/api
Update api
2 parents 401b3d2 + 50e4687 commit 8ac379d

32 files changed

Lines changed: 919 additions & 726 deletions

api/app.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const express = require('express');
2+
const cors = require('cors');
3+
const { limiter } = require('./middleware/limiter.middleware');
4+
5+
const app = express();
6+
7+
const adminRoutes = require('./routes/admin.routes');
8+
const serverRoutes = require('./routes/server.routes');
9+
const propertiesRoute = require('./routes/properties.routes');
10+
const installationsRoutes = require('./routes/installations.routes');
11+
const infoRoutes = require('./routes/info.routes');
12+
13+
app.use(cors());
14+
app.use(limiter)
15+
app.use(express.json());
16+
17+
app.use('/server', serverRoutes);
18+
app.use('/installations', installationsRoutes);
19+
app.use('/properties', propertiesRoute);
20+
app.use('/info', infoRoutes);
21+
app.use('/admin', adminRoutes);
22+
23+
app.get('/ping', async (req, res) => {
24+
res.send(`pong`);
25+
});
26+
27+
module.exports = {
28+
app
29+
}

api/cleaner.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
21
let cleanup_done = false;
3-
const networkingUtils = require('./utils/networkingUtils');
4-
const configUtils = require('./utils/configUtils');
2+
const networkingUtils = require('./utils/networking.util');
3+
const configUtils = require('./utils/config.util');
54
const debug = configUtils.getConfigAttribute("debug");
65

7-
86
async function cleanup() {
9-
107
if(debug){
118
console.log("\nDebug mode is turned on\n Skipping cleanup tasks...\n if you don't intend this, change 'debug' from true to false in server-config.json");
129
console.log("====== API TERMINATED! ======");
@@ -27,20 +24,17 @@ async function cleanup() {
2724
}
2825

2926
process.on('exit', () => {
30-
if (!cleanup_done) {
27+
if (!cleanup_done)
3128
cleanup();
32-
}
3329
});
3430

35-
process.on('SIGINT', async () => {
31+
async function handleExit() {
3632
await cleanup();
3733
process.exit(0);
38-
});
34+
}
3935

40-
process.on('SIGTERM', async () => {
41-
await cleanup();
42-
process.exit(0);
43-
});
36+
process.on('SIGINT', handleExit);
37+
process.on('SIGTERM', handleExit);
4438

4539
process.on('uncaughtException', async (err) => {
4640
console.error('Uncaught Exception:', err);

api/consts.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ const configFilePath = "./server-config.json";
1414
const eulaFilePath = `${serverDirectory}/eula.txt`;
1515
const upnpcPath = '/upnpc';
1616

17+
const serverStatus = {
18+
OFFLINE: 0,
19+
RUNNING: 1,
20+
STARTING: 2
21+
};
1722

1823
module.exports = {
1924
serverDirectory,
@@ -29,4 +34,5 @@ module.exports = {
2934
serverBannedIPsPath,
3035
keysJSONPath,
3136
eulaFilePath,
37+
serverStatus
3238
};

api/controllers/info.controller.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const propertiesService = require('../services/properties.service');
2+
const infoService = require('../services/info.service');
3+
const serverService = require('../services/server.service');
4+
const consts = require('../consts');
5+
6+
async function playerCount(req, res) {
7+
try {
8+
const playerCount = await propertiesService.getOnlinePlayers();
9+
10+
res.status(200).send({ playerCount: playerCount });
11+
} catch(error) {
12+
console.error(error);
13+
res.status(500).send("error.. " + error.message);
14+
}
15+
}
16+
17+
async function getUpTime(req, res) {
18+
try {
19+
if (infoService.getStartTime() === null) {
20+
res.status(200).send({ uptime: "0s" });
21+
return;
22+
}
23+
24+
const ms = Date.now() - infoService.getStartTime();
25+
26+
const totalSeconds = Math.floor(ms / 1000);
27+
const hours = Math.floor(totalSeconds / 3600);
28+
const minutes = Math.floor((totalSeconds % 3600) / 60);
29+
const seconds = totalSeconds % 60;
30+
31+
let formatted = [];
32+
if (hours > 0) formatted.push(`${hours}h`);
33+
if (minutes > 0 || hours > 0) formatted.push(`${minutes}m`);
34+
formatted.push(`${seconds}s`);
35+
36+
res.status(200).send({ uptime: formatted.join(" ") });
37+
} catch (error) {
38+
console.error(error);
39+
res.status(500).send("error.. " + error.message);
40+
}
41+
}
42+
43+
async function getMemoryUsage(req, res) {
44+
try {
45+
let serverProcess = serverService.getServerProcess();
46+
res.status(200).send(await infoService.getMemoryUsage(serverProcess));
47+
} catch (error) {
48+
console.error(error);
49+
res.status(500).send("error.. " + error.message);
50+
}
51+
}
52+
53+
async function getWorldSize(req, res) {
54+
try {
55+
let worldSize = await infoService.getDirectorySize(consts.serverDirectory + "/world");
56+
res.status(200).send({ worldSize: `${worldSize.toFixed(2)}MB` });
57+
} catch (error) {
58+
console.error(error);
59+
res.status(500).send("error.. " + error.message);
60+
}
61+
}
62+
63+
async function getVersion(req, res) {
64+
try {
65+
let version = infoService.getVersion(consts.serverDirectory + "/" + consts.serverName);
66+
res.status(200).send({ version: version });
67+
} catch (error) {
68+
console.error(error);
69+
res.status(500).send("error.. " + error.message);
70+
}
71+
}
72+
73+
async function getPlatform(req, res) {
74+
try {
75+
let platform = infoService.getPlatform(consts.serverDirectory + "/" + consts.serverName);
76+
res.status(200).send({ platform: platform });
77+
} catch (error) {
78+
console.error(error);
79+
res.status(500).send("error.. " + error.message);
80+
}
81+
}
82+
83+
module.exports = {
84+
playerCount,
85+
getUpTime,
86+
getMemoryUsage,
87+
getWorldSize,
88+
getVersion,
89+
getPlatform
90+
}
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
const express = require('express');
2-
const router = express.Router();
3-
const installationsUtils = require('../utils/installationsUtils');
4-
const { updateConfigAttribute } = require('../utils/configUtils');
5-
1+
const installationsService = require('../services/installations.service');
2+
const { updateConfigAttribute } = require('../utils/config.util');
63
const { Sema } = require('async-sema');
74

85
const downloadSema = new Sema(1);
96

10-
router.put('/download/:platform/:version', async (req, res) => {
7+
async function downloadServer(req, res) {
118
await downloadSema.acquire();
129

1310
try {
14-
await installationsUtils.downloadRouter(req.params.platform, req.params.version);
11+
await installationsService.downloadRouter(req.params.platform, req.params.version);
1512
res.status(201).send('Downloaded Successfully');
1613

1714
updateConfigAttribute("platform", req.params.platform);
1815
updateConfigAttribute("version", req.params.version);
19-
2016
} catch (error) {
21-
2217
res.status(500).send(`Error downloading server files: ${error}`);
2318
} finally{
2419
downloadSema.release();
2520
}
26-
});
27-
21+
}
2822

29-
module.exports = router;
23+
module.exports = {
24+
downloadServer
25+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
const propertiesService = require("../services/properties.service");
2+
const configUtils = require("../utils/config.util");
3+
const jsonFilesUtils = require("../utils/files.util");
4+
5+
const { Sema } = require('async-sema');
6+
7+
let togglePropertySema = new Sema(1);
8+
9+
async function toggleProperty(req, res) {
10+
togglePropertySema.acquire();
11+
12+
try {
13+
console.log(req.params.property);
14+
await propertiesService.updateProperty(req.params.property, true);
15+
res.status(200).send("done");
16+
} catch(error) {
17+
console.error(error);
18+
res.status(500).send("error.. " + error.message);
19+
} finally {
20+
togglePropertySema.release();
21+
}
22+
}
23+
24+
let ramAllocationSema = new Sema(1);
25+
26+
async function allocateRam(req, res) {
27+
ramAllocationSema.acquire();
28+
29+
try {
30+
configUtils.updateMemoryAllocated(req.params.mb, true);
31+
res.status(200).send(`Ram allocation updated to ${req.params.mb}M`);
32+
} catch(error) {
33+
console.error(error);
34+
res.status(500).send("error.. " + error.message);
35+
} finally {
36+
ramAllocationSema.release();
37+
}
38+
}
39+
40+
async function serverConfig(req, res) {
41+
try {
42+
const configJSON = configUtils.getConfigJSON();
43+
res.status(200).send(configJSON);
44+
} catch(error) {
45+
console.error(error);
46+
res.status(500).send("error.. " + error.message);
47+
}
48+
}
49+
50+
async function getWhitelist(req, res) {
51+
try {
52+
const whitelistJSON = jsonFilesUtils.getWhitelistJSON();
53+
res.status(200).send(whitelistJSON);
54+
} catch(error) {
55+
console.error(error);
56+
res.status(500).send("error.. " + error.message);
57+
}
58+
}
59+
60+
async function getOps(req, res) {
61+
try {
62+
const opsJSON = jsonFilesUtils.getOpsJSON();
63+
res.status(200).send(opsJSON);
64+
} catch(error) {
65+
console.error(error);
66+
res.status(500).send("error.. " + error.message);
67+
}
68+
}
69+
70+
async function getBannedPlayers(req, res) {
71+
try {
72+
const bannedplayersJSON = jsonFilesUtils.getBannedPlayersJSON();
73+
res.status(200).send(bannedplayersJSON);
74+
} catch(error) {
75+
console.error(error);
76+
res.status(500).send("error.. " + error.message);
77+
}
78+
}
79+
80+
async function modifyOperator(req, res) {
81+
try {
82+
if (req.params.operation != "add" && req.params.operation != "remove") {
83+
res.status(404).send(`Invalid operation ${req.params.operation}`);
84+
return;
85+
}
86+
87+
await jsonFilesUtils.modifyOpsJSON(req.params.playername, req.params.operation === "add");
88+
res.status(200).send(`${req.params.operation === "add" ? "Added" : "Remove"} ${req.params.playername} as an Operator`);
89+
} catch(error) {
90+
console.error(error);
91+
res.status(500).send("error.. " + error.message);
92+
}
93+
}
94+
95+
async function modifyWhitelist(req, res) {
96+
try {
97+
if (req.params.operation != "add" && req.params.operation != "remove") {
98+
res.status(404).send(`Invalid operation ${req.params.operation}`);
99+
return;
100+
}
101+
102+
await jsonFilesUtils.modifyWhitelistJSON(req.params.playername, req.params.operation === "add");
103+
if (req.params.operation === "add")
104+
res.status(200).send(`Added ${req.params.playername} to the Whitelist`);
105+
else
106+
res.status(200).send(`Remove ${req.params.playername} from the Whitelist`);
107+
108+
} catch(error) {
109+
console.error(error);
110+
res.status(500).send("error.. " + error.message);
111+
}
112+
}
113+
114+
async function modifyBanned(req, res) {
115+
try {
116+
if (req.params.operation != "add" && req.params.operation != "remove") {
117+
res.status(404).send(`Invalid operation ${req.params.operation}`);
118+
return;
119+
}
120+
121+
await jsonFilesUtils.modifyBannedPlayersJSON(req.params.playername, req.params.operation === "add");
122+
if (req.params.operation === "add")
123+
res.status(200).send(`Banned ${req.params.playername}`);
124+
else
125+
res.status(200).send(`Pardoned ${req.params.playername}`);
126+
127+
} catch(error) {
128+
console.error(error);
129+
res.status(500).send("error.. " + error.message);
130+
}
131+
}
132+
133+
async function modifyBannedIPs(req, res) {
134+
try {
135+
if (req.params.operation != "add" && req.params.operation != "remove") {
136+
res.status(404).send(`Invalid operation ${req.params.operation}`);
137+
return;
138+
}
139+
140+
await jsonFilesUtils.modifyBannedIPsJSON(req.params.playername, req.params.operation === "add");
141+
if (req.params.operation === "add")
142+
res.status(200).send(`Banned ${req.params.playername}`);
143+
else
144+
res.status(200).send(`Pardoned ${req.params.playername}`);
145+
146+
} catch(error) {
147+
console.error(error);
148+
res.status(500).send("error.. " + error.message);
149+
}
150+
}
151+
152+
153+
module.exports = {
154+
toggleProperty,
155+
allocateRam,
156+
serverConfig,
157+
getWhitelist,
158+
getOps,
159+
getBannedPlayers,
160+
modifyOperator,
161+
modifyWhitelist,
162+
modifyBanned,
163+
modifyBannedIPs
164+
}

0 commit comments

Comments
 (0)