Skip to content

Commit

Permalink
Adding swagger-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
chdanielmueller committed Jun 6, 2015
1 parent 5b4c1eb commit ebf2d72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
23 changes: 17 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var doctrine = require('doctrine'),
express = require('express'),
fs = require('fs'),
jsYaml = require('js-yaml'),
path = require('path');
path = require('path'),
swaggerTools = require('swagger-tools');

// This is the Swagger object that conforms to the Swagger 2.0 specification.
module.exports.swaggerObject = {
Expand Down Expand Up @@ -48,12 +49,22 @@ module.exports.init = function (app, options) {
parseApiFile(options.apis[i]);
}

// Attach to Express routes.
app.get(options.swaggerJsonPath, function (req, res) {
res.json(module.exports.swaggerObject);
});
// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(module.exports.swaggerObject, function (middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());

// Validate Swagger requests
app.use(middleware.swaggerValidator());

// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));

app.use(options.swaggerUiPath, express.static(options.swaggerUiDir));
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi());


});
};

// ---------------------------------------------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"homepage": "https://github.com/devlouisc/jsdoc-express-with-swagger#readme",
"dependencies": {
"doctrine": "^0.6.4",
"js-yaml": "^3.3.1"
"js-yaml": "^3.3.1",
"swagger-tools": "^0.8.7"
},
"devDependencies": {
"eslint": "^0.22.1",
Expand Down

0 comments on commit ebf2d72

Please sign in to comment.