diff --git a/lib/api.js b/lib/api.js index 8996241..6e5ef84 100644 --- a/lib/api.js +++ b/lib/api.js @@ -24,32 +24,28 @@ namespace("Cylon", function() { if (opts == null) { opts = {}; } - this.opts = opts; - this.host = opts.host || "127.0.0.1"; - this.port = opts.port || "3000"; - - if (opts.ssl == false) { - this.ssl = false; - } else { - this.ssl = opts.ssl || {key: __dirname + "/ssl/server.key", cert: __dirname + "/ssl/server.crt"}; - } - + this.opts = opts; + this.host = opts.host || "127.0.0.1"; + this.port = opts.port || "3000"; + this.ssl = opts.ssl == false ? false : {}; this.master = opts.master; this.server = express(); - if (!!this.ssl.cert && !!this.ssl.key) { + //configure ssl if requested + if (this.ssl && typeof(this.ssl) === 'object') { + var https = require('https'); + var options = { - cert: fs.readFileSync(this.ssl.cert), - key: fs.readFileSync(this.ssl.key) - } + key: fs.readFileSync(this.ssl.key || __dirname + "/ssl/server.key"), + cert: fs.readFileSync(this.ssl.cert || __dirname + "/ssl/server.crt") + }; 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; } - this.server.set('title', 'Cylon API Server'); - // configure basic auth, if requested if (opts.auth && opts.auth.type && opts.auth.type === 'basic') { var user = opts.auth.user, @@ -60,6 +56,7 @@ namespace("Cylon", function() { } } + 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/")); @@ -68,15 +65,11 @@ namespace("Cylon", function() { ApiServer.prototype.listen = function() { var self = this; this.server.node.listen(this.port, this.host, null, function() { - var title = self.server.get('title'); - Logger.info(title + " is now online."); + var title = self.server.get('title'); + var protocol = self.ssl ? "https" : "http"; - if(self.opts.ssl == false) { - Logger.info("Listening at http://" + self.host + ":" + self.port); - Logger.warn("API using insecure connection. We recommend using an SSL certificate with Cylon."); - } else { - Logger.info("Listening at https://" + self.host + ":" + self.port); - } + Logger.info(title + " is now online."); + Logger.info("Listening at " + protocol + "://" + self.host + ":" + self.port); }); }; diff --git a/lib/cylon.js b/lib/cylon.js index 009c052..8d64a4a 100644 --- a/lib/cylon.js +++ b/lib/cylon.js @@ -48,7 +48,7 @@ var Cylon = (function() { port: '3000', auth: {}, CORS: null, - ssl: null + ssl: {} }; this.robot = bind(this.robot, this);