Ejemplo n.º 1
0
Swagger.prototype.setupSwagger = function(app) {
  this.swaggerInstance = swagger.createNew();

  app.set('x-powered-by', 'CASSANGO');

  app.use(function(req, res, next) { req.locals = {}; next(); }); // todo: is this needed?
  app.param('dbId', require('./util/paramHandlers/db-id-validator'));
  app.param('tableId', require('./util/paramHandlers/table-id-validator'));

  this.swaggerInstance.setAppHandler(app);

  this.addModels();

  this.addResources();

  // Add any custom headers here.
  this.swaggerInstance.setHeaders = function(res) {
    res.header('Content-Type', 'application/json; charset=utf-8');
  };

  this.swaggerInstance.configureSwaggerPaths('', '/api-docs', '');
  this.swaggerInstance.configure('/', 'v1');

  // Serve up swagger ui at /docs via static route
  var p = __dirname + '/swagger-ui/';

  var docsHandler = express.static(p);
  var swaggerIndexHtml;

  app.get('/', function(req, res) {
    res.redirect(302, '/docs/');
  });

  /* For the swagger root file, instead of pulling in a templating engine for 1 use case, we'll just do a simple
     string replace for now. Can always improve later.
  */
  app.get('/docs/', function(req, res) {
    if (/\/docs$/.test(req.url) === true) { // express static barfs on root url w/o trailing slash
      return res.redirect(302, req.url + '/');
    }

    if (!swaggerIndexHtml) {
      // block for documentation...
      swaggerIndexHtml = fs.readFileSync(path.resolve(__dirname, './swagger-ui/index.html'), 'utf8');
    }

    res.end(swaggerIndexHtml);
  });

  app.get('/docs*', function(req, res, next) {

    // take off leading /docs so that connect locates file correctly
    var url = '/docs';
    req.url = req.url.substr(url.length);

    return docsHandler(req, res, next);
  });
};
Ejemplo n.º 2
0
Mean.prototype.swagger = function () {
  var self = this
  var Swagger = require('swagger-node-express')
  var swaggerUI = require('swagger-ui')
  self.app.use('/api' + '/index.html', handleIndex)
  self.app.use('/api' + '/', handleIndex)
  self.app.use('/api', express.static(swaggerUI.dist))
  var html
  function handleIndex (req, res, next) {
    if (req.url !== '/' && req.url !== '/index.html') {
      return next()
    }
    if (req.originalUrl === '/api') {
      return res.redirect(301, '/api' + '/')
    }
    if (html) {
      return res.send(html)
    }
    fs.readFile(swaggerUI.dist + '/index.html', {
      encoding: 'utf8'
    }, function (err, data) {
      if (err) {
        console.error(err)
        return res.send(500)
      }
      html = data.replace('http://petstore.swagger.io/v2/swagger.json', '/api-docs')
      res.send(html)
    })
  }
  var swagger = Swagger.createNew(self.app)

  var paramTypes = swagger.paramTypes
  var sortParm = paramTypes.query('sort', 'Comma seperated list of params to sort by.  (e.g "-created,name") ', 'string')
  var limitParm = paramTypes.query('limit', 'Number of items to return', 'number')
  var skipParm = paramTypes.query('skip', 'Number of items to skip', 'number')

  var defaultGetParams = [
    sortParm,
    limitParm,
    skipParm
  ]

  var swaggerPath = path.resolve(self.dir, './server/swagger')
  if (!fs.existsSync(swaggerPath)) {
    throw new Error('Critical Folder Missing:')
  }
  var swaggerDir = _.filter(fs.readdirSync(swaggerPath), function (n) {
    return !_.startsWith(n, '.')
  })
  _.forEach(swaggerDir, function (n) {
    var model = require(path.join(self.dir, '/server/swagger/', n, '/models'))
    swagger.addModels(model)
    require(path.join(self.dir, '/server/swagger/', n, '/services'))
      .load(swagger, {
        searchableOptions: defaultGetParams
      })
  })
  swagger.configureSwaggerPaths('', '/api-docs', '')
  swagger.configureDeclaration('Meanstackjs', {
    description: 'Meanstackjs API',
    authorizations: [''],
    produces: ['application/json']
  })
  swagger.setApiInfo({
    title: 'Meanstackjs',
    description: 'Meanstackjs API',
    termsOfServiceUrl: 'http://meanstackjs.com',
    contact: '*****@*****.**',
    licenseUrl: 'http://en.wikipedia.org/wiki/MIT_License'
  })
  swagger.setAuthorizations({
    apiKey: {
      type: 'apiKey',
      passAs: 'header'
    }
  })
  swagger.configure('/api', '1.0')
}