diff options
| author | Jan Tuomi <jan.tuomi@eficode.com> | 2019-02-12 14:28:33 +0200 |
|---|---|---|
| committer | Jan Tuomi <jan.tuomi@eficode.com> | 2019-02-12 14:28:44 +0200 |
| commit | c558a234b8564b04d26639549506ec4063f2b00a (patch) | |
| tree | 46f43464b2d99e1c6ecef3cff65827a14c50685a /index.js | |
| parent | 586b29c82735d86615d96cd9cf0e9e9297922407 (diff) | |
Add deps and implement index.jsmaster
Diffstat (limited to 'index.js')
| -rwxr-xr-x | index.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/index.js b/index.js new file mode 100755 index 0000000..fbfb86c --- /dev/null +++ b/index.js @@ -0,0 +1,43 @@ +#!/usr/bin/env node + +const util = require('util'); +const exec = util.promisify(require('child_process').exec); + +const argv = require('minimist')(process.argv.slice(2)); +const express = require('express'); +const app = express(); +const morgan = require('morgan'); +app.use(morgan('tiny')); + +const command = argv.exec || 'echo "Echo working!"'; +const port = argv.p || 8080; +const method = argv.method || 'GET'; +const path = argv.path || '/'; + +async function runCommand() { + const { stdout, stderr } = await exec(command); + if (stdout.trim().length > 0) { + console.log('stdout:', stdout); + } + if (stderr.trim().length > 0) { + console.log('stderr:', stderr); + } + return stdout; +}; + +const method_attr = method.toLowerCase(); +app[method_attr](path, async (req, res) => { + try { + const output = await runCommand(); + res.send(output); + } catch (err) { + res.send(String(err)); + console.error(err); + } +}); + +app.listen(port, () => { + console.log(`Listening on port ${port}!`); + console.log(`HTTP method: ${method_attr}, path: ${path}`); + console.log(`Shell command: "${command}"`); +}); |
