2013-09-05 16:02:48 +02:00
|
|
|
/**
|
|
|
|
* Copyright 2013 IBM Corp.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var createUI = require("./ui");
|
|
|
|
var redNodes = require("./nodes");
|
2013-10-13 22:01:46 +02:00
|
|
|
|
2013-09-05 16:02:48 +02:00
|
|
|
var app = null;
|
|
|
|
var server = null;
|
2013-11-12 18:13:06 +01:00
|
|
|
var settings = null;
|
|
|
|
var storage = null;
|
2013-09-05 16:02:48 +02:00
|
|
|
|
2013-11-12 18:13:06 +01:00
|
|
|
function createServer(_server,_settings) {
|
2013-09-05 16:02:48 +02:00
|
|
|
server = _server;
|
2013-11-12 18:13:06 +01:00
|
|
|
settings = _settings;
|
|
|
|
storage = require("./storage");
|
2013-09-05 16:02:48 +02:00
|
|
|
app = createUI(settings);
|
|
|
|
|
2013-10-05 22:19:55 +02:00
|
|
|
flowfile = settings.flowFile || 'flows_'+require('os').hostname()+'.json';
|
2013-10-04 18:28:15 +02:00
|
|
|
|
2013-09-05 16:02:48 +02:00
|
|
|
app.get("/nodes",function(req,res) {
|
|
|
|
res.writeHead(200, {'Content-Type': 'text/plain'});
|
|
|
|
res.write(redNodes.getNodeConfigs());
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/flows",function(req,res) {
|
2013-11-10 01:05:58 +01:00
|
|
|
res.writeHead(200, {'Content-Type': 'text/plain'});
|
|
|
|
res.write(JSON.stringify(redNodes.getConfig()));
|
|
|
|
res.end();
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
app.post("/flows",function(req,res) {
|
|
|
|
var fullBody = '';
|
|
|
|
req.on('data', function(chunk) {
|
|
|
|
fullBody += chunk.toString();
|
|
|
|
});
|
|
|
|
req.on('end', function() {
|
|
|
|
res.writeHead(204, {'Content-Type': 'text/plain'});
|
|
|
|
res.end();
|
2013-11-10 01:05:58 +01:00
|
|
|
var flows = JSON.parse(fullBody);
|
|
|
|
storage.saveFlows(flows).then(function() {
|
|
|
|
redNodes.setConfig(flows);
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
util.log("[red] Error saving flows : "+err);
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-09-20 18:15:45 +02:00
|
|
|
}
|
2013-10-13 22:01:46 +02:00
|
|
|
|
2013-09-20 18:15:45 +02:00
|
|
|
function start() {
|
2013-11-12 18:13:06 +01:00
|
|
|
storage.init(settings).then(function() {
|
|
|
|
console.log("\nWelcome to Node-RED\n===================\n");
|
|
|
|
util.log("[red] Loading palette nodes");
|
|
|
|
util.log("------------------------------------------");
|
2013-11-13 18:02:29 +01:00
|
|
|
redNodes.load(settings);
|
2013-11-12 18:13:06 +01:00
|
|
|
util.log("");
|
|
|
|
util.log('You may ignore any errors above here if they are for');
|
|
|
|
util.log('nodes you are not using. The nodes indicated will not');
|
|
|
|
util.log('be available in the main palette until any missing');
|
|
|
|
util.log('modules are installed, typically by running:');
|
|
|
|
util.log(' npm install {the module name}');
|
|
|
|
util.log('or any other errors are resolved');
|
|
|
|
util.log("------------------------------------------");
|
|
|
|
|
|
|
|
storage.getFlows().then(function(flows) {
|
|
|
|
if (flows.length > 0) {
|
|
|
|
redNodes.setConfig(flows);
|
|
|
|
}
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
util.log("[red] Error loading flows : "+err);
|
|
|
|
});
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
}
|
2013-09-20 18:15:45 +02:00
|
|
|
|
2013-10-13 20:14:39 +02:00
|
|
|
function stop() {
|
|
|
|
redNodes.stopFlows();
|
|
|
|
}
|
|
|
|
|
2013-09-05 16:02:48 +02:00
|
|
|
module.exports = {
|
2013-09-20 18:15:45 +02:00
|
|
|
init: createServer,
|
2013-10-13 20:14:39 +02:00
|
|
|
start: start,
|
|
|
|
stop: stop
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.__defineGetter__("app", function() { return app });
|
|
|
|
module.exports.__defineGetter__("server", function() { return server });
|