Refactoring api optional ssl logic
This commit is contained in:
parent
224287744a
commit
394691a8f2
41
lib/api.js
41
lib/api.js
|
@ -24,32 +24,28 @@ namespace("Cylon", function() {
|
||||||
|
|
||||||
if (opts == null) { opts = {}; }
|
if (opts == null) { opts = {}; }
|
||||||
|
|
||||||
this.opts = opts;
|
this.opts = opts;
|
||||||
this.host = opts.host || "127.0.0.1";
|
this.host = opts.host || "127.0.0.1";
|
||||||
this.port = opts.port || "3000";
|
this.port = opts.port || "3000";
|
||||||
|
this.ssl = opts.ssl == false ? false : {};
|
||||||
if (opts.ssl == false) {
|
|
||||||
this.ssl = false;
|
|
||||||
} else {
|
|
||||||
this.ssl = opts.ssl || {key: __dirname + "/ssl/server.key", cert: __dirname + "/ssl/server.crt"};
|
|
||||||
}
|
|
||||||
|
|
||||||
this.master = opts.master;
|
this.master = opts.master;
|
||||||
this.server = express();
|
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 = {
|
var options = {
|
||||||
cert: fs.readFileSync(this.ssl.cert),
|
key: fs.readFileSync(this.ssl.key || __dirname + "/ssl/server.key"),
|
||||||
key: fs.readFileSync(this.ssl.key)
|
cert: fs.readFileSync(this.ssl.cert || __dirname + "/ssl/server.crt")
|
||||||
}
|
};
|
||||||
|
|
||||||
this.server.node = https.createServer(options, this.server);
|
this.server.node = https.createServer(options, this.server);
|
||||||
} else {
|
} else {
|
||||||
|
Logger.warn("API using insecure connection. We recommend using an SSL certificate with Cylon.")
|
||||||
this.server.node = this.server;
|
this.server.node = this.server;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.server.set('title', 'Cylon API Server');
|
|
||||||
|
|
||||||
// configure basic auth, if requested
|
// configure basic auth, if requested
|
||||||
if (opts.auth && opts.auth.type && opts.auth.type === 'basic') {
|
if (opts.auth && opts.auth.type && opts.auth.type === 'basic') {
|
||||||
var user = opts.auth.user,
|
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.json());
|
||||||
this.server.use(express.urlencoded());
|
this.server.use(express.urlencoded());
|
||||||
this.server.use(express["static"](__dirname + "/../node_modules/robeaux/"));
|
this.server.use(express["static"](__dirname + "/../node_modules/robeaux/"));
|
||||||
|
@ -68,15 +65,11 @@ namespace("Cylon", function() {
|
||||||
ApiServer.prototype.listen = function() {
|
ApiServer.prototype.listen = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.server.node.listen(this.port, this.host, null, function() {
|
this.server.node.listen(this.port, this.host, null, function() {
|
||||||
var title = self.server.get('title');
|
var title = self.server.get('title');
|
||||||
Logger.info(title + " is now online.");
|
var protocol = self.ssl ? "https" : "http";
|
||||||
|
|
||||||
if(self.opts.ssl == false) {
|
Logger.info(title + " is now online.");
|
||||||
Logger.info("Listening at http://" + self.host + ":" + self.port);
|
Logger.info("Listening at " + protocol + "://" + 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);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ var Cylon = (function() {
|
||||||
port: '3000',
|
port: '3000',
|
||||||
auth: {},
|
auth: {},
|
||||||
CORS: null,
|
CORS: null,
|
||||||
ssl: null
|
ssl: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.robot = bind(this.robot, this);
|
this.robot = bind(this.robot, this);
|
||||||
|
|
Loading…
Reference in New Issue