Created
January 28, 2020 08:10
-
-
Save alexesDev/2974825d459a1cb3664e4df41fdf3851 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { exec } = require('child_process'); | |
const view = ` | |
<pre style="height:300px; overflow: auto; border: 1px solid #eee" id="history"> | |
</pre> | |
<form method="post" id="form"> | |
<input type="text" name="command" size="100" id="command" /> | |
<button>Run</button> | |
</form> | |
<script> | |
const command = document.getElementById('command'); | |
const history = document.getElementById('history'); | |
document.getElementById('form').addEventListener('submit', function(e) { | |
e.preventDefault(); | |
history.innerHTML += '\\n\\n' + command.value + '\\n'; | |
fetch(location.pathname, { method: 'POST', body: command.value }) | |
.then(function(r) { return r.json() }) | |
.then(function(data) { | |
const result = [data.error, data.stdout, data.stderr].filter(Boolean).join('\\n'); | |
history.innerHTML += result; | |
history.scrollTop = history.scrollHeight; | |
}); | |
command.value = ''; | |
}); | |
</script> | |
`; | |
function render(type, body) { | |
return { | |
statusCode: 200, | |
headers: { | |
'Content-Type': type, | |
}, | |
isBase64Encoded: false, | |
body, | |
} | |
} | |
exports.handler = async function (event, context) { | |
if (event.body) { | |
const command = Buffer.from(event.body, 'base64').toString(); | |
return new Promise(resolve => { | |
exec(command, function (error, stdout, stderr) { | |
resolve(render('application/json', JSON.stringify({ error, stdout, stderr }))); | |
}); | |
}); | |
} | |
return render('text/html', view); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment