Skip to content

Commit

Permalink
Fix unit via test
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Jun 16, 2024
1 parent c643e7a commit 58a37cb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/executor/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ class UnitExecutor {
}
/**
* @param {string} path
* @returns {Promise<{code: string | number, stdout: string, stderr: string}>}
*/
async get(path) {
return await spawnSudoUtil("UNIT_GET", [path]);
}
/**
* @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]);
});
}
}
Expand Down
39 changes: 18 additions & 21 deletions sudoutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 58a37cb

Please sign in to comment.