Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for adding metadata to proxy routes #328

Merged
9 commits merged into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion samples/ReverseProxy.Config.Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.ReverseProxy.Middleware;
Expand Down Expand Up @@ -64,7 +65,8 @@ public void Configure(IApplicationBuilder app)
proxyPipeline.UseProxyLoadBalancing();
proxyPipeline.UseRequestAffinitizer();
proxyPipeline.UsePassiveHealthChecks();
});
})
.ConfigureEndpoints((builder, route) => builder.WithDisplayName($"ReverseProxy {route.RouteId}-{route.ClusterId}"));
});
}
}
Expand Down
19 changes: 0 additions & 19 deletions src/ReverseProxy/Abstractions/Config/IProxyConfigManager.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ internal static class IReverseProxyBuilderExtensions
public static IReverseProxyBuilder AddConfigBuilder(this IReverseProxyBuilder builder)
{
builder.Services.TryAddSingleton<IConfigValidator, ConfigValidator>();
builder.Services.TryAddSingleton<IRuntimeRouteBuilder, RuntimeRouteBuilder>();
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<MatcherPolicy, HeaderMatcherPolicy>());
return builder;
}
Expand All @@ -40,7 +39,7 @@ public static IReverseProxyBuilder AddRuntimeStateManagers(this IReverseProxyBui

public static IReverseProxyBuilder AddConfigManager(this IReverseProxyBuilder builder)
{
builder.Services.TryAddSingleton<IProxyConfigManager, ProxyConfigManager>();
builder.Services.TryAddSingleton<ProxyConfigManager>();
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.ReverseProxy;
using Microsoft.ReverseProxy.Configuration;
using Microsoft.ReverseProxy.Configuration.DependencyInjection;
using Microsoft.ReverseProxy.Service;
Expand Down Expand Up @@ -45,6 +46,8 @@ public static IReverseProxyBuilder AddReverseProxy(this IServiceCollection servi
.AddLoadBalancingPolicies()
.AddProxy();

services.TryAddSingleton<ProxyEndpointFactory>();

services.AddDataProtection();
services.AddAuthorization();
services.AddCors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// Licensed under the MIT License.

using System;
using System.Linq;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.ReverseProxy.Abstractions;
using Microsoft.ReverseProxy;
using Microsoft.ReverseProxy.Middleware;
using Microsoft.ReverseProxy.Service;
using Microsoft.ReverseProxy.Service.Management;

namespace Microsoft.AspNetCore.Builder
{
Expand Down Expand Up @@ -34,7 +35,7 @@ public static void MapReverseProxy(this IEndpointRouteBuilder endpoints)
/// Adds Reverse Proxy routes to the route table with the customized processing pipeline. The pipeline includes
/// by default the initialization step and the final proxy step, but not LoadBalancingMiddleware or other intermediate components.
/// </summary>
public static void MapReverseProxy(this IEndpointRouteBuilder endpoints, Action<IApplicationBuilder> configureApp)
public static ReverseProxyConventionBuilder MapReverseProxy(this IEndpointRouteBuilder endpoints, Action<IApplicationBuilder> configureApp)
{
if (endpoints is null)
{
Expand All @@ -51,16 +52,27 @@ public static void MapReverseProxy(this IEndpointRouteBuilder endpoints, Action<
appBuilder.UseMiddleware<ProxyInvokerMiddleware>();
var app = appBuilder.Build();

var routeBuilder = endpoints.ServiceProvider.GetRequiredService<IRuntimeRouteBuilder>();
routeBuilder.SetProxyPipeline(app);
var proxyEndpointFactory = endpoints.ServiceProvider.GetRequiredService<ProxyEndpointFactory>();
proxyEndpointFactory.SetProxyPipeline(app);

var configManager = endpoints.ServiceProvider.GetRequiredService<IProxyConfigManager>();
return GetOrCreateDataSource(endpoints).DefaultBuilder;
}

private static ProxyConfigManager GetOrCreateDataSource(IEndpointRouteBuilder endpoints)
{
var dataSource = endpoints.DataSources.OfType<ProxyConfigManager>().FirstOrDefault();
if (dataSource == null)
{
dataSource = endpoints.ServiceProvider.GetRequiredService<ProxyConfigManager>();
endpoints.DataSources.Add(dataSource);

// Config validation is async but startup is sync. We want this to block so that A) any validation errors can prevent
// the app from starting, and B) so that all the config is ready before the server starts accepting requests.
// Reloads will be async.
dataSource.InitialLoadAsync().GetAwaiter().GetResult();
}

// Config validation is async but startup is sync. We want this to block so that A) any validation errors can prevent
// the app from starting, and B) so that all the config is ready before the server starts accepting requests.
// Reloads will be async.
var dataSource = configManager.InitialLoadAsync().GetAwaiter().GetResult();
endpoints.DataSources.Add(dataSource);
return dataSource;
}
}
}
131 changes: 0 additions & 131 deletions src/ReverseProxy/Service/Config/RuntimeRouteBuilder.cs

This file was deleted.

Loading