question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to handle non-root gateway?

See original GitHub issue

I have a question regarding the handling of swagger generation when the Ocelot gateway endpoint is not on root. Using the following ocelot.json:

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/v1/settings/{everything}",
      "DownstreamScheme": "http",
      "ServiceName": "backendpreferences",
      "UpstreamPathTemplate": "/api/v1/settings/{everything}",
      "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "Bearer",
        "AllowedScopes": []
      },
      "SwaggerKey": "settings"
    }
  ],
  "SwaggerEndPoints": [
    {
      "Key": "settings",
      "TransformByOcelotConfig": true,
      "Config": [
        {
          "Name": "My Settings API",
          "Version": "v1",
          "Service": {
            "Name": "backendpreferences",
            "Path": "/swagger/v1/swagger.json"
          }
        }
      ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://myapi.com/mygateway",
    "RequestIdKey": "OcRequestId",
    "AdministrationPath": "/administration",
    "UseServiceDiscovery": true,
    "ServiceDiscoveryProvider": {
      "Host": "consul",
      "Port": 8500,
      "Type": "Consul"
    }
  }

In code the SwaggerForOcelot setup is very basic:

app.UseSwaggerForOcelotUI(opt =>
{
    opt.PathToSwaggerGenerator = "/swagger/docs";
});

Ocelot itself works fine with this setup.

However, whenever we open the swagger documentation at https://myapi.com/mygateway/swagger/index.html the page starts loading but it tries to load the subdocuments at https://myapi.com/swagger/docs rather than the expected https://myapi.com/mygateway/swagger/docs. This causes the page to not load fully and show a fetch error.

Is there a way to set the base url which SwaggerForOcelot should use for the gateway (or how to make it use the Ocelot baseUrl)?

We’ve tried something like opt.DownstreamSwaggerEndPointBasePath = "../swagger/docs"; as suggested in issue #113, and while this solved the loading of documents, it doesn’t fix the urls in the generated document itself. Those still go for the root and therefore the calls fail with a 404: afbeelding

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
leonluc-devcommented, Oct 19, 2021

We had set the base path in ocelot.json. But adding it in code using UsePathBase seems to have solved the issue.

0reactions
liamharper2453commented, Jul 24, 2023

@alex-tselikovsky None of the above worked for me.

Had to do something like:

    private string AlterUpstreamSwaggerJson(HttpContext context, string swaggerJson)
    {
        swaggerJson = swaggerJson.Replace("\"/v", "\"/api-gateway/v");
        var swagger = JObject.Parse(swaggerJson);
        return swagger.ToString(Formatting.Indented);
    }

    app.UseSwaggerForOcelotUI(opt =>
    {
      opt.ReConfigureUpstreamSwaggerJson = env.IsDevelopment() ? default : AlterUpstreamSwaggerJson;
      opt.DownstreamSwaggerEndPointBasePath = "../swagger/docs";
    });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Running HTTP Gateway processes as a non-root user
When running HTTP Gateway process as a non-root user, you cannot run ascp as another user by configuring ascpconfig.run_as_user and ascpconfig.run_as_group ...
Read more >
Running HTTP Gateway Processes as a Non-Root User
You can run HTTP Gateway as a non-root user.
Read more >
Run API Gateway as non-root on UNIX/Linux
A solution to this problem is to run the API Gateway as a non-root user, but still allow the API Gateway process to...
Read more >
Running DMZ Gateway as non-root user in Linux
Create a non-root user account on the server (be sure to set up a home folder for that user account as well). Establish...
Read more >
Running API Gateway as non-root procedure mixes ...
Running API Gateway as non-root procedure mixes Axway libraries with OS libraries. The procedure described to run API Gtw as non-root user.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found