Skip to content

how to write transforms value in class  #2329

@dantemayson

Description

@dantemayson

hi,I want to write the equivalent of appsetting in the form of code. The only problem I have is that I don't know how to write the value of transforms in the form of code.
appsettings :

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ReverseProxy": {
    "Routes": {
      "route1": {
        "ClusterId": "cluster1",
        "CorsPolicy": "customPolicy",
        "Match": {
          "Path": "/api/{**catch-all}"
        },
        "Transforms": [
          { "PathPattern": "{**catch-all}" }
        ]
      },
      "route2": {
        "ClusterId": "cluster2",
        "CorsPolicy": "customPolicy",
        "Match": {
          "Path": "/mvc/{**catch-all}"
        },
        "Transforms": [
          { "PathPattern": "{**catch-all}" }
        ]
      }
    },
    "Clusters": {
      "cluster1": {
        "Destinations": {
          "destination1": {
            "Address": "http://host.docker.internal:5167"
          } 
        }
      },
      "cluster2": {
        "Destinations": {
          "destination1": {
            "Address": "http://host.docker.internal:5199"
          }
        }
      }
    }
  }
}

Programmatic Configuration

    public class YarpProxyConfig : IProxyConfig
    {
        readonly List<RouteConfig> _routes;
        readonly List<ClusterConfig> _clusters;
        readonly CancellationChangeToken _changeToken;
        readonly CancellationTokenSource _cts = new CancellationTokenSource();
        public YarpProxyConfig()
        {
            _routes = GenerateRoutes();
            _clusters = GenerateClusters();
            _cts = new CancellationTokenSource();
            _changeToken = new CancellationChangeToken(_cts.Token);
        }
        public IReadOnlyList<RouteConfig> Routes => _routes;
        public IReadOnlyList<ClusterConfig> Clusters => _clusters;
        public IChangeToken ChangeToken => _changeToken;

        private List<ClusterConfig> GenerateClusters()
        {
            var collection = new List<ClusterConfig>();
            collection.Add(new ClusterConfig()
            {
                ClusterId = "FirstCluster",
                Destinations = new Dictionary<string, DestinationConfig>{
            {
                "server", new DestinationConfig()
                {
                    Address = "http://localhost:5167"
                }
            }
        }
            });
            return collection;
        }

        private List<RouteConfig> GenerateRoutes()
        {
            var collection = new List<RouteConfig>();
            collection.Add(new RouteConfig()
            {
                RouteId="Route1",
                ClusterId = "FirstCluster",
                Match = new RouteMatch()
                {
                    Path = "/api/{**catch-all}"
                },
                Transforms= //? == "PathPattern": "{**catch-all}" 
            });
            return collection;
        }
    }

The equivalent of this code in C#

  "Transforms": [
           { "PathPattern": "{**catch-all}" }
         ]

Transforms= ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: IdeaThis issue is a high-level idea for discussion.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions