Burgyn / MMLib.SwaggerForOcelot

This repo contains swagger extension for ocelot.
MIT License
353 stars 94 forks source link

Help with Error Code: UnableToFindDownstreamRouteError Message: Failed to match ReRoute configuration for upstream path #99

Closed rsanhuez closed 4 years ago

rsanhuez commented 4 years ago

Hi. I am testing this package to use it in a project. I tried to follow the steps of the README and the example on https://github.com/Burgyn/MMLib.SwaggerForOcelot/tree/master/demo/ApiGatewayWithPath. For testing purposes I am connecting one API Gateway with one Microservice. The technologies used are:

When I try to display the swagger documentation of my Microservice (http://localhost:8888/swagger/v1/swagger.json) through my API Gateway (http://localhost:7777/gateway/swagger/index.html) I get the following error:

errorSwaggerOcelot

And in Visual Studio:

Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware: Warning: requestId: 80000028-0002-fd00-b63f-84710c7967bb, previousRequestId: no previous request id, message: DownstreamRouteFinderMiddleware setting pipeline errors. IDownstreamRouteFinder returned Error Code: UnableToFindDownstreamRouteError Message: Failed to match ReRoute configuration for upstream path: /swagger/docs/v1/transacciones, verb: GET.
Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware: Warning: requestId: 80000028-0002-fd00-b63f-84710c7967bb, previousRequestId: no previous request id, message: Failed to match ReRoute configuration for upstream path: /swagger/docs/v1/transacciones, verb: GET.
Ocelot.Responder.Middleware.ResponderMiddleware: Warning: requestId: 80000028-0002-fd00-b63f-84710c7967bb, previousRequestId: no previous request id, message: Error Code: UnableToFindDownstreamRouteError Message: Failed to match ReRoute configuration for upstream path: /swagger/docs/v1/transacciones, verb: GET. errors found in ResponderMiddleware. Setting error response for request path:/swagger/docs/v1/transacciones, request method: GET

Any help to solve this problem would be very appreciated. Note that Ocelot itself is working fine. For example I get the correct response from the API Gateway using this URL on the web browser: http://localhost:7777/api/transacciones/EstadoSolicitud

My simple ocelot.json is the following:

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 8888
        }
      ],
      "UpstreamPathTemplate": "/api/transacciones/{everything}",
      "UpstreamHttpMethod": [ "Get" ],
      "SwaggerKey": "transacciones"
    }
  ],
  "SwaggerEndPoints": [
    {
      "Key": "transacciones",
      "Config": [
        {
          "Name": "Transacciones v1",
          "Version": "v1",
          "Url": "http://localhost:8888/swagger/v1/swagger.json"
        }
      ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost"
  }
}

My Program.cs:


public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config
                    .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                            .AddJsonFile("appsettings.json", true, true)
                            .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
                            .AddJsonFile("ocelot.json")
                            .AddEnvironmentVariables();
                }).
            UseStartup<Startup>();
    }

And my Startup.cs:


public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            /*
             * Code for setting ActiveMQ ...
             */

            services.AddOcelot();
            services.AddSwaggerForOcelot(Configuration);
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
            services.AddSingleton<IEstadoSolicitudApi>(new EstadoSolicitudApi("http://localhost:8888"));

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UsePathBase("/gateway");
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseStaticFiles();      
            app.UseSwaggerForOcelotUI(Configuration, opt =>
            {
                opt.DownstreamSwaggerEndPointBasePath = "/gateway/swagger/docs";
                opt.PathToSwaggerGenerator = "/swagger/doc";
            }).UseOcelot().Wait();
        }
    }
Burgyn commented 4 years ago

Hi,

try to remove the next block of code:

opt.DownstreamSwaggerEndPointBasePath = "/gateway/swagger/docs";
opt.PathToSwaggerGenerator = "/swagger/doc";

use only app.UseSwaggerForOcelotUI(Configuration)

you can try new prerelease version 2.0.0-alpha.4

rsanhuez commented 4 years ago

It worked perfectly. Thanks!. Now I am going to add some custom controllers to the API Gateway and see If I am able to display them along with the Ocelot's endpoints.

Burgyn commented 4 years ago

I am glad to hear that.