Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions lib/fittings/on_swagger_router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
'use strict';

//var debug = require('debug')('swagger:swagger_router');
var path = require('path');
var assert = require('assert');
var SWAGGER_ROUTER_CONTROLLER = 'x-swagger-router-controller';
var util = require('util');

module.exports = function create(fittingDef, bagpipes) {

//debug('config: %j', fittingDef);

assert(Array.isArray(fittingDef.controllersDirs), 'controllersDirs must be an array');
assert(Array.isArray(fittingDef.mockControllersDirs), 'mockControllersDirs must be an array');

var swaggerNodeRunner = bagpipes.config.swaggerNodeRunner;
var appRoot = swaggerNodeRunner.config.swagger.appRoot;

var mockMode = !!fittingDef.mockMode || !!swaggerNodeRunner.config.swagger.mockMode;

var controllersDirs = mockMode ? fittingDef.mockControllersDirs : fittingDef.controllersDirs;

controllersDirs = controllersDirs.map(function(dir) {
return path.resolve(appRoot, dir);
});

var controllerFunctionsCache = {};

return function swagger_router(context, cb) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identifier 'swagger_router' is not in camel case.

//debug('exec');

var operation = context.request.swagger.operation;
var controllerName = operation[SWAGGER_ROUTER_CONTROLLER] || operation.pathObject[SWAGGER_ROUTER_CONTROLLER];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.


var controller;

if (controllerName in controllerFunctionsCache) {

//debug('controller in cache', controllerName);
controller = controllerFunctionsCache[controllerName];

} else {

//debug('loading controller %s from fs: %s', controllerName, controllersDirs);
for (var i = 0; i < controllersDirs.length; i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected use of '++'.

var controllerPath = path.resolve(controllersDirs[i], controllerName);
try {
controller = require(controllerPath);
controllerFunctionsCache[controllerName] = controller;
//debug('controller found', controllerPath);
break;
} catch (err) {
if (!mockMode && i === controllersDirs.length - 1) {
return cb(err);
}
//debug('controller not in', controllerPath);
}
}
}

if (controller) {

var operationId = operation.definition['operationId'] || context.request.method.toLowerCase();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

['operationId'] is better written in dot notation.

var controllerFunction = controller[operationId];

if (controllerFunction && typeof controllerFunction === 'function') {
//debug('running controller');
return controllerFunction(context.request, context.response, cb);
}

var msg = util.format('Controller %s doesn\'t export handler function %s', controllerName, operationId);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

if (mockMode) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty block.

//debug(msg);
} else {
return cb(new Error(msg));
}
}

if (mockMode) {

var statusCode = parseInt(context.request.get('_mockreturnstatus')) || 200;

var mimetype = context.request.get('accept') || 'application/json';
var mock = operation.getResponseExample(statusCode, mimetype);

if (mock) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty block.

//debug('returning mock example value', mock);
} else {
mock = operation.getResponseSample(statusCode);
//debug('returning mock sample value', mock);
}

context.headers['Content-Type'] = mimetype;
context.statusCode = statusCode;

return cb(null, mock);
}

// for completeness, we should never actually get here
cb(new Error(util.format('No controller found for %s in %j', controllerName, controllersDirs)));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

};
4 changes: 2 additions & 2 deletions swagger_config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# values in the swagger hash are system configuration for swagger-node
swagger:

fittingsDirs: [ api/fittings ]
fittingsDirs: [ lib/fittings ]
defaultPipe: null
swaggerControllerPipe: swagger_controllers # defines the standard processing pipe for controllers

Expand All @@ -12,7 +12,7 @@ swagger:
bagpipes:

_router:
name: swagger_router
name: on_swagger_router
mockMode: false
mockControllersDirs: [ lib/api/2.0/mocks lib/api/redfish-1.0/mocks ]
controllersDirs: [ lib/api/2.0, lib/api/redfish-1.0 ]
Expand Down