1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Restructure server code to make embedding easier

This commit is contained in:
Nicholas O'Leary 2013-09-20 17:15:45 +01:00
parent 7348e475eb
commit 0ecbbca7e4
5 changed files with 109 additions and 86 deletions

10
red.js
View File

@ -19,20 +19,18 @@ var util = require("util");
var express = require("express"); var express = require("express");
var crypto = require("crypto"); var crypto = require("crypto");
var settings = require("./settings"); var settings = require("./settings");
var RED = require("./red/red.js");
var server; var server;
var app = express(); var app = express();
var redApp = null;
if (settings.https) { if (settings.https) {
server = https.createServer(settings.https,function(req,res){app(req,res);}); server = https.createServer(settings.https,function(req,res){app(req,res);});
} else { } else {
server = http.createServer(function(req,res){app(req,res);}); server = http.createServer(function(req,res){app(req,res);});
} }
redApp = require('./red/server.js').init(server,settings);
settings.httpRoot = settings.httpRoot||"/"; settings.httpRoot = settings.httpRoot||"/";
if (settings.httpRoot[0] != "/") { if (settings.httpRoot[0] != "/") {
@ -51,9 +49,11 @@ if (settings.httpAuth) {
); );
} }
app.use(settings.httpRoot,redApp); RED.init(server,settings);
app.use(settings.httpRoot,RED.app);
server.listen(settings.uiPort); server.listen(settings.uiPort);
RED.start();
util.log('[red] Server now running at http'+(settings.https?'s':'')+'://127.0.0.1:'+settings.uiPort+settings.httpRoot); util.log('[red] Server now running at http'+(settings.https?'s':'')+'://127.0.0.1:'+settings.uiPort+settings.httpRoot);

View File

