Skip to content

Enable running iodocs under https #36

Closed
wants to merge 9 commits into from
View
79 app.js
@@ -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);
@@ -277,6 +316,8 @@ function processRequest(req, res, next) {
var reqQuery = req.body,
params = reqQuery.params || {},
+ content = reqQuery.requestContent || '',
+ contentType = reqQuery.contentType || '',
methodURL = reqQuery.methodUri,
httpMethod = reqQuery.httpMethod,
apiKey = reqQuery.apiKey,
@@ -315,13 +356,9 @@ function processRequest(req, res, next) {
host: baseHostUrl,
port: baseHostPort,
method: httpMethod,
- path: apiConfig.publicPath + methodURL// + ((paramString.length > 0) ? '?' + paramString : "")
+ path: apiConfig.publicPath + methodURL + ((paramString.length > 0) ? '?' + paramString : "")
};
- if (['POST','DELETE','PUT'].indexOf(httpMethod) !== -1) {
- var requestBody = query.stringify(params);
- }
-
if (apiConfig.oauth) {
console.log('Using OAuth');
@@ -472,9 +509,7 @@ function processRequest(req, res, next) {
function unsecuredCall() {
console.log('Unsecured Call');
- if (['POST','PUT','DELETE'].indexOf(httpMethod) === -1) {
- options.path += ((paramString.length > 0) ? '?' + paramString : "");
- }
+ options.path += ((paramString.length > 0) ? '?' + paramString : "");
// Add API Key to params, if any.
if (apiKey != '' && apiKey != 'undefined' && apiKey != undefined) {
@@ -522,18 +557,10 @@ function processRequest(req, res, next) {
options.headers = headers;
}
- if (!options.headers['Content-Length']) {
- if (requestBody) {
- options.headers['Content-Length'] = requestBody.length;
- }
- else {
- options.headers['Content-Length'] = 0;
- }
+ if (content.length > 0) {
+ options.headers['Content-Length'] = content.length;
}
- if (requestBody) {
- options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
- }
if (config.debug) {
console.log(util.inspect(options));
@@ -548,6 +575,12 @@ function processRequest(req, res, next) {
console.log('Protocol: HTTP');
doRequest = http.request;
}
+ if(contentType !== ''){
+ if (config.debug) {
+ console.log('Setting Content-Type: ' + contentType);
+ }
+ options.headers['Content-Type'] = contentType;
+ }
// API Call. response is the response from the API, res is the response we will send back to the user.
var apiCall = doRequest(options, function(response) {
@@ -602,12 +635,10 @@ function processRequest(req, res, next) {
};
});
- if (requestBody) {
- apiCall.end(requestBody, 'utf-8');
- }
- else {
- apiCall.end();
+ if(content !== ''){
+ apiCall.write(content,'utf-8');
}
+ apiCall.end();
}
}
View
7 config.json.sample
@@ -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"
+ }
}
View
11 views/api.jade
@@ -152,5 +152,16 @@ ul
a(href='#', class='remove') Remove
a(href='#', class='add-headers') Add Header
// Create header fields and button to add/remove headers.
+ - if (method.content)
+ div.content
+ h4.title
+ div.indicator
+ span Content
+ div.fields
+ select(name='contentType')
+ - each choice in method.content.contentType
+ option(value=choice) #{choice}
+ textarea(columns='80', rows='10', name='requestContent', placeholder='{}')
+ a(href='#', class='remove') Remove
- if (!method['read-only'])
input(type='submit', id=method.MethodName, value='Try it!')
Something went wrong with that request. Please try again.