diff --git a/api/routes/serverRoutes.js b/api/routes/serverRoutes.js index 809b0a7..b85d01c 100644 --- a/api/routes/serverRoutes.js +++ b/api/routes/serverRoutes.js @@ -2,6 +2,7 @@ const express = require('express'); const router = express.Router(); const serverUtils = require('../utils/serverUtils'); const { Sema } = require('async-sema'); +const configUtils = require('../utils/configUtils'); const consoleSema = new Sema(1); @@ -81,8 +82,13 @@ router.put('/start', async (req, res) => { } serverUtils.serverStatus = 2; - await serverUtils.startServerWithScript(); + if( configUtils.getConfigAttribute("start_with_script") ){ + await serverUtils.startServerWithScript(); + }else{ + await serverUtils.startServer(); + } res.send('Server started.'); + } catch (error) { serverUtils.serverStatus = 0; res.status(500).send(`Error starting server: ${error}`); diff --git a/api/utils/configUtils.js b/api/utils/configUtils.js index 2d31df0..3731897 100644 --- a/api/utils/configUtils.js +++ b/api/utils/configUtils.js @@ -7,6 +7,7 @@ const defaultConfig = { "memory": "1024M", "platform": "vanilla", "version": "1.21.4", + "start_with_script": false, "mc_port": 25565, "api_port": 3001, "debug": false @@ -55,6 +56,7 @@ function generateConfigFile(OS=os.type(), memory="1024M", platform="vanilla", version="1.21.4", + start_with_script=false, mc_port=25565, api_port=3001, debug=false, @@ -64,6 +66,7 @@ function generateConfigFile(OS=os.type(), memory: defaultConfig.memory, platform: defaultConfig.platform, version: defaultConfig.version, + start_with_script: defaultConfig.start_with_script, mc_port: defaultConfig.mc_port, api_port: defaultConfig.api_port, debug: defaultConfig.debug diff --git a/api/utils/serverUtils.js b/api/utils/serverUtils.js index 3f556d1..fc3647a 100644 --- a/api/utils/serverUtils.js +++ b/api/utils/serverUtils.js @@ -3,6 +3,7 @@ const fs = require('fs'); const consts = require("../consts"); const {freemem} = require('os'); const {getConfigAttribute} = require("./configUtils"); +const path = require('path'); let serverProcess = null; let serverStatus = 0; @@ -306,7 +307,11 @@ async function startServerWithScript() { if(!validateMemory()){ throw new Error("Not enough memory for server to run"); } - + try{ + if (!fs.existsSync(path.join(consts.serverDirectory, 'start.sh'))) { + 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"); + } + serverProcess = spawn('sh', ['start.sh'], { cwd: consts.serverDirectory, stdio: ['pipe', 'pipe', 'pipe'], @@ -325,6 +330,11 @@ async function startServerWithScript() { serverProcess.on('close', (code) => { console.log(`Server process exited with code ${code}`); }); + + + }catch(error){ + throw new Error(error); + } }