Exemplo n.º 1
0
    /**
     * Creates and endpoint to publish the swagger documentation.
     * @param router Express router
     * @param options Options for swagger endpoint
     */
    public static swagger(router: express.Router, options?: SwaggerOptions) {
        const swaggerUi = require('swagger-ui-express');
        options = Server.getOptions(options);

        const swaggerDocument: any = Server.loadSwaggerDocument(options);

        if (options.host) {
            swaggerDocument.host = options.host;
        }
        if (options.schemes) {
            swaggerDocument.schemes = options.schemes;
        }

        router.get(path.posix.join('/', options.endpoint, 'json'), (req, res, next) => {
            res.send(swaggerDocument);
        });
        router.get(path.posix.join('/', options.endpoint, 'yaml'), (req, res, next) => {
            res.set('Content-Type', 'text/vnd.yaml');
            res.send(YAML.stringify(swaggerDocument, 1000));
        });
        router.use(path.posix.join('/', options.endpoint), swaggerUi.serve, swaggerUi.setup(swaggerDocument, options.swaggerUiOptions));
    }
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(methodOverride())

RegisterRoutes(app)
app.get(`/`, (req, res) => {
  res.redirect(`${basePath}swagger`)
})
app.use(`${basePath}healthz`, (_, res) => {
  res.send({
    status: 'Healthy!'
  })
})
app.use(`${basePath}swagger.json`, express.static(__dirname + './swagger.json'))
app.use(`${basePath}swagger`, swaggerUI.serve, swaggerUI.setup(swaggerJSON))

function startServer() {
  app.listen(process.env.PORT || 5007, () => {
    console.info(`App's running at http://localhost:${process.env.PORT || 5007}`)
    console.info('Press CTRL-C to stop\n')
  })
}

mongoose.connection.once('open', function() {
  console.info(`Connected to ${mongoDbUri}.`)
  app.emit('ready')
})

app.on('ready', function() {
  startServer()
// controllers need to be referenced in order to get crawled by the generator
import './controllers/healthcheckController'
import './controllers/statusController'

const app = express()

app.use('/swagger.json', (req, res) => {
  res.sendFile(path.join(__dirname, 'swagger.json'))
})

let options = {
  swaggerUrl: `/swagger.json`
}

app.use('/docs', swaggerUi.serve, swaggerUi.setup(null, options))

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(methodOverride())

RegisterRoutes(app)

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  let err = new NotFoundError()
  next(err)
})

// error handlers
app.use(function (err, req, res, next) {