GithubHelp home page GithubHelp logo

motycak / mmlib.swaggerforocelot Goto Github PK

View Code? Open in Web Editor NEW

This project forked from burgyn/mmlib.swaggerforocelot

0.0 1.0 0.0 360 KB

This repo contains swagger extension for ocelot.

License: MIT License

C# 100.00%

mmlib.swaggerforocelot's Introduction

MMLib.SwaggerForOcelot Build status

SwaggerForOcelot combines two amazing projects Swashbuckle.AspNetCore and Ocelot. Allows you to view and use swagger documentation for downstream services directly through the Ocelot project.

Direct via http://ocelotprojecturl:port/swagger provides documentation for downstream services configured in ocelot.json. Additionally, the addresses are modified to match the UpstreamPathTemplate from the configuration.

SwaggerForOcelot

Get Started

  1. Configure SwaggerGen in your downstream services.

    Follow the SwashbuckleAspNetCore documentation.

  2. Install Nuget package into yout ASP.NET Core Ocelot project.

    dotnet add package MMLib.SwaggerForOcelot

  3. Configure SwaggerForOcelot in ocelot.json.
 {
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5100
        }
      ],
      "UpstreamPathTemplate": "/api/contacts/{everything}",
      "UpstreamHttpMethod": [ "Get" ],
      "SwaggerKey": "contacts"
    },
    {
      "DownstreamPathTemplate": "/api/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5200
        }
      ],
      "UpstreamPathTemplate": "/api/orders/{everything}",
      "UpstreamHttpMethod": [ "Get" ],
      "SwaggerKey": "orders"
    }
  ],
  "SwaggerEndPoints": [
    {
      "Key": "contacts",
      "Config": [
        {
          "Name": "Contacts API",
          "Version": "v1",
          "Url": "http://localhost:5100/swagger/v1/swagger.json"
        }
      ]
    },
    {
      "Key": "orders",
      "Config": [
        {
          "Name": "Orders API",
          "Version": "v0.9",
          "Url": "http://localhost:5200/swagger/v0.9/swagger.json"
        },
        {
          "Name": "Orders API",
          "Version": "v1",
          "Url": "http://localhost:5200/swagger/v1/swagger.json"
        },
        {
          "Name": "Orders API",
          "Version": "v2",
          "Url": "http://localhost:5200/swagger/v2/swagger.json"
        },
        {
          "Name": "Orders API",
          "Version": "v3",
          "Url": "http://localhost:5200/swagger/v3/swagger.json"
        }
      ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost"
  }
}

SwaggerEndPoint is configuration for downstream service swagger generator endpoint. Property Key is used to pair with the ReRoute configuration. Name is displayed in the combobox. Url is downstream service swagger generator endpoint.

  1. In the ConfigureServices method of Startup.cs, register the SwaggerForOcelot generator.
    services.AddSwaggerForOcelot(Configuration);
  2. In Configure method, insert the SwaggerForOcelot middleware to expose interactive documentation.
           app.UseSwaggerForOcelotUI(Configuration, opt => {
                 opt.PathToSwaggerGenerator = "/swagger/docs";
             })
    You can optionally include headers that your Ocelot Gateway will send when requesting a swagger endpoint. This can be especially useful if your downstream microservices require contents from a header to authenticate.
         app.UseSwaggerForOcelotUI(Configuration, opt => {
             opts.DownstreamSwaggerHeaders = new[]
               {
                   new KeyValuePair<string, string>("Auth-Key", "AuthValue"),
               };
         })
    After swagger for ocelot transforms the downstream swagger to the upstream swagger, you have the ability to alter the upstream swagger if you need to by setting the ReConfigureUpstreamSwaggerJson option or ReConfigureUpstreamSwaggerJsonAsync option for async methods.
        public string AlterUpstreamSwaggerJson(HttpContext context, string swaggerJson)
        {
             var swagger = JObject.Parse(swaggerJson);
             // ... alter upstream json
             return swagger.ToString(Formatting.Indented);
        }
    
        app.UseSwaggerForOcelotUI(Configuration, opt => {
            opts.ReConfigureUpstreamSwaggerJson = AlterUpstreamSwaggerJson;
        })
  3. Show your microservices interactive documentation.

    http://ocelotserviceurl/swagger

Virtual directory

If you have a downstream service hosted in the virtual directory, you probably have a DownstreamPathTemplate starting with the name of this virtual directory /virtualdirectory/api/{everything}. In order to properly replace the paths, it is necessary to set the property route "Virtualdirectory":"/virtualdirectory".

Example:

 {
  "DownstreamPathTemplate": "/project/api/{everything}",
  "DownstreamScheme": "http",
  "DownstreamHostAndPorts": [
      {
      "Host": "localhost",
      "Port": 5100
      }
  ],
  "UpstreamPathTemplate": "/api/project/{everything}",
  "UpstreamHttpMethod": [ "Get" ],
  "SwaggerKey": "project",
  "VirtualDirectory":"/project"
}

ToDo:

  • Design.
  • Create demo project.
  • Add swagger endpoints into SwagerUI combobox.
  • Create extension for adding middleware into pipeline.
  • Create SwaggerForOcelotMiddleware and replace downstream path.
  • Add capability for confiuragtion SwaggerUI and configure EndpointBasePath.
  • Unit tests
  • Complex Ocelot configuration.
    • Add Petstore swagger Example.
    • Group more reroutes with same SwaggerKey.
    • Filter UpstreamHttpMethod.
    • Query parameters.
    • Aggregates.
  • Documentations
  • Continuous delivery
  • Custom Index.html.

mmlib.swaggerforocelot's People

Contributors

burgyn avatar duncan-g avatar felpasl avatar jkosters avatar arsslensoft avatar danadesrosiers avatar jondmcelroy avatar mikado3780 avatar satano avatar ussamoo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.