Skip to content

Commit

Permalink
rename appHandler
Browse files Browse the repository at this point in the history
add a getServer method
  • Loading branch information
RBLU committed Sep 25, 2014
1 parent 6ede697 commit 7b805ea
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Apps/petstore/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var app = express();
app.use(express.bodyParser());

// Set the main handler in swagger to the express app
swagger.setAppHandler(app);
swagger.setRestifyServer(app);

// This is a sample validator. It simply says that for _all_ POST, DELETE, PUT
// methods, the header `api_key` OR query param `api_key` must be equal
Expand Down
24 changes: 15 additions & 9 deletions Common/node/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var swaggerVersion = "1.1";
var apiVersion = "0.0";
var resources = {};
var validators = [];
var appHandler = null;
var restifyServer = null;
var allowedMethods = ['get', 'post', 'put', 'patch', 'delete'];
var allowedDataTypes = ['string', 'int', 'long', 'double', 'boolean', 'date', 'array'];
var params = require(__dirname + '/paramTypes.js');
Expand Down Expand Up @@ -55,10 +55,10 @@ function configureSwaggerPaths(format, path, suffix) {
function configure(bp, av) {
basePath = bp;
apiVersion = av;
setResourceListingPaths(appHandler);
setResourceListingPaths(restifyServer);

// add the GET for resource listing
appHandler.get(resourcePath.replace(formatString, jsonSuffix), resourceListing);
restifyServer.get(resourcePath.replace(formatString, jsonSuffix), resourceListing);
// update resources if already configured

_.forOwn(resources, function (resource) {
Expand Down Expand Up @@ -432,8 +432,13 @@ function addMethod(app, callback, spec) {

// Set expressjs app handler

function setAppHandler(app) {
appHandler = app;
function setRestifyServer(server) {
restifyServer = server;
}


function getRestifyServer() {
return restifyServer;
}

// Change error handler
Expand All @@ -448,7 +453,7 @@ function setErrorHandler(handler) {
function addHandlers(type, handlers) {
_.forOwn(handlers, function (handler) {
handler.spec.method = type;
addMethod(appHandler, handler.action, handler.spec);
addMethod(restifyServer, handler.action, handler.spec);
});
}

Expand All @@ -457,7 +462,7 @@ function addHandlers(type, handlers) {
function discover(resource) {
_.forOwn(resource, function (handler, key) {
if (handler.spec && handler.spec.method && allowedMethods.indexOf(handler.spec.method.toLowerCase()) > -1) {
addMethod(appHandler, handler.action, handler.spec);
addMethod(restifyServer, handler.action, handler.spec);
} else {
console.error('auto discover failed for: ' + key);
}
Expand All @@ -473,7 +478,7 @@ function discoverFile(file) {
// adds get handler

function addOperation(operationDoc) {
addMethod(appHandler, operationDoc.action,operationDoc.spec);
addMethod(restifyServer, operationDoc.action,operationDoc.spec);
}

// adds models to swagger
Expand Down Expand Up @@ -706,7 +711,8 @@ exports.resourceListing = resourceListing;
exports.setHeaders = setHeaders;
exports.addOperation = addOperation;
exports.addModels = addModels;
exports.setAppHandler = setAppHandler;
exports.setRestifyServer = setRestifyServer;
exports.getRestifyServer = getRestifyServer;
exports.setErrorHandler = setErrorHandler;
exports.errorHandler = errorHandler;
exports.discover = discover;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ restify.defaultResponseHeaders = function(data) {
this.header('Access-Control-Allow-Origin', '*');
};

swagger.setAppHandler(server);
swagger.setRestifyServer(server);
```

You can optionally add a validator function, which is used to filter the swagger json and request operations:
Expand Down
2 changes: 1 addition & 1 deletion docs/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h4>petData.js</h4>
<span class="kd">var</span> <span class="nx">petResources</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">&quot;./petResources.js&quot;</span><span class="p">);</span>

<span class="kd">var</span> <span class="nx">app</span> <span class="o">=</span> <span class="nx">express</span><span class="p">();</span>
<span class="nx">app</span><span class="p">.</span><span class="nx">use</span><span class="p">(</span><span class="nx">express</span><span class="p">.</span><span class="nx">bodyParser</span><span class="p">());</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>Set the main handler in swagger to the express app</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">swagger</span><span class="p">.</span><span class="nx">setAppHandler</span><span class="p">(</span><span class="nx">app</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>This is a sample validator. It simply says that for <em>all</em> POST
<span class="nx">app</span><span class="p">.</span><span class="nx">use</span><span class="p">(</span><span class="nx">express</span><span class="p">.</span><span class="nx">bodyParser</span><span class="p">());</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>Set the main handler in swagger to the express app</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">swagger</span><span class="p">.</span><span class="nx">setRestifyServer</span><span class="p">(</span><span class="nx">app</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>This is a sample validator. It simply says that for <em>all</em> POST
methods, the header <code>api_key</code> OR query param <code>api_key</code> must be equal
to the string literal <code>special-key</code>. All other HTTP ops are A-OK</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">swagger</span><span class="p">.</span><span class="nx">addValidator</span><span class="p">(</span>
<span class="kd">function</span> <span class="nx">validate</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">path</span><span class="p">,</span> <span class="nx">httpMethod</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>example, only allow POST for api_key="special-key"</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="s2">&quot;POST&quot;</span> <span class="o">==</span> <span class="nx">httpMethod</span><span class="p">)</span> <span class="p">{</span>
Expand Down
Loading

0 comments on commit 7b805ea

Please sign in to comment.