Add base class for middlewares to enforce consistency

This commit is contained in:
jvyden 2022-05-15 10:35:52 -04:00
commit e998e59607
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
3 changed files with 26 additions and 15 deletions

View file

@ -4,17 +4,14 @@ using Microsoft.AspNetCore.Http;
namespace LBPUnion.ProjectLighthouse.Middlewares; namespace LBPUnion.ProjectLighthouse.Middlewares;
public class FakeRemoteIPAddressMiddleware public class FakeRemoteIPAddressMiddleware : Middleware
{ {
private readonly IPAddress fakeIpAddress = IPAddress.Parse("127.0.0.1"); private readonly IPAddress fakeIpAddress = IPAddress.Parse("127.0.0.1");
private readonly RequestDelegate next;
public FakeRemoteIPAddressMiddleware(RequestDelegate next) public FakeRemoteIPAddressMiddleware(RequestDelegate next) : base(next)
{ {}
this.next = next;
}
public async Task Invoke(HttpContext httpContext) public override async Task InvokeAsync(HttpContext httpContext)
{ {
httpContext.Connection.RemoteIpAddress = this.fakeIpAddress; httpContext.Connection.RemoteIpAddress = this.fakeIpAddress;

View file

@ -0,0 +1,18 @@
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Http;
namespace LBPUnion.ProjectLighthouse.Middlewares;
public abstract class Middleware
{
private protected RequestDelegate next { get; init; }
protected Middleware(RequestDelegate next)
{
this.next = next;
}
[UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)]
public abstract Task InvokeAsync(HttpContext httpContext);
}

View file

@ -6,19 +6,15 @@ using Microsoft.AspNetCore.Http;
namespace LBPUnion.ProjectLighthouse.Middlewares; namespace LBPUnion.ProjectLighthouse.Middlewares;
public class RequestLogMiddleware public class RequestLogMiddleware : Middleware
{ {
private readonly RequestDelegate next; public RequestLogMiddleware(RequestDelegate next) : base(next)
{}
public RequestLogMiddleware(RequestDelegate next)
{
this.next = next;
}
// Logs every request and the response to it // Logs every request and the response to it
// Example: "200, 13ms: GET /LITTLEBIGPLANETPS3_XML/news" // Example: "200, 13ms: GET /LITTLEBIGPLANETPS3_XML/news"
// Example: "404, 127ms: GET /asdasd?query=osucookiezi727ppbluezenithtopplayhdhr" // Example: "404, 127ms: GET /asdasd?query=osucookiezi727ppbluezenithtopplayhdhr"
public async Task InvokeAsync(HttpContext context) public override async Task InvokeAsync(HttpContext context)
{ {
Stopwatch requestStopwatch = new(); Stopwatch requestStopwatch = new();
requestStopwatch.Start(); requestStopwatch.Start();