-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProxiesJsonFileConfigProvider.cs
More file actions
59 lines (44 loc) · 1.66 KB
/
ProxiesJsonFileConfigProvider.cs
File metadata and controls
59 lines (44 loc) · 1.66 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using Yarp.ReverseProxy.Configuration;
namespace AppServiceProxy.Configuration;
internal class ProxiesJsonFileConfigProvider : IProxyConfigProvider
{
private readonly PhysicalFileProvider _fileProvider = new(s_wwwroot)
{
UseActivePolling = true,
UsePollingFileWatcher = true
};
private const string ProxiesJsonFileName = "proxies.json";
private static readonly string s_wwwroot = Environment.ExpandEnvironmentVariables(@"%HOME%\site\wwwroot");
public IProxyConfig GetConfig()
{
var proxiesJsonFile = Path.Combine(s_wwwroot, ProxiesJsonFileName);
var changeToken = _fileProvider.Watch(ProxiesJsonFileName);
return new ProxiesJsonFileConfig(proxiesJsonFile, changeToken);
}
private class ProxiesJsonFileConfig : IProxyConfig
{
public ProxiesJsonFileConfig(string proxiesJsonFile, IChangeToken changeToken)
{
try
{
var json = File.ReadAllText(proxiesJsonFile);
var proxies = ProxiesJsonReader.ParseJson(json);
var (routes, clusters) = ProxiesJsonTransform.Apply(proxies);
Routes = routes;
Clusters = clusters;
}
catch
{
Routes = Array.Empty<RouteConfig>();
Clusters = Array.Empty<ClusterConfig>();
}
ChangeToken = changeToken;
}
public IReadOnlyList<RouteConfig> Routes { get; }
public IReadOnlyList<ClusterConfig> Clusters { get; }
public IChangeToken ChangeToken { get; }
}
}