Created
April 6, 2015 05:12
-
-
Save paulsena/700ce1d75648ac25b177 to your computer and use it in GitHub Desktop.
Restify Node.js WS that executes a singleton cmd shell process. Was used as a POC for a WS controllable Raspberry Pi LED Text Ticker.
This file contains hidden or 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
var restify = require('restify'); | |
var sys = require('sys') | |
var exec = require('child_process').exec; | |
var child; | |
var USERNAME = 'sal'; | |
var PASSWORD = '16BrooksSt'; | |
var CMD = "emacs"; | |
//**** REST CODE ****** | |
function respond(req, res, next) { | |
if (req.authorization.basic && req.authorization.basic.username == USERNAME && | |
req.authorization.basic.password == PASSWORD) { | |
//Kill old process | |
if (child) { | |
child.kill("SIGKILL"); | |
} | |
//Start new Process - Async | |
child = exec(CMD + ' ' + req.query.text, function (error, stdout, stderr) { | |
// next.ifError(error); | |
//sys.puts(stdout); | |
console.log(stdout); | |
res.send("Sent"); | |
}); | |
next(); | |
} else { | |
return next(new restify.errors.UnauthorizedError); | |
} | |
} | |
var server = restify.createServer(); | |
server.use(restify.authorizationParser()); | |
server.use(restify.queryParser()); | |
server.get('/led', respond); | |
server.listen(8080, function() { | |
console.log('%s listening at %s', server.name, server.url); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment