Refactoring api optional ssl logic

This commit is contained in:
Javier Cervantes 2014-04-24 11:18:20 -05:00
parent 224287744a
commit 394691a8f2
2 changed files with 18 additions and 25 deletions

View File

@ -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);
});
};

View File

@ -48,7 +48,7 @@ var Cylon = (function() {
port: '3000',
auth: {},
CORS: null,
ssl: null
ssl: {}
};
this.robot = bind(this.robot, this);