Concerns
Concerns allow to add behavior to a section of your web application by intercepting requests and responses. The framework allows any handler to be extended by using concerns. The following example will add a custom HTTP header to every response generated by the server:
using GenHTTP.Api.Content;
using GenHTTP.Api.Protocol;
using GenHTTP.Engine.Internal;
using GenHTTP.Modules.IO;
using GenHTTP.Modules.Layouting;
public class CustomConcern : IConcern
{
public IHandler Content { get; }
public CustomConcern(IHandler content)
{
Content = content;
}
public ValueTask PrepareAsync() => Content.PrepareAsync();
public async ValueTask<IResponse?> HandleAsync(IRequest request)
{
var response = await Content.HandleAsync(request);
if (response != null)
{
response.Headers.Add("X-Custom-Header", "Custom Concern");
}
return response;
}
}
public class CustomConcernBuilder : IConcernBuilder
{
public IConcern Build(IHandler content)
{
return new CustomConcern(content);
}
}
var handler = Layout.Create()
.Index(Content.From(Resource.FromString("Hello World")))
.Add(new CustomConcernBuilder());
await Host.Create()
.Handler(handler)
.RunAsync();
The GenHTTP SDK uses the same mechanism to achieve functionality such as compression or support for CORS.