- remove duplicate ITenantRepository/TenantRepository from Console - extend Platform ITenantRepository with GetByIdAsync, GetPagedAsync, CountAsync - update Console services to use Platform.Infrastructure.Repositories - fix nullable warnings (UserDto, OAuthClientService) - fix YarpGateway Directory.Build.props duplicate import - fix DynamicProxyConfigProvider CS8618 warning
79 lines
2.3 KiB
C#
79 lines
2.3 KiB
C#
using Microsoft.Extensions.Primitives;
|
|
using Yarp.ReverseProxy.Configuration;
|
|
using YarpGateway.Config;
|
|
|
|
namespace YarpGateway.DynamicProxy;
|
|
|
|
public class DynamicProxyConfigProvider : IProxyConfigProvider
|
|
{
|
|
private volatile IProxyConfig _config;
|
|
private readonly DatabaseRouteConfigProvider _routeProvider;
|
|
private readonly DatabaseClusterConfigProvider _clusterProvider;
|
|
private readonly object _lock = new();
|
|
private CancellationTokenSource? _cts;
|
|
|
|
public DynamicProxyConfigProvider(
|
|
DatabaseRouteConfigProvider routeProvider,
|
|
DatabaseClusterConfigProvider clusterProvider)
|
|
{
|
|
_routeProvider = routeProvider;
|
|
_clusterProvider = clusterProvider;
|
|
_cts = new CancellationTokenSource();
|
|
_config = null!;
|
|
UpdateConfig();
|
|
}
|
|
|
|
public IProxyConfig GetConfig()
|
|
{
|
|
return _config;
|
|
}
|
|
|
|
public void UpdateConfig()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
_cts?.Cancel();
|
|
_cts = new CancellationTokenSource();
|
|
|
|
var routes = _routeProvider.GetRoutes();
|
|
var clusters = _clusterProvider.GetClusters();
|
|
|
|
_config = new InMemoryProxyConfig(
|
|
routes,
|
|
clusters,
|
|
Array.Empty<IReadOnlyDictionary<string, string>>(),
|
|
_cts.Token
|
|
);
|
|
}
|
|
}
|
|
|
|
public async Task ReloadAsync()
|
|
{
|
|
await _routeProvider.ReloadAsync();
|
|
await _clusterProvider.ReloadAsync();
|
|
UpdateConfig();
|
|
}
|
|
|
|
private class InMemoryProxyConfig : IProxyConfig
|
|
{
|
|
private readonly CancellationChangeToken _changeToken;
|
|
|
|
public InMemoryProxyConfig(
|
|
IReadOnlyList<RouteConfig> routes,
|
|
IReadOnlyList<ClusterConfig> clusters,
|
|
IReadOnlyList<IReadOnlyDictionary<string, string>> transforms,
|
|
CancellationToken token)
|
|
{
|
|
Routes = routes;
|
|
Clusters = clusters;
|
|
Transforms = transforms;
|
|
_changeToken = new CancellationChangeToken(token);
|
|
}
|
|
|
|
public IReadOnlyList<RouteConfig> Routes { get; }
|
|
public IReadOnlyList<ClusterConfig> Clusters { get; }
|
|
public IReadOnlyList<IReadOnlyDictionary<string, string>> Transforms { get; }
|
|
public IChangeToken ChangeToken => _changeToken;
|
|
}
|
|
}
|