dotnet / dotnet/yarp

Feature request: Configuration-driven proxy filtering

Open
#2,165 3 comments 0 reactions 1 assignee Claimed by @bradygaster View on GitHub
Type: Idea
Dominant language
C#
Stars
9.6k
Forks
933
Avg merge
12d 18h
Merged PRs (30d)
2

Description

In most of the scenarios where I've used YARP, I've found it useful to copy in a class I borrowed from another sample created during the `Project Tye` era that would read in values from `IConfiguration` to be pumped into the route and cluster endpoint values. In an example by Shayne Boyer, which I've mimicked in the `CustomConfigFilter` [class I created for the Build 2023 cloud native .NET demo code](https://github.com/bradygaster/dotnet-cloud-native-build-2023/blob/main/ContosoOnline/Proxy/CustomConfigFilter.cs), a configuration-driven filter would give developers a conventional way of mapping environment variables to placeholders in their YARP configuration.

Take the `appsettings.json` code below, from the same Build 2023 demo repo:

```json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"System.Net.Http.HttpClient": "Warning",
"Yarp": "Warning"
}
},
"ORDERS_API": "http://localhost:5004",
"STORE_UI": "http://localhost:5176",
"ReverseProxy": {
"Clusters": {
"Frontend": {
"Destinations": {
"Store": {
"Address": "{{STORE_UI}}"
}
}
},
"OrdersApi": {
"Destinations": {
"Store": {
"Address": "{{ORDERS_API}}"
}
}
}
},
"Routes": {
"ClientRoute": {
"ClusterId": "Frontend",
"Match": {
"Path": "{**catch-all}"
}
},
"OrdersApiRoute": {
"ClusterId": "OrdersApi",
"Match": {
"Path": "/api/orders/{**catch-all}"
},
"Transforms": [
{ "PathPattern": "/api/orders/{**remainder}" },
{ "PathRemovePrefix": "/api" }
]
}
}
}
}
```

In this code, the `ORDERS_API` and `STORE_UI` values are hard-coded in the file as settings, so they'll be read in from here by default or from environment variables. The placeholders `{{ORDERS_API}}` and `{{STORE_UI}}` are replaced with the values in these settings or environment variables at runtime, making it easy for developers to use environment variables to route traffic between services. It also gives developers who want to integrate things like Tye with YARP to create new service discovery approaches.

I'll follow this issue up with a pull request to add the filter to the product's code.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.