Tidy up REST interface

- Ensure application/json where appropriate
 - Use jquery api rather than d3
This commit is contained in:
Nick O'Leary
2014-04-15 22:30:07 +01:00
parent d702caa5be
commit d00624f9e3
3 changed files with 53 additions and 62 deletions

View File

@@ -29,8 +29,7 @@ function init() {
});
req.on('end', function() {
storage.saveFlow(req.params[0],fullBody).then(function() {
res.writeHead(204, {'Content-Type': 'text/plain'});
res.end();
res.send(204);
}).otherwise(function(err) {
util.log("[red] Error loading flow '"+req.params[0]+"' : "+err);
res.send(500);
@@ -40,17 +39,14 @@ function init() {
redApp.get("/library/flows",function(req,res) {
storage.getAllFlows().then(function(flows) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(JSON.stringify(flows));
res.end();
res.json(flows);
});
});
redApp.get(new RegExp("/library/flows\/(.*)"), function(req,res) {
storage.getFlow(req.params[0]).then(function(data) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(data);
res.end();
res.set('Content-Type', 'application/json');
res.send(data);
}).otherwise(function(err) {
if (err) {
util.log("[red] Error loading flow '"+req.params[0]+"' : "+err);
@@ -67,13 +63,13 @@ function createLibrary(type) {
redApp.get(new RegExp("/library/"+type+"($|\/(.*))"),function(req,res) {
var path = req.params[1]||"";
storage.getLibraryEntry(type,path).then(function(result) {
res.writeHead(200, {'Content-Type': 'text/plain'});
if (typeof result === "string") {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(result);
res.end();
} else {
res.write(JSON.stringify(result));
res.json(result);
}
res.end();
}).otherwise(function(err) {
if (err) {
util.log("[red] Error loading library entry '"+path+"' : "+err);

View File

@@ -37,39 +37,29 @@ function createServer(_server,_settings) {
flowfile = settings.flowFile || 'flows_'+require('os').hostname()+'.json';
app.get("/nodes",function(req,res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(redNodes.getNodeConfigs());
res.end();
res.json(redNodes.getNodeConfigs());
});
app.get("/flows",function(req,res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(JSON.stringify(redNodes.getConfig()));
res.end();
res.json(redNodes.getConfig());
});
app.post("/flows",function(req,res) {
var fullBody = '';
req.on('data', function(chunk) {
fullBody += chunk.toString();
app.post("/flows",
express.json(),
function(req,res) {
var flows = req.body;
storage.saveFlows(flows).then(function() {
res.json(204);
redNodes.setConfig(flows);
}).otherwise(function(err) {
util.log("[red] Error saving flows : "+err);
res.send(500,err.message);
});
req.on('end', function() {
try {
var flows = JSON.parse(fullBody);
storage.saveFlows(flows).then(function() {
res.writeHead(204, {'Content-Type': 'text/plain'});
res.end();
redNodes.setConfig(flows);
}).otherwise(function(err) {
util.log("[red] Error saving flows : "+err);
res.send(500, err.message);
});
} catch(err) {
util.log("[red] Error saving flows : "+err);
res.send(400, "Invalid flow");
}
});
});
},
function(error,req,res,next) {
res.send(400,"Invalid Flow");
}
);
}
function start() {