swagger-stats icon indicating copy to clipboard operation
swagger-stats copied to clipboard

how to use with openapi 3

Open DiV666 opened this issue 5 years ago • 0 comments

Hi,

I have created a simple app with oas-generator and in the index.js I have added swagger-stats but I can not access it. Should it be configured differently?

index.js

'use strict';

var fs = require('fs'),
    http = require('http'),
    path = require('path');

var express = require("express");
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json({
  strict: false
}));
var oasTools = require('oas-tools');

var jsyaml = require('js-yaml');
var serverPort = 8080;

var spec = fs.readFileSync(path.join(__dirname, '/api/oas-doc.yaml'), 'utf8');
var oasDoc = jsyaml.safeLoad(spec);

const swaggerStats = require('swagger-stats');
app.use(
    swaggerStats.getMiddleware({
        swaggerSpec: oasDoc,
        uriPath:'/stats'
    })
);

var options_object = {
  controllers: path.join(__dirname, './controllers'),
  loglevel: 'info',
  strict: false,
  router: true,
  validator: true
};

oasTools.configure(options_object);

oasTools.initialize(oasDoc, app, function() {
  http.createServer(app).listen(serverPort, function() {
    console.log("App running at http://localhost:" + serverPort);
    console.log("________________________________________________________________");
    if (options_object.docs !== false) {
      console.log('API docs (Swagger UI) available on http://localhost:' + serverPort + '/docs');
      console.log("________________________________________________________________");
    }
  });
});

app.get('/info', function(req, res) {
  res.send({
    info: "This API was generated using oas-generator!",
    name: oasDoc.info.title
  });
});

oas-doc.yaml

openapi: 3.0.0
info:
  description: The scaffolding API
  version: 0.0.0
  title: Scaffolding API
  termsOfService: 'http://example.com'
  contact:
    email: [email protected]
  license:
    name: No license
    url: 'https://choosealicense.com/no-permission/'
tags:
  - name: Scaffolding
    description: REST API that allows you to do a scaffolding
paths:
  '/v1/health':
    get:
      operationId: getHealthStatus
      tags:
        - Health
      summary: Get the current system status
      description: Get the current system status
      responses:
        '200':
          description: Current system status description
          content:
            text/plain:
              schema:
                $ref: '#/components/schemas/Status'
servers:
  - url: /scaffolding/api
components:
  schemas:
    Status:
      required:
        - status
      properties:
        status:
          type: string

DiV666 avatar May 16 '19 09:05 DiV666