-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvConfigFilter.cs
More file actions
45 lines (38 loc) · 1.59 KB
/
Copy pathEnvConfigFilter.cs
File metadata and controls
45 lines (38 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Yarp.ReverseProxy.Configuration;
public class EnvConfigFilter : IProxyConfigFilter
{
private readonly Regex _envVarPattern = new("\\{\\{(\\w+)\\}\\}");
public ValueTask<ClusterConfig> ConfigureClusterAsync(ClusterConfig origCluster, CancellationToken cancel)
{
var newDests = new Dictionary<string, DestinationConfig>(StringComparer.OrdinalIgnoreCase);
foreach (var dest in origCluster.Destinations)
{
var origAddress = dest.Value.Address;
if (_envVarPattern.IsMatch(origAddress))
{
var envVarName = _envVarPattern.Match(origAddress).Groups[1].Value;
var envValue = Environment.GetEnvironmentVariable(envVarName);
if (string.IsNullOrWhiteSpace(envValue))
{
throw new ArgumentException($"Environment variable '{envVarName}' not found for destination '{dest.Key}'.");
}
var updatedDest = dest.Value with { Address = envValue };
newDests.Add(dest.Key, updatedDest);
}
else
{
newDests.Add(dest.Key, dest.Value);
}
}
return new ValueTask<ClusterConfig>(origCluster with { Destinations = newDests });
}
public ValueTask<RouteConfig> ConfigureRouteAsync(RouteConfig route, ClusterConfig cluster, CancellationToken cancel)
{
return new ValueTask<RouteConfig>(route);
}
}