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:
parent
e66c1145d5
commit
cc3dac9442
23
lib/api.js
23
lib/api.js
|
@ -9,7 +9,8 @@
|
|||
"use strict";
|
||||
|
||||
var fs = require('fs'),
|
||||
https = require('https');
|
||||
https = require('https'),
|
||||
http = require('http');
|
||||
|
||||
var express = require('express'),
|
||||
namespace = require('node-namespace');
|
||||
|
@ -29,15 +30,18 @@ namespace("Cylon", function() {
|
|||
this.port = opts.port || "3000";
|
||||
|
||||
this.master = opts.master;
|
||||
|
||||
var options = {
|
||||
cert: fs.readFileSync(opts.cert || __dirname + "/ssl/server.crt"),
|
||||
key: fs.readFileSync(opts.key || __dirname + "/ssl/server.key")
|
||||
}
|
||||
|
||||
this.server = express();
|
||||
|
||||
this.server.https = https.createServer(options, this.server);
|
||||
if (opts.ssl) {
|
||||
var options = {
|
||||
cert: fs.readFileSync(opts.cert || __dirname + "/ssl/server.crt"),
|
||||
key: fs.readFileSync(opts.key || __dirname + "/ssl/server.key")
|
||||
}
|
||||
|
||||
this.server.https = https.createServer(options, this.server);
|
||||
} else {
|
||||
this.server.https = this.server;
|
||||
}
|
||||
|
||||
this.server.set('title', 'Cylon API Server');
|
||||
|
||||
|
@ -62,6 +66,9 @@ namespace("Cylon", function() {
|
|||
var title = self.server.get('title');
|
||||
Logger.info(title + " is now online.");
|
||||
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.");
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -49,7 +49,8 @@ var Cylon = (function() {
|
|||
cert: null,
|
||||
key: null,
|
||||
auth: {},
|
||||
CORS: null
|
||||
CORS: null,
|
||||
ssl: false
|
||||
};
|
||||
|
||||
this.robot = bind(this.robot, this);
|
||||
|
@ -101,7 +102,7 @@ var Cylon = (function() {
|
|||
Master.prototype.api = function(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++) {
|
||||
var key = keys[i];
|
||||
|
|
|
@ -13,7 +13,8 @@ describe("API", function() {
|
|||
stub(https, 'createServer').returns({ listen: spy() });
|
||||
|
||||
opts = {
|
||||
master: { name: 'master' }
|
||||
master: { name: 'master' },
|
||||
ssl: true
|
||||
}
|
||||
|
||||
api = new API(opts);
|
||||
|
|
Loading…
Reference in New Issue