Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion api/routes/serverRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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}`);
Expand Down
3 changes: 3 additions & 0 deletions api/utils/configUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion api/utils/serverUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'],
Expand All @@ -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);
}
}


Expand Down