-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (33 loc) · 1.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require('dotenv').config({path: __dirname + '/.env'});
const express = require('express');
const bodyParser = require('body-parser');
const { promisify } = require('util');
const exec = promisify(require('child_process').exec);
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const execute = async (req, res) => {
console.log(`RECV COMMAND: ${req.query && JSON.stringify(req.query)}`);
let data;
if (req.query && req.query.cmd && req.query.cmd.startsWith('axelard q ')) {
const execCommand = `docker exec -i axelar-core ${req.query.cmd}`;
console.log(`EXEC COMMAND: ${execCommand}`);
try {
data = await exec(execCommand);
data.stdout = data.stdout.trim();
} catch (error) {
data = error;
}
}
else {
data = { error: 'command not found' };
}
try {
console.log(`SEND RESPONSE: ${JSON.stringify(data)}`);
} catch (error) {}
res.send({ data });
};
app.get('/', (req, res) => execute(req, res));
const port = process.env.PORT || 3333;
console.log(`START SERVICE: on port ${port}`);
app.listen(port);