Permalink
Browse files

Enable iodocs to run as https.

Add options to config.json so the user may specifiy a
key.pem and cert.pem file. In addition, enable a boolean toggle
for https mode.

All the options are in config.https:
* on: boolean
* keyPath: path to key.pem file
* certPath: path to cer.pem file
  • Loading branch information...
1 parent 0c01913 commit 084d5f1a1ae15a5efcb275a44c6781db64040bf0 Bill Humphries committed Jul 17, 2012
Showing with 46 additions and 2 deletions.
  1. +40 −1 app.js
  2. +6 −1 config.json.sample
View
@@ -78,7 +78,46 @@ fs.readFile('public/data/apiconfig.json', 'utf-8', function(err, data) {
}
});
-var app = module.exports = express.createServer();
+//
+// Determine if we should launch as http/s and get keys and certs if needed
+//
+
+var app, httpsKey, httpsCert;
+
+if (config.https && config.https.on && config.https.keyPath && config.https.certPath) {
+ console.log("Starting secure server (https)");
+
+ // try reading the key and cert files, die if that fails
+ try {
+ httpsKey = fs.readFileSync(config.https.keyPath);
+ }
+ catch (err) {
+ console.error("Failed to read https key", config.https.keyPath);
+ console.log(err);
+ process.exit(1);
+ }
+ try {
+ httpsCert = fs.readFileSync(config.https.certPath);
+ }
+ catch (err) {
+ console.error("Failed to read https cert", config.https.certPath);
+ console.log(err);
+ process.exit(1);
+ }
+
+ app = module.exports = express.createServer({
+ key: httpsKey,
+ cert: httpsCert
+ });
+
+}
+else if (config.https && config.https.on) {
+ console.error("No key or certificate specified.");
+ process.exit(1);
+}
+else {
+ app = module.exports = express.createServer();
+}
if (process.env.REDISTOGO_URL) {
var rtg = require("url").parse(process.env.REDISTOGO_URL);
View
@@ -9,5 +9,10 @@
"port" : 6379,
"password" : "",
"database" : "0"
- }
+ },
+ "https" : {
+ "on": false,
+ "keyPath": "/Path/to/my.hostname.key.pem",
+ "certPath": "/Path/to/my.hostname.crt.pem"
+ }
}

0 comments on commit 084d5f1

Please sign in to comment.