Remove node-namespace from API
This commit is contained in:
parent
7bab3ff65f
commit
a3974f38b7
358
lib/api.js
358
lib/api.js
|
@ -10,216 +10,208 @@
|
|||
|
||||
var fs = require('fs');
|
||||
|
||||
var express = require('express'),
|
||||
namespace = require('node-namespace');
|
||||
var express = require('express');
|
||||
|
||||
namespace("Cylon", function() {
|
||||
// The Cylon API Server provides an interface to communicate with master class
|
||||
// and retrieve information about the robots being controlled.
|
||||
this.ApiServer = (function() {
|
||||
function ApiServer(opts) {
|
||||
var self = this;
|
||||
|
||||
if (opts == null) { opts = {}; }
|
||||
var API = module.exports = function API(opts) {
|
||||
var self = this;
|
||||
|
||||
this.opts = opts;
|
||||
this.host = opts.host || "127.0.0.1";
|
||||
this.port = opts.port || "3000";
|
||||
this.ssl = opts.ssl;
|
||||
this.master = opts.master;
|
||||
this.server = express();
|
||||
if (opts == null) {
|
||||
opts = {};
|
||||
}
|
||||
|
||||
//configure ssl if requested
|
||||
if (this.ssl && typeof(this.ssl) === 'object') {
|
||||
var https = require('https');
|
||||
this.opts = opts;
|
||||
this.host = opts.host || "127.0.0.1";
|
||||
this.port = opts.port || "3000";
|
||||
this.ssl = opts.ssl;
|
||||
this.master = opts.master;
|
||||
this.server = express();
|
||||
|
||||
var options = {
|
||||
key: fs.readFileSync(this.ssl.key || __dirname + "/ssl/server.key"),
|
||||
cert: fs.readFileSync(this.ssl.cert || __dirname + "/ssl/server.crt")
|
||||
};
|
||||
//configure ssl if requested
|
||||
if (this.ssl && typeof(this.ssl) === 'object') {
|
||||
var https = require('https');
|
||||
|
||||
this.server.node = https.createServer(options, this.server);
|
||||
} else {
|
||||
Logger.warn("API using insecure connection. We recommend using an SSL certificate with Cylon.")
|
||||
this.server.node = this.server;
|
||||
}
|
||||
|
||||
// configure basic auth, if requested
|
||||
if (opts.auth && opts.auth.type && opts.auth.type === 'basic') {
|
||||
var user = opts.auth.user,
|
||||
pass = opts.auth.pass;
|
||||
|
||||
if (user && pass) {
|
||||
this.server.use(express.basicAuth(user, pass));
|
||||
}
|
||||
}
|
||||
|
||||
this.server.set('title', 'Cylon API Server');
|
||||
this.server.use(express.json());
|
||||
this.server.use(express.urlencoded());
|
||||
this.server.use(express["static"](__dirname + "/../node_modules/robeaux/"));
|
||||
var options = {
|
||||
key: fs.readFileSync(this.ssl.key || __dirname + "/ssl/server.key"),
|
||||
cert: fs.readFileSync(this.ssl.cert || __dirname + "/ssl/server.crt")
|
||||
};
|
||||
|
||||
ApiServer.prototype.listen = function() {
|
||||
var self = this;
|
||||
this.server.node.listen(this.port, this.host, null, function() {
|
||||
var title = self.server.get('title');
|
||||
var protocol = self.ssl ? "https" : "http";
|
||||
this.server.node = https.createServer(options, this.server);
|
||||
} else {
|
||||
Logger.warn("API using insecure connection. We recommend using an SSL certificate with Cylon.")
|
||||
this.server.node = this.server;
|
||||
}
|
||||
|
||||
Logger.info(title + " is now online.");
|
||||
Logger.info("Listening at " + protocol + "://" + self.host + ":" + self.port);
|
||||
});
|
||||
};
|
||||
// configure basic auth, if requested
|
||||
if (opts.auth && opts.auth.type && opts.auth.type === 'basic') {
|
||||
var user = opts.auth.user,
|
||||
pass = opts.auth.pass;
|
||||
|
||||
// Parses req to extract params to be used for commands.
|
||||
//
|
||||
// Returns an array of params
|
||||
ApiServer.prototype.parseCommandParams = function(req) {
|
||||
var param_container = {},
|
||||
params = [];
|
||||
if (user && pass) {
|
||||
this.server.use(express.basicAuth(user, pass));
|
||||
}
|
||||
}
|
||||
|
||||
if (req.method === 'GET' || Object.keys(req.query).length > 0) {
|
||||
param_container = req.query;
|
||||
} else if (typeof req.body === 'object') {
|
||||
param_container = req.body;
|
||||
}
|
||||
this.server.set('title', 'Cylon API Server');
|
||||
this.server.use(express.json());
|
||||
this.server.use(express.urlencoded());
|
||||
this.server.use(express["static"](__dirname + "/../node_modules/robeaux/"));
|
||||
};
|
||||
|
||||
for (var p in param_container) { params.push(param_container[p]); }
|
||||
API.prototype.listen = function() {
|
||||
var self = this;
|
||||
|
||||
return params;
|
||||
};
|
||||
this.server.node.listen(this.port, this.host, null, function() {
|
||||
var title = self.server.get('title');
|
||||
var protocol = self.ssl ? "https" : "http";
|
||||
|
||||
// Sets all routes for the server
|
||||
ApiServer.prototype.configureRoutes = function() {
|
||||
var self = this;
|
||||
Logger.info(title + " is now online.");
|
||||
Logger.info("Listening at " + protocol + "://" + self.host + ":" + self.port);
|
||||
});
|
||||
};
|
||||
|
||||
var master = this.master;
|
||||
// Parses req to extract params to be used for commands.
|
||||
//
|
||||
// Returns an array of params
|
||||
API.prototype.parseCommandParams = function(req) {
|
||||
var param_container = {},
|
||||
params = [];
|
||||
|
||||
this.server.all("/*", function(req, res, next) {
|
||||
res.set("Access-Control-Allow-Origin", self.opts.CORS || "*");
|
||||
res.set("Access-Control-Allow-Headers", "Content-Type");
|
||||
res.set('Content-Type', 'application/json');
|
||||
return next();
|
||||
if (req.method === 'GET' || Object.keys(req.query).length > 0) {
|
||||
param_container = req.query;
|
||||
} else if (typeof req.body === 'object') {
|
||||
param_container = req.body;
|
||||
}
|
||||
|
||||
for (var p in param_container) { params.push(param_container[p]); }
|
||||
|
||||
return params;
|
||||
};
|
||||
|
||||
// Sets all routes for the server
|
||||
API.prototype.configureRoutes = function() {
|
||||
var self = this;
|
||||
|
||||
var master = this.master;
|
||||
|
||||
this.server.all("/*", function(req, res, next) {
|
||||
res.set("Access-Control-Allow-Origin", self.opts.CORS || "*");
|
||||
res.set("Access-Control-Allow-Headers", "Content-Type");
|
||||
res.set('Content-Type', 'application/json');
|
||||
return next();
|
||||
});
|
||||
|
||||
this.server.get("/robots", function(req, res) {
|
||||
var data = [];
|
||||
|
||||
for (var i = 0; i < master.robots.length; i++) {
|
||||
var robot = master.robots[i];
|
||||
data.push(robot.data());
|
||||
}
|
||||
|
||||
res.json(data);
|
||||
});
|
||||
|
||||
this.server.get("/robots/:robot", function(req, res) {
|
||||
master.findRobot(req.params.robot, function(err, robot) {
|
||||
res.json(err ? err : robot.data());
|
||||
});
|
||||
});
|
||||
|
||||
this.server.get("/robots/:robot/commands", function(req, res) {
|
||||
master.findRobot(req.params.robot, function(err, robot) {
|
||||
res.json(err ? err : robot.data().commands);
|
||||
});
|
||||
});
|
||||
|
||||
this.server.all("/robots/:robot/commands/:command", function(req, res) {
|
||||
var params = self.parseCommandParams(req);
|
||||
|
||||
master.findRobot(req.params.robot, function(err, robot) {
|
||||
if (err) { return res.json(err); }
|
||||
|
||||
var result = robot[req.params.command].apply(robot, params);
|
||||
res.json({ result: result });
|
||||
});
|
||||
});
|
||||
|
||||
this.server.get("/robots/:robot/devices", function(req, res) {
|
||||
master.findRobot(req.params.robot, function(err, robot) {
|
||||
res.json(err ? err : robot.data().devices);
|
||||
});
|
||||
});
|
||||
|
||||
this.server.get("/robots/:robot/devices/:device", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
device = req.params.device;
|
||||
|
||||
master.findRobotDevice(robot, device, function(err, device) {
|
||||
res.json(err ? err : device.data());
|
||||
});
|
||||
});
|
||||
|
||||
this.server.get("/robots/:robot/devices/:device/events/:event", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
device = req.params.device,
|
||||
event = req.params.event;
|
||||
|
||||
master.findRobotDevice(robot, device, function(err, device) {
|
||||
if (err) { res.json(err); }
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Connection': 'keep-alive',
|
||||
'Cache-Control': 'no-cache'
|
||||
});
|
||||
|
||||
this.server.get("/robots", function(req, res) {
|
||||
var data = [];
|
||||
var writeData = function(data) {
|
||||
res.write("data: " + JSON.stringify(data) + "\n\n")
|
||||
};
|
||||
|
||||
for (var i = 0; i < master.robots.length; i++) {
|
||||
var robot = master.robots[i];
|
||||
data.push(robot.data());
|
||||
}
|
||||
device.on(event, writeData);
|
||||
|
||||
res.json(data);
|
||||
res.on('close', function() {
|
||||
device.removeListener(event, writeData);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
this.server.get("/robots/:robot", function(req, res) {
|
||||
master.findRobot(req.params.robot, function(err, robot) {
|
||||
res.json(err ? err : robot.data());
|
||||
});
|
||||
});
|
||||
this.server.get("/robots/:robot/devices/:device/commands", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
device = req.params.device;
|
||||
|
||||
this.server.get("/robots/:robot/commands", function(req, res) {
|
||||
master.findRobot(req.params.robot, function(err, robot) {
|
||||
res.json(err ? err : robot.data().commands);
|
||||
});
|
||||
});
|
||||
master.findRobotDevice(robot, device, function(err, device) {
|
||||
res.json(err ? err : device.data().commands);
|
||||
});
|
||||
});
|
||||
|
||||
this.server.all("/robots/:robot/commands/:command", function(req, res) {
|
||||
var params = self.parseCommandParams(req);
|
||||
this.server.all("/robots/:robot/devices/:device/commands/:command", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
device = req.params.device,
|
||||
command = req.params.command;
|
||||
|
||||
master.findRobot(req.params.robot, function(err, robot) {
|
||||
if (err) { return res.json(err); }
|
||||
var params = self.parseCommandParams(req);
|
||||
|
||||
var result = robot[req.params.command].apply(robot, params);
|
||||
res.json({ result: result });
|
||||
});
|
||||
});
|
||||
master.findRobotDevice(robot, device, function(err, device) {
|
||||
if (err) { return res.json(err); }
|
||||
|
||||
this.server.get("/robots/:robot/devices", function(req, res) {
|
||||
master.findRobot(req.params.robot, function(err, robot) {
|
||||
res.json(err ? err : robot.data().devices);
|
||||
});
|
||||
});
|
||||
var result = device[command].apply(device, params);
|
||||
res.json({ result: result });
|
||||
});
|
||||
});
|
||||
|
||||
this.server.get("/robots/:robot/devices/:device", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
device = req.params.device;
|
||||
this.server.get("/robots/:robot/connections", function(req, res) {
|
||||
master.findRobot(req.params.robot, function(err, robot) {
|
||||
res.json(err ? err : robot.data().connections);
|
||||
});
|
||||
});
|
||||
|
||||
master.findRobotDevice(robot, device, function(err, device) {
|
||||
res.json(err ? err : device.data());
|
||||
});
|
||||
});
|
||||
this.server.get("/robots/:robot/connections/:connection", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
connection = req.params.connection;
|
||||
|
||||
this.server.get("/robots/:robot/devices/:device/events/:event", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
device = req.params.device,
|
||||
event = req.params.event;
|
||||
|
||||
master.findRobotDevice(robot, device, function(err, device) {
|
||||
if (err) { res.json(err); }
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Connection': 'keep-alive',
|
||||
'Cache-Control': 'no-cache'
|
||||
});
|
||||
|
||||
var writeData = function(data) {
|
||||
res.write("data: " + JSON.stringify(data) + "\n\n")
|
||||
};
|
||||
|
||||
device.on(event, writeData);
|
||||
|
||||
res.on('close', function() {
|
||||
device.removeListener(event, writeData);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
this.server.get("/robots/:robot/devices/:device/commands", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
device = req.params.device;
|
||||
|
||||
master.findRobotDevice(robot, device, function(err, device) {
|
||||
res.json(err ? err : device.data().commands);
|
||||
});
|
||||
});
|
||||
|
||||
this.server.all("/robots/:robot/devices/:device/commands/:command", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
device = req.params.device,
|
||||
command = req.params.command;
|
||||
|
||||
var params = self.parseCommandParams(req);
|
||||
|
||||
master.findRobotDevice(robot, device, function(err, device) {
|
||||
if (err) { return res.json(err); }
|
||||
|
||||
var result = device[command].apply(device, params);
|
||||
res.json({ result: result });
|
||||
});
|
||||
});
|
||||
|
||||
this.server.get("/robots/:robot/connections", function(req, res) {
|
||||
master.findRobot(req.params.robot, function(err, robot) {
|
||||
res.json(err ? err : robot.data().connections);
|
||||
});
|
||||
});
|
||||
|
||||
this.server.get("/robots/:robot/connections/:connection", function(req, res) {
|
||||
var robot = req.params.robot,
|
||||
connection = req.params.connection;
|
||||
|
||||
master.findRobotConnection(robot, connection, function(err, connection) {
|
||||
res.json(err ? err : connection.data());
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return ApiServer;
|
||||
|
||||
})();
|
||||
});
|
||||
|
||||
module.exports = Cylon.ApiServer;
|
||||
master.findRobotConnection(robot, connection, function(err, connection) {
|
||||
res.json(err ? err : connection.data());
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
@ -209,11 +209,11 @@ var Cylon = (function() {
|
|||
//
|
||||
// Returns an Cylon.ApiServer instance
|
||||
Master.prototype.startAPI = function() {
|
||||
var Server = require('./api');
|
||||
var API = require('./api');
|
||||
this.api_config.master = this;
|
||||
|
||||
if (this.api_instance === null) {
|
||||
this.api_instance = new Server(this.api_config);
|
||||
this.api_instance = new API(this.api_config);
|
||||
this.api_instance.configureRoutes();
|
||||
this.api_instance.listen();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue