diff --git a/lib/api.js b/lib/api.js index 7335e4d..cb0eb7f 100644 --- a/lib/api.js +++ b/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."); + } }); }; diff --git a/lib/cylon.js b/lib/cylon.js index edc2d04..0fc9536 100644 --- a/lib/cylon.js +++ b/lib/cylon.js @@ -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]; diff --git a/test/specs/api.spec.js b/test/specs/api.spec.js index 444b913..c63189c 100644 --- a/test/specs/api.spec.js +++ b/test/specs/api.spec.js @@ -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);