Adding optional ssl param in API

By default http is used for api, but you can pass a param to use ssl.

This is the same behaviour that we have currently in robot.
This commit is contained in:
Javier Cervantes 2014-04-23 17:02:25 -05:00
parent e66c1145d5
commit cc3dac9442
3 changed files with 20 additions and 11 deletions

View File

@ -9,7 +9,8 @@
"use strict"; "use strict";
var fs = require('fs'), var fs = require('fs'),
https = require('https'); https = require('https'),
http = require('http');
var express = require('express'), var express = require('express'),
namespace = require('node-namespace'); namespace = require('node-namespace');
@ -29,15 +30,18 @@ namespace("Cylon", function() {
this.port = opts.port || "3000"; this.port = opts.port || "3000";
this.master = opts.master; this.master = opts.master;
this.server = express();
if (opts.ssl) {
var options = { var options = {
cert: fs.readFileSync(opts.cert || __dirname + "/ssl/server.crt"), cert: fs.readFileSync(opts.cert || __dirname + "/ssl/server.crt"),
key: fs.readFileSync(opts.key || __dirname + "/ssl/server.key") key: fs.readFileSync(opts.key || __dirname + "/ssl/server.key")
} }
this.server = express();
this.server.https = https.createServer(options, this.server); this.server.https = https.createServer(options, this.server);
} else {
this.server.https = this.server;
}
this.server.set('title', 'Cylon API Server'); this.server.set('title', 'Cylon API Server');
@ -62,6 +66,9 @@ namespace("Cylon", function() {
var title = self.server.get('title'); var title = self.server.get('title');
Logger.info(title + " is now online."); Logger.info(title + " is now online.");
Logger.info("Listening at https://" + self.host + ":" + self.port); Logger.info("Listening at https://" + self.host + ":" + self.port);
if(!self.opts.ssl) {
Logger.warn("API using insecure connection. We recommend using an SSL certificate with Cylon.");
}
}); });
}; };

View File

@ -49,7 +49,8 @@ var Cylon = (function() {
cert: null, cert: null,
key: null, key: null,
auth: {}, auth: {},
CORS: null CORS: null,
ssl: false
}; };
this.robot = bind(this.robot, this); this.robot = bind(this.robot, this);
@ -101,7 +102,7 @@ var Cylon = (function() {
Master.prototype.api = function(opts) { Master.prototype.api = function(opts) {
if (opts == null) { opts = {}; } if (opts == null) { opts = {}; }
var keys = ['host', 'port', 'auth', 'cert', 'CORS', 'key']; var keys = ['host', 'port', 'auth', 'cert', 'CORS', 'key', 'ssl'];
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
var key = keys[i]; var key = keys[i];

View File

@ -13,7 +13,8 @@ describe("API", function() {
stub(https, 'createServer').returns({ listen: spy() }); stub(https, 'createServer').returns({ listen: spy() });
opts = { opts = {
master: { name: 'master' } master: { name: 'master' },
ssl: true
} }
api = new API(opts); api = new API(opts);