diff --git a/src/controllers/runner.js b/src/controllers/runner.js index f768ec3..5a5ccc4 100644 --- a/src/controllers/runner.js +++ b/src/controllers/runner.js @@ -155,16 +155,62 @@ export async function runConfigInBackgroundSingleton(payload) { }).unref(); } +export async function runConfigInForeground(payload) { + return new Promise((resolve, reject) => { + let child = spawn('node', [path.join(process.cwd(), '/runner.js')], { + stdio: 'pipe', + env: { + RUNNER_PAYLOAD: JSON.stringify(payload), + } + }) + + let stdoutData = ''; + + // Collect stdout data + child.stdout.on('data', (data) => { + stdoutData += data.toString(); + }); + + // Collect stderr data + child.stderr.on('data', (data) => { + stdoutData += data.toString(); + }); + + // Handle process exit + child.on('exit', (exitCode) => { + if (exitCode !== 0) { + reject(new Error(`Process exited with code ${exitCode}\n${stdoutData}`)); + } else { + resolve(stdoutData.trim()); // Resolve with collected stdout + } + }); + + // Handle spawn errors + child.on('error', (error) => { + reject(error); + }); + }) +} + export default function () { var router = express.Router(); router.post('/', checkGet(['domain']), async function (req, res, next) { - runConfigInBackgroundSingleton({ - body: req.body, - domain: req.query.domain + "", - sandbox: !!parseInt(req.query.sandbox + '' || '0'), - callback: req.header('x-callback'), - }); - res.send('OK'); + const callback = req.header('x-callback'); + if (callback) { + runConfigInBackgroundSingleton({ + body: req.body, + domain: req.query.domain + "", + sandbox: !!parseInt(req.query.sandbox + '' || '0'), + callback: req.header('x-callback'), + }); + res.send('OK'); + } else { + res.send(await runConfigInForeground({ + body: req.body, + domain: req.query.domain + "", + sandbox: !!parseInt(req.query.sandbox + '' || '0'), + })); + } }); return router; } diff --git a/src/executor/virtualmin.js b/src/executor/virtualmin.js index 20f3197..01633f7 100644 --- a/src/executor/virtualmin.js +++ b/src/executor/virtualmin.js @@ -173,7 +173,7 @@ class VirtualminExecutor { return new Promise((resolve, reject) => { spawnSudoUtil('VIRTUAL_SERVER_GET', [id]).then(() => { const fileConf = cat(tmpFile); - const config = cat(tmpFile).trimEnd().split("\n"); + const config = fileConf.trimEnd().split("\n"); for (const [key, value] of Object.entries(props)) { let i = config.findIndex(x => x.startsWith(key + '=')); if (i >= 0) {