Skip to content

Commit a3ec5cd

Browse files
authored
chore: Merge pull request #2 from DEVUCP/api
fix(api): Remove hardcoded use of starting with script
2 parents b6dc895 + a30eff7 commit a3ec5cd

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

api/routes/serverRoutes.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const express = require('express');
22
const router = express.Router();
33
const serverUtils = require('../utils/serverUtils');
44
const { Sema } = require('async-sema');
5+
const configUtils = require('../utils/configUtils');
56

67
const consoleSema = new Sema(1);
78

@@ -81,8 +82,13 @@ router.put('/start', async (req, res) => {
8182
}
8283
serverUtils.serverStatus = 2;
8384

84-
await serverUtils.startServerWithScript();
85+
if( configUtils.getConfigAttribute("start_with_script") ){
86+
await serverUtils.startServerWithScript();
87+
}else{
88+
await serverUtils.startServer();
89+
}
8590
res.send('Server started.');
91+
8692
} catch (error) {
8793
serverUtils.serverStatus = 0;
8894
res.status(500).send(`Error starting server: ${error}`);

api/utils/configUtils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const defaultConfig = {
77
"memory": "1024M",
88
"platform": "vanilla",
99
"version": "1.21.4",
10+
"start_with_script": false,
1011
"mc_port": 25565,
1112
"api_port": 3001,
1213
"debug": false
@@ -55,6 +56,7 @@ function generateConfigFile(OS=os.type(),
5556
memory="1024M",
5657
platform="vanilla",
5758
version="1.21.4",
59+
start_with_script=false,
5860
mc_port=25565,
5961
api_port=3001,
6062
debug=false,
@@ -64,6 +66,7 @@ function generateConfigFile(OS=os.type(),
6466
memory: defaultConfig.memory,
6567
platform: defaultConfig.platform,
6668
version: defaultConfig.version,
69+
start_with_script: defaultConfig.start_with_script,
6770
mc_port: defaultConfig.mc_port,
6871
api_port: defaultConfig.api_port,
6972
debug: defaultConfig.debug

api/utils/serverUtils.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const fs = require('fs');
33
const consts = require("../consts");
44
const {freemem} = require('os');
55
const {getConfigAttribute} = require("./configUtils");
6+
const path = require('path');
67

78
let serverProcess = null;
89
let serverStatus = 0;
@@ -306,7 +307,11 @@ async function startServerWithScript() {
306307
if(!validateMemory()){
307308
throw new Error("Not enough memory for server to run");
308309
}
309-
310+
try{
311+
if (!fs.existsSync(path.join(consts.serverDirectory, 'start.sh'))) {
312+
throw new Error("start.sh script not found in server directory\n If you don't intend on using a script, set start_server_with_script to false in server-config.json");
313+
}
314+
310315
serverProcess = spawn('sh', ['start.sh'], {
311316
cwd: consts.serverDirectory,
312317
stdio: ['pipe', 'pipe', 'pipe'],
@@ -325,6 +330,11 @@ async function startServerWithScript() {
325330
serverProcess.on('close', (code) => {
326331
console.log(`Server process exited with code ${code}`);
327332
});
333+
334+
335+
}catch(error){
336+
throw new Error(error);
337+
}
328338
}
329339

330340

0 commit comments

Comments
 (0)