Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variable in ocelot config file

I have multiple microservices which are accessible by clients through Ocelot gateway. Inside configuration file there are properties to specify downstream host and port. This have to be done for EACH route.

The problem is that if the service's hostname or port changes, I will have to modify every single route associated with this particular service.

So, the question is, Is it possible to introduce ENV variable inside ocelot.json configuration file? In that case I will have to modify only one ENV variable and all associated routes will be affected.

Here is my current configuration file (I'm using docker-compose so service names are used as hosts):

"Routes": [
    {
      "UpstreamPathTemplate": "/api/v1/signIn",
      "DownstreamPathTemplate": "/api/v1/signIn",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "identity-api",
          "Port": 80
        }
      ],
      "SwaggerKey": "Identity"
    },
    {
      "UpstreamPathTemplate": "/api/v1/validate",
      "DownstreamPathTemplate": "/api/v1/validate",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "identity-api",
          "Port": 80
        }
      ],
      "SwaggerKey": "Identity"
    },

What I want:

"Routes": [
    {
      "UpstreamPathTemplate": "/api/v1/signIn",
      "DownstreamPathTemplate": "/api/v1/signIn",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": {SERVICE_HOST},
          "Port": {SERVICE_PORT}
        }
      ],
      "SwaggerKey": "Identity"
    },
    {
      "UpstreamPathTemplate": "/api/v1/validate",
      "DownstreamPathTemplate": "/api/v1/validate",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": {SERVICE_HOST},
          "Port": {SERVICE_PORT}
        }
      ],
      "SwaggerKey": "Identity"
    },
like image 439
Konstantin Chsherbakov Avatar asked Jan 24 '23 19:01

Konstantin Chsherbakov


1 Answers

I was able to implement this missing functionality with PostConfigure extension method. In my case I prefer to put placeholders configuration into Ocelot.json but you can change the code to look in IConfiguration rather using GlobalHosts

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.Configuration.File;

public static class FileConfigurationExtensions
{
    public static IServiceCollection ConfigureDownstreamHostAndPortsPlaceholders(
        this IServiceCollection services,
        IConfiguration configuration)
    {
        services.PostConfigure<FileConfiguration>(fileConfiguration =>
        {
            var globalHosts = configuration
                .GetSection($"{nameof(FileConfiguration.GlobalConfiguration)}:Hosts")
                .Get<GlobalHosts>();

            foreach (var route in fileConfiguration.Routes)
            {
                ConfigureRote(route, globalHosts);
            }
        });

        return services;
    }

    private static void ConfigureRote(FileRoute route, GlobalHosts globalHosts)
    {
        foreach (var hostAndPort in route.DownstreamHostAndPorts)
        {
            var host = hostAndPort.Host;
            if (host.StartsWith("{") && host.EndsWith("}"))
            {
                var placeHolder = host.TrimStart('{').TrimEnd('}');
                if (globalHosts.TryGetValue(placeHolder, out var uri))
                {
                    route.DownstreamScheme = uri.Scheme;
                    hostAndPort.Host = uri.Host;
                    hostAndPort.Port = uri.Port;
                }
            }
        }
    }
}

The GlobalHosts class:

public class GlobalHosts : Dictionary<string, Uri> { }

The usage:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddOcelot();
    services.ConfigureDownstreamHostAndPortsPlaceholders(Configuration);
}

The Ocelot.json

{
  "Routes": [
    {
      "UpstreamPathTemplate": "/my-resource",
      "UpstreamHttpMethod": [ "POST" ],
      "DownstreamPathTemplate": "/my/resource",
      "DownstreamHostAndPorts": [
        {
          "Host": "{MyService}"
        }
      ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:5000",
    "Hosts": {
      "MyService": "http://localhost:3999"
    }
  }
} 
like image 98
Artur Avatar answered May 28 '23 12:05

Artur