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

Minimal APIs for docs and samples #2327

Merged
merged 3 commits into from
Nov 16, 2023
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
46 changes: 20 additions & 26 deletions docs/docfx/articles/ab-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,28 @@ A/B testing and rolling upgrades require procedures for dynamically assigning in
## Example

```
public void Configure(IApplicationBuilder app, IProxyStateLookup lookup)
app.MapReverseProxy(proxyPipeline =>
{
// Custom cluster selection
proxyPipeline.Use((context, next) =>
{
app.UseRouting();
app.UseEndpoints(endpoints =>
context.RequestServices.GetRequiredService<IProxyStateLookup>();
if (lookup.TryGetCluster(ChooseCluster(context), out var cluster))
{
endpoints.MapReverseProxy(proxyPipeline =>
{
// Custom cluster selection
proxyPipeline.Use((context, next) =>
{
if (lookup.TryGetCluster(ChooseCluster(context), out var cluster))
{
context.ReassignProxyRequest(cluster);
}

return next();
});
proxyPipeline.UseSessionAffinity();
proxyPipeline.UseLoadBalancing();
});
});
}

private string ChooseCluster(HttpContext context)
{
// Decide which cluster to use. This could be random, weighted, based on headers, etc.
return Random.Shared.Next(2) == 1 ? "cluster1" : "cluster2";
}
context.ReassignProxyRequest(cluster);
}

return next();
});
proxyPipeline.UseSessionAffinity();
proxyPipeline.UseLoadBalancing();
});

private string ChooseCluster(HttpContext context)
{
// Decide which cluster to use. This could be random, weighted, based on headers, etc.
return Random.Shared.Next(2) == 1 ? "cluster1" : "cluster2";
}
```

## Usage
Expand Down
29 changes: 9 additions & 20 deletions docs/docfx/articles/authn-authz.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,22 @@ Example:

[Authorization policies](https://docs.microsoft.com/aspnet/core/security/authorization/policies) are an ASP.NET Core concept that the proxy utilizes. The proxy provides the above configuration to specify a policy per route and the rest is handled by existing ASP.NET Core authentication and authorization components.

Authorization policies can be configured in Startup.ConfigureServices as follows:
Authorization policies can be configured in the application as follows:
```
public void ConfigureServices(IServiceCollection services)
services.AddAuthorization(options =>
{
services.AddAuthorization(options =>
{
options.AddPolicy("customPolicy", policy =>
policy.RequireAuthenticatedUser());
});
}
options.AddPolicy("customPolicy", policy =>
policy.RequireAuthenticatedUser());
});
```

In Startup.Configure add the Authorization and Authentication middleware between Routing and Endpoints.
In Program.cs add the Authorization and Authentication middleware.

```
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapReverseProxy();
});
}
app.MapReverseProxy();
```

See the [Authentication](https://docs.microsoft.com/aspnet/core/security/authentication/) docs for setting up your preferred kind of authentication.
Expand Down
36 changes: 13 additions & 23 deletions docs/docfx/articles/config-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,24 @@
The reverse proxy can load configuration for routes and clusters from files using the IConfiguration abstraction from Microsoft.Extensions. The examples given here use JSON, but any IConfiguration source should work. The configuration will also be updated without restarting the proxy if the source file changes.

## Loading Configuration
To load the proxy configuration from IConfiguration add the following code in Startup:
To load the proxy configuration from IConfiguration add the following code in Program.cs:
```c#
public IConfiguration Configuration { get; }
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
var builder = WebApplication.CreateBuilder(args);

public void ConfigureServices(IServiceCollection services)
{
services.AddReverseProxy()
.LoadFromConfig(Configuration.GetSection("ReverseProxy"));
}
// Add the reverse proxy capability to the server
builder.Services.AddReverseProxy()
// Initialize the reverse proxy from the "ReverseProxy" section of configuration
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
var app = builder.Build();

app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapReverseProxy();
});
}
// Register the reverse proxy routes
app.MapReverseProxy();

app.Run();
```
**Note**: For details about middleware ordering see [here](https://docs.microsoft.com/aspnet/core/fundamentals/middleware/#middleware-order).

Expand Down
1 change: 0 additions & 1 deletion docs/docfx/articles/config-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ As of 1.1, YARP supports loading the proxy configuration from multiple sources.
```
or
```

services.AddReverseProxy()
.LoadFromMemory(routes, clusters)
.LoadFromConfig(Configuration.GetSection("ReverseProxy"));
Expand Down
27 changes: 8 additions & 19 deletions docs/docfx/articles/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,23 @@ Example:

[CORS policies](https://docs.microsoft.com/aspnet/core/security/cors#cors-with-named-policy-and-middleware) are an ASP.NET Core concept that the proxy utilizes. The proxy provides the above configuration to specify a policy per route and the rest is handled by existing ASP.NET Core CORS Middleware.

CORS policies can be configured in Startup.ConfigureServices as follows:
CORS policies can be configured in the application as follows:
```
public void ConfigureServices(IServiceCollection services)
services.AddCors(options =>
{
services.AddCors(options =>
options.AddPolicy("customPolicy", builder =>
{
options.AddPolicy("customPolicy", builder =>
{
builder.AllowAnyOrigin();
});
builder.AllowAnyOrigin();
});
}
});
```

In Startup.Configure add the CORS middleware between Routing and Endpoints.
Then add the CORS middleware.

```
public void Configure(IApplicationBuilder app)
{
app.UseRouting();

app.UseCors();
app.UseCors();

app.UseEndpoints(endpoints =>
{
endpoints.MapReverseProxy();
});
}
app.MapReverseProxy();
```


Expand Down
Loading
Loading