Skip to content

Commit

Permalink
Added more direct routing APIs (#2147)
Browse files Browse the repository at this point in the history
* Added more direct routing APIs
- Added helpers to make direct routing more easily accessible.
- Updated the sample to use them.

* Dedupe more
  • Loading branch information
davidfowl committed May 25, 2023
1 parent b9ee824 commit 5529aca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions samples/ReverseProxy.Direct.Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public void Configure(IApplicationBuilder app, IHttpForwarder forwarder)
}
});
endpoints.MapForwarder("/sample/{id}", "https://httpbin.org", "/anything/{id}");
endpoints.MapForwarder("/sample/anything/{id}", "https://httpbin.org", b => b.AddPathRemovePrefix("/sample"));
// When using extension methods for registering IHttpForwarder providing configuration, transforms, and HttpMessageInvoker is optional (defaults will be used).
endpoints.MapForwarder("/{**catch-all}", "https://example.com", requestOptions, transformer, httpClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Yarp.ReverseProxy.Forwarder;
using Yarp.ReverseProxy.Transforms;
using Yarp.ReverseProxy.Transforms.Builder;

namespace Microsoft.AspNetCore.Builder;

Expand All @@ -22,6 +24,42 @@ public static IEndpointConventionBuilder MapForwarder(this IEndpointRouteBuilder
return endpoints.MapForwarder(pattern, destinationPrefix, ForwarderRequestConfig.Empty);
}

/// <summary>
/// Adds direct forwarding of HTTP requests that match the specified pattern to a specific destination and target path applying route values from the pattern using default configuration for the outgoing request, and default HTTP client.
/// </summary>
public static IEndpointConventionBuilder MapForwarder(this IEndpointRouteBuilder endpoints, string pattern, string destinationPrefix, string targetPath)
{
return endpoints.MapForwarder(pattern, destinationPrefix, ForwarderRequestConfig.Empty, targetPath);
}

/// <summary>
/// Adds direct forwarding of HTTP requests that match the specified pattern to a specific destination and target path applying route values from the pattern using customized configuration for the outgoing request, and default HTTP client.
/// </summary>
public static IEndpointConventionBuilder MapForwarder(this IEndpointRouteBuilder endpoints, string pattern, string destinationPrefix, ForwarderRequestConfig requestConfig, string targetPath)
{
return endpoints.MapForwarder(pattern, destinationPrefix, requestConfig, b => b.AddPathRouteValues(targetPath));
}

/// <summary>
/// Adds direct forwarding of HTTP requests that match the specified pattern to a specific destination using default configuration for the outgoing request, customized transforms, and default HTTP client.
/// </summary>
public static IEndpointConventionBuilder MapForwarder(this IEndpointRouteBuilder endpoints, string pattern, string destinationPrefix, Action<TransformBuilderContext> configureTransform)
{
return endpoints.MapForwarder(pattern, destinationPrefix, ForwarderRequestConfig.Empty, configureTransform);
}

/// <summary>
/// Adds direct forwarding of HTTP requests that match the specified pattern to a specific destination using customized configuration for the outgoing request, customized transforms, and default HTTP client.
/// </summary>
public static IEndpointConventionBuilder MapForwarder(this IEndpointRouteBuilder endpoints, string pattern, string destinationPrefix, ForwarderRequestConfig requestConfig, Action<TransformBuilderContext> configureTransform)
{
var transformBuilder = endpoints.ServiceProvider.GetRequiredService<ITransformBuilder>();

var transformer = transformBuilder.Create(configureTransform);

return endpoints.MapForwarder(pattern, destinationPrefix, requestConfig, transformer);
}

/// <summary>
/// Adds direct forwarding of HTTP requests that match the specified pattern to a specific destination using customized configuration for the outgoing request, default transforms, and default HTTP client.
/// </summary>
Expand Down

0 comments on commit 5529aca

Please sign in to comment.