@ -16,10 +16,12 @@
var fs = require("fs"); var fs = require("fs");
var fspath = require("path"); var fspath = require("path");
var redUI = require("./server"); var redApp = null;
function init() {
redApp = require("./server").app;
// -------- Flow Library -------- // -------- Flow Library --------
redUI.app.post(new RegExp("/library/flows\/(.*)"), function(req,res) { redApp.post(new RegExp("/library/flows\/(.*)"), function(req,res) {
var fullBody = ''; var fullBody = '';
req.on('data', function(chunk) { req.on('data', function(chunk) {
fullBody += chunk.toString(); fullBody += chunk.toString();
@ -60,7 +62,7 @@ function listFiles(dir) {
return result; return result;
} }
redUI.app.get("/library/flows",function(req,res) { redApp.get("/library/flows",function(req,res) {
var flows = {}; var flows = {};
if (fs.existsSync("lib/flows")) { if (fs.existsSync("lib/flows")) {
flows = listFiles("lib/flows"); flows = listFiles("lib/flows");
@ -73,7 +75,7 @@ redUI.app.get("/library/flows",function(req,res) {
}); });
redUI.app.get(new RegExp("/library/flows\/(.*)"), function(req,res) { redApp.get(new RegExp("/library/flows\/(.*)"), function(req,res) {
var fn = "lib/flows/"+req.params[0]+".json"; var fn = "lib/flows/"+req.params[0]+".json";
if (fs.existsSync(fn)) { if (fs.existsSync(fn)) {
fs.readFile(fn,function(err,data) { fs.readFile(fn,function(err,data) {
@ -87,7 +89,7 @@ redUI.app.get(new RegExp("/library/flows\/(.*)"), function(req,res) {
}); });
// ------------------------------ // ------------------------------
}
function createLibrary(type) { function createLibrary(type) {
@ -100,7 +102,7 @@ function createLibrary(type) {
fs.mkdir(root); fs.mkdir(root);
} }
}); });
redUI.app.get(new RegExp("/library/"+type+"($|\/(.*))"),function(req,res) { redApp.get(new RegExp("/library/"+type+"($|\/(.*))"),function(req,res) {
var path = req.params[1]||""; var path = req.params[1]||"";
var rootPath = fspath.join(root,path); var rootPath = fspath.join(root,path);
@ -141,7 +143,7 @@ function createLibrary(type) {
}); });
}); });
redUI.app.post(new RegExp("/library/"+type+"\/(.*)"),function(req,res) { redApp.post(new RegExp("/library/"+type+"\/(.*)"),function(req,res) {
var path = req.params[0]; var path = req.params[0];
var fullBody = ''; var fullBody = '';
req.on('data', function(chunk) { req.on('data', function(chunk) {
@ -246,4 +248,5 @@ function getFileBody(root,path,res) {
res.end(); res.end();
} }
module.exports.init = init;
module.exports.register = createLibrary; module.exports.register = createLibrary;

View File

@ -66,7 +66,11 @@ var registry = (function() {
events.emit("nodes-stopped"); events.emit("nodes-stopped");
nodes = {}; nodes = {};
}, },
each: function(cb) {
for (var n in nodes) {
cb(nodes[n]);
}
},
addLogHandler: function(handler) { addLogHandler: function(handler) {
logHandlers.push(handler); logHandlers.push(handler);
} }
@ -132,6 +136,7 @@ util.inherits(Node,EventEmitter);
Node.prototype.close = function() { Node.prototype.close = function() {
// called when a node is removed // called when a node is removed
this.emit("close");
} }

View File

@ -18,18 +18,28 @@ var events = require("./events");
var server = require("./server"); var server = require("./server");
var nodes = require("./nodes"); var nodes = require("./nodes");
var library = require("./library"); var library = require("./library");
var settings = require("../settings"); var settings = null;
var events = require("events"); var events = require("events");
var RED = { var RED = {
init: function(httpServer,userSettings) {
settings = userSettings;
server.init(httpServer,settings);
library.init();
},
start: server.start,
nodes: nodes, nodes: nodes,
app: server.app,
server: server.server,
settings: settings,
library: library, library: library,
events: events events: events
}; };
RED.__defineGetter__("app", function() { return server.app });
RED.__defineGetter__("server", function() { return server.server });
RED.__defineGetter__("settings", function() { return settings });
module.exports = RED; module.exports = RED;

View File

@ -19,6 +19,8 @@ var util = require('util');
var createUI = require("./ui"); var createUI = require("./ui");
var redNodes = require("./nodes"); var redNodes = require("./nodes");
var host = require('os').hostname(); var host = require('os').hostname();
//TODO: relocated user dir
var rulesfile = process.argv[2] || 'flows_'+host+'.json';
var app = null; var app = null;
var server = null; var server = null;
@ -28,7 +30,6 @@ function createServer(_server,settings) {
app = createUI(settings); app = createUI(settings);
//TODO: relocated user dir //TODO: relocated user dir
var rulesfile = process.argv[2] || 'flows_'+host+'.json';
fs.exists("lib/",function(exists) { fs.exists("lib/",function(exists) {
if (!exists) { if (!exists) {
fs.mkdir("lib"); fs.mkdir("lib");
@ -70,7 +71,8 @@ function createServer(_server,settings) {
}); });
}); });
}); });
}
function start() {
console.log("\nWelcome to Node-RED\n===================\n"); console.log("\nWelcome to Node-RED\n===================\n");
util.log("[red] Loading palette nodes"); util.log("[red] Loading palette nodes");
util.log("------------------------------------------"); util.log("------------------------------------------");
@ -87,16 +89,19 @@ function createServer(_server,settings) {
fs.exists(rulesfile, function (exists) { fs.exists(rulesfile, function (exists) {
if (exists) { if (exists) {
util.log("[red] Loading workspace flow : "+rulesfile); util.log("[red] Loading flows : "+rulesfile);
fs.readFile(rulesfile,'utf8',function(err,data) { fs.readFile(rulesfile,'utf8',function(err,data) {
redNodes.setConfig(JSON.parse(data)); redNodes.setConfig(JSON.parse(data));
}); });
} else {
util.log("[red] Flows file not found : "+rulesfile);
} }
}); });
return app;
} }
module.exports = { module.exports = {
init: createServer init: createServer,
start: start
} }
module.exports.__defineGetter__("app", function() { return app }); module.exports.__defineGetter__("app", function() { return app });