node-red/red/server.js

111 lines
3.6 KiB
JavaScript
Raw Normal View History

2013-09-05 14:02:48 +00: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 fs = require('fs');
var util = require('util');
var createUI = require("./ui");
var redNodes = require("./nodes");
//TODO: relocated user dir
2013-10-04 16:28:15 +00:00
var flowfile = '';
2013-09-05 14:02:48 +00:00
var app = null;
var server = null;
function createServer(_server,settings) {
server = _server;
app = createUI(settings);
2013-10-04 16:28:15 +00:00
flowfile = settings.flowfile || 'flows_'+require('os').hostname()+'.json';
2013-09-05 14:02:48 +00:00
//TODO: relocated user dir
2013-09-20 13:21:45 +00:00
fs.exists("lib/",function(exists) {
if (!exists) {
fs.mkdir("lib");
}
});
2013-09-05 14:02:48 +00: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-10-04 16:28:15 +00:00
fs.exists(flowfile, function (exists) {
2013-09-05 14:02:48 +00:00
if (exists) {
2013-10-04 16:28:15 +00:00
res.sendfile(flowfile);
2013-09-05 14:02:48 +00:00
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write("[]");
res.end();
}
});
});
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-10-04 16:28:15 +00:00
fs.writeFile(flowfile, fullBody, function(err) {
2013-09-05 14:02:48 +00:00
if(err) {
util.log(err);
} else {
redNodes.setConfig(JSON.parse(fullBody));
2013-09-05 14:02:48 +00:00
}
});
});
});
}
function start() {
2013-09-05 14:02:48 +00:00
console.log("\nWelcome to Node-RED\n===================\n");
util.log("[red] Loading palette nodes");
util.log("------------------------------------------");
redNodes.load();
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("------------------------------------------");
2013-09-05 14:02:48 +00:00
2013-10-04 16:28:15 +00:00
fs.exists(flowfile, function (exists) {
2013-09-05 14:02:48 +00:00
if (exists) {
2013-10-04 16:28:15 +00:00
util.log("[red] Loading flows : "+flowfile);
fs.readFile(flowfile,'utf8',function(err,data) {
redNodes.setConfig(JSON.parse(data));
2013-09-05 14:02:48 +00:00
});
} else {
2013-10-04 16:28:15 +00:00
util.log("[red] Flows file not found : "+flowfile);
2013-09-05 14:02:48 +00:00
}
});
}
2013-09-05 14:02:48 +00:00
module.exports = {
init: createServer,
start: start
2013-09-05 14:02:48 +00:00
}
module.exports.__defineGetter__("app", function() { return app });
module.exports.__defineGetter__("server", function() { return server });