Skip to content

Commit 2482b59

Browse files
authored
chore: Merge pull request #6 from DEVUCP/api
Api to Dev update
2 parents b6dc895 + bcfeb76 commit 2482b59

34 files changed

Lines changed: 1054 additions & 723 deletions

.github/workflows/node.js.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ on:
55
branches:
66
- main
77
- 'ci*'
8+
- 'front-end'
9+
- 'api'
810
pull_request:
911
branches:
1012
- main
1113
- 'ci*'
14+
- 'front-end'
15+
- 'api'
1216

1317
jobs:
1418
test-api-linux:
1519
name: API Test (Linux)
20+
if: github.ref_name == 'api' || (github.ref_name != 'api' && github.ref_name != 'front-end')
1621
runs-on: ubuntu-latest
1722

1823
strategy:
@@ -35,6 +40,7 @@ jobs:
3540

3641
test-frontend-linux:
3742
name: Frontend Test (Linux)
43+
if: github.ref_name == 'front-end' || (github.ref_name != 'api' && github.ref_name != 'front-end')
3844
runs-on: ubuntu-latest
3945

4046
strategy:
@@ -57,6 +63,7 @@ jobs:
5763

5864
test-api-windows:
5965
name: API Test (Windows)
66+
if: github.ref_name == 'api' || (github.ref_name != 'api' && github.ref_name != 'front-end')
6067
runs-on: windows-latest
6168

6269
strategy:
@@ -79,6 +86,7 @@ jobs:
7986

8087
test-frontend-windows:
8188
name: Frontend Test (Windows)
89+
if: github.ref_name == 'front-end' || (github.ref_name != 'api' && github.ref_name != 'front-end')
8290
runs-on: windows-latest
8391

8492
strategy:

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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
20+
res.status(200).send(await infoService.getUpTime());
21+
} catch (error) {
22+
console.error(error);
23+
res.status(500).send("error.. " + error.message);
24+
}
25+
}
26+
27+
async function getMemoryUsage(req, res) {
28+
try {
29+
let serverProcess = serverService.getServerProcess();
30+
res.status(200).send(await infoService.getMemoryUsage(serverProcess));
31+
} catch (error) {
32+
console.error(error);
33+
res.status(500).send("error.. " + error.message);
34+
}
35+
}
36+
37+
async function getWorldSize(req, res) {
38+
try {
39+
let worldSize = await infoService.getDirectorySize(consts.serverDirectory + "/world");
40+
res.status(200).send({ worldSize: `${worldSize.toFixed(2)}MB` });
41+
} catch (error) {
42+
console.error(error);
43+
res.status(500).send("error.. " + error.message);
44+
}
45+
}
46+
47+
async function getVersion(req, res) {
48+
try {
49+
let version = infoService.getVersion(consts.serverDirectory + "/" + consts.serverName);
50+
res.status(200).send({ version: version });
51+
} catch (error) {
52+
console.error(error);
53+
res.status(500).send("error.. " + error.message);
54+
}
55+
}
56+
57+
async function getPlatform(req, res) {
58+
try {
59+
let platform = infoService.getPlatform(consts.serverDirectory + "/" + consts.serverName);
60+
res.status(200).send({ platform: platform });
61+
} catch (error) {
62+
console.error(error);
63+
res.status(500).send("error.. " + error.message);
64+
}
65+
}
66+
67+
async function getAllInfo(req, res) {
68+
try {
69+
let serverProcess = serverService.getServerProcess();
70+
let jarPath = consts.serverDirectory + "/" + consts.serverName
71+
res.status(200).send(await infoService.getInfo(serverProcess, jarPath, consts.serverDirectory));
72+
} catch (error) {
73+
console.error(error);
74+
res.status(500).send("error.. " + error.message);
75+
}
76+
}
77+
78+
module.exports = {
79+
playerCount,
80+
getUpTime,
81+
getMemoryUsage,
82+
getWorldSize,
83+
getVersion,
84+
getPlatform,
85+
getAllInfo
86+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const installationsService = require('../services/installations.service');
2+
const { updateConfigAttribute } = require('../utils/config.util');
3+
const { Sema } = require('async-sema');
4+
5+
const downloadSema = new Sema(1);
6+
7+
async function downloadServer(req, res) {
8+
await downloadSema.acquire();
9+
10+
try {
11+
await installationsService.downloadRouter(req.params.platform, req.params.version);
12+
res.status(201).send('Downloaded Successfully');
13+
14+
updateConfigAttribute("platform", req.params.platform);
15+
updateConfigAttribute("version", req.params.version);
16+
} catch (error) {
17+
res.status(500).send(`Error downloading server files: ${error}`);
18+
} finally{
19+
downloadSema.release();
20+
}
21+
}
22+
23+
module.exports = {
24+
downloadServer
25+
}

0 commit comments

Comments
 (0)