From 58a37cb958248bb98617cfdd84a6efe01cca1245 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Sun, 16 Jun 2024 14:35:35 +0700 Subject: [PATCH] Fix unit via test --- .env.test | 1 + app.js | 2 +- src/controllers/unit.js | 8 ++++---- src/executor/unit.js | 4 +++- sudoutil.js | 39 ++++++++++++++++++--------------------- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/.env.test b/.env.test index 04ec32c..36f0efd 100644 --- a/.env.test +++ b/.env.test @@ -5,6 +5,7 @@ NGINX_PATH=./test/nginx.d/$.conf NGINX_OUT=./test/nginx.out/$.conf NGINX_BIN=echo nginx NGINX_START=echo systemctl start nginx +UNIT_SOCKET=/var/run/control.unit.sock IPTABLES_PATH=./test/iptables IPTABLES_OUT=./test/iptables.out IPTABLES_SAVE=echo . diff --git a/app.js b/app.js index 0b4a0ab..258d4fc 100644 --- a/app.js +++ b/app.js @@ -9,7 +9,7 @@ import app from './src/index.js'; const port = process.env.PORT ? parseInt(process.env.PORT) : 2223; const server = app.listen(port, function () { console.log(`Start time takes ` + (Date.now() - startTime) / 1000 + ` s`) - console.log(`Listening on ${port}`); + console.log(`Listening on ${port} in ${process.env.NODE_ENV} mode`); }) server.on('close', () => { console.log(`Server closing`); diff --git a/src/controllers/unit.js b/src/controllers/unit.js index d2b7b17..44cc19f 100644 --- a/src/controllers/unit.js +++ b/src/controllers/unit.js @@ -4,16 +4,16 @@ import { unitExec } from '../executor/unit.js'; export default function () { var router = express.Router(); - router.get('/', async function (req, res, next) { + router.get('/*', async function (req, res, next) { try { - res.json(await unitExec.get(req.path)); + res.json(JSON.parse((await unitExec.get(req.path)).stdout)); } catch (error) { next(error); } }); - router.post('/', async function (req, res, next) { + router.post('/*', async function (req, res, next) { try { - res.json(await unitExec.set(req.path, JSON.stringify(req.body))); + res.json(JSON.parse((await unitExec.set(req.path, JSON.stringify(req.body))).stdout)); } catch (error) { next(error); } diff --git a/src/executor/unit.js b/src/executor/unit.js index 899a212..b566a12 100644 --- a/src/executor/unit.js +++ b/src/executor/unit.js @@ -12,6 +12,7 @@ class UnitExecutor { } /** * @param {string} path + * @returns {Promise<{code: string | number, stdout: string, stderr: string}>} */ async get(path) { return await spawnSudoUtil("UNIT_GET", [path]); @@ -19,11 +20,12 @@ class UnitExecutor { /** * @param {string} path * @param {string} body + * @returns {Promise<{code: string | number, stdout: string, stderr: string}>} */ async set(path, body) { return await executeLock('unit', async () => { ShellString(body).to(tmpFile); - await spawnSudoUtil("UNIT_SET", [path]); + return await spawnSudoUtil("UNIT_SET", [path]); }); } } diff --git a/sudoutil.js b/sudoutil.js index 2db922c..c737a6c 100755 --- a/sudoutil.js +++ b/sudoutil.js @@ -114,37 +114,34 @@ switch (cli.args.shift()) { exit(0); case 'UNIT_GET': arg = cli.args.shift(); - http.request({ - socketPath: env.UNIT_SOCKET, - path: '/config' + arg, - }, res => { - res.setEncoding('utf8'); - res.on('data', data => process.stdout.write(data)); - res.on('end', () => exit(0)) - res.on('error', data => { console.error(data); exit(1); }); + var unit = spawn('curl', ['--unix-socket', env.UNIT_SOCKET, 'http://localhost/config' + arg], { + stdio: 'inherit', + }); + unit.on('close', function (code) { + exit(code); }); setTimeout(() => { // just in case - exit(1); + if (!unit.killed) + unit.kill(); }, 1000 * 60).unref(); + break; case 'UNIT_SET': arg = cli.args.shift(); - http.request({ - socketPath: env.UNIT_SOCKET, - path: '/config' + arg, - method: 'PUT', - }, res => { - res.setEncoding('utf8'); - res.push(cat(env.UNIT_TMP).stdout, 'utf-8'); - res.on('data', data => process.stdout.write(data)); - res.on('end', () => exit(0)) - res.on('error', data => { console.error(data); exit(1); }); + unit = spawn('curl', ['-X', 'PUT', + '-data-binary', '@' + env.UNIT_TMP, '--unix-socket', + env.UNIT_SOCKET, 'http://localhost/config' + arg], { + stdio: 'inherit', + }); + unit.on('close', function (code) { + exit(code); }); setTimeout(() => { // just in case - exit(1); + if (!unit.killed) + unit.kill(); }, 1000 * 60).unref(); - exit(0); + break; case 'VIRTUAL_SERVER_GET': arg = cli.args.shift(); cat(env.VIRTUAL_SERVER_PATH.replace('$', arg)).to(env.VIRTUAL_SERVER_TMP);