Support for Feature Management libraries
Endpoints can now be setup to execute a FeatureFlag for every Http request that comes in, which allows an endpoint to be conditionally available according to some evaluation logic.
To create a feature flag, implement the interface IFeatureFlag and simply return true from the IsEnabledAsync() handler method if the endpoint is to be accessible to that particular request.
sealed class BetaTestersOnly : IFeatureFlag
{
public async Task<bool> IsEnabledAsync(IEndpoint endpoint)
{
//use whatever mechanism/library you like to determine if this endpoint is enabled for the current request.
if (endpoint.HttpContext.Request.Headers.TryGetValue("x-beta-tester", out _))
return true; // return true to enable
//this is optional. if you don't send anything, a 404 is sent automatically.
await endpoint.HttpContext.Response.SendErrorsAsync([new("featureDisabled", "You are not a beta tester!")]);
return false; // return false to disable
}
}
Attach it to the endpoint like so:
sealed class BetaEndpoint : EndpointWithoutRequest<string>
{
public override void Configure()
{
Get("beta");
FeatureFlag<BetaTestersOnly>();
}
public override async Task HandleAsync(CancellationToken c)
{
await Send.OkAsync("this is the beta!");
}
}