
This commit splits the request pipeline conditionally instead of invoking every middleware in the stack. It also simplifies middleware instantiation by using runtime discovery, allowing for Plugins to add Middleware.
28 lines
No EOL
710 B
C#
28 lines
No EOL
710 B
C#
using System.Diagnostics;
|
|
using Iceshrimp.Backend.Core.Extensions;
|
|
using JetBrains.Annotations;
|
|
|
|
namespace Iceshrimp.Backend.Core.Middleware;
|
|
|
|
[UsedImplicitly]
|
|
public class RequestDurationMiddleware(RequestDelegate next)
|
|
{
|
|
[UsedImplicitly]
|
|
public async Task InvokeAsync(HttpContext ctx)
|
|
{
|
|
if (ctx.GetEndpoint()?.Metadata.GetMetadata<HideRequestDuration>() == null)
|
|
{
|
|
var pre = Stopwatch.GetTimestamp();
|
|
ctx.Response.OnStarting(() =>
|
|
{
|
|
var duration = Stopwatch.GetElapsedTime(pre).GetTotalMilliseconds();
|
|
ctx.Response.Headers.Append("X-Request-Duration", $"{duration} ms");
|
|
return Task.CompletedTask;
|
|
});
|
|
}
|
|
|
|
await next(ctx);
|
|
}
|
|
}
|
|
|
|
public class HideRequestDuration : Attribute; |