From e998e59607ae7b34a479540f1b91c7ced46624f9 Mon Sep 17 00:00:00 2001 From: jvyden Date: Sun, 15 May 2022 10:35:52 -0400 Subject: [PATCH] Add base class for middlewares to enforce consistency --- .../FakeRemoteIPAddressMiddleware.cs | 11 ++++------- ProjectLighthouse/Middlewares/Middleware.cs | 18 ++++++++++++++++++ .../Middlewares/RequestLogMiddleware.cs | 12 ++++-------- 3 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 ProjectLighthouse/Middlewares/Middleware.cs diff --git a/ProjectLighthouse/Middlewares/FakeRemoteIPAddressMiddleware.cs b/ProjectLighthouse/Middlewares/FakeRemoteIPAddressMiddleware.cs index 2858fa2d..aedfb747 100644 --- a/ProjectLighthouse/Middlewares/FakeRemoteIPAddressMiddleware.cs +++ b/ProjectLighthouse/Middlewares/FakeRemoteIPAddressMiddleware.cs @@ -4,17 +4,14 @@ using Microsoft.AspNetCore.Http; namespace LBPUnion.ProjectLighthouse.Middlewares; -public class FakeRemoteIPAddressMiddleware +public class FakeRemoteIPAddressMiddleware : Middleware { private readonly IPAddress fakeIpAddress = IPAddress.Parse("127.0.0.1"); - private readonly RequestDelegate next; - public FakeRemoteIPAddressMiddleware(RequestDelegate next) - { - this.next = next; - } + public FakeRemoteIPAddressMiddleware(RequestDelegate next) : base(next) + {} - public async Task Invoke(HttpContext httpContext) + public override async Task InvokeAsync(HttpContext httpContext) { httpContext.Connection.RemoteIpAddress = this.fakeIpAddress; diff --git a/ProjectLighthouse/Middlewares/Middleware.cs b/ProjectLighthouse/Middlewares/Middleware.cs new file mode 100644 index 00000000..bcfedda9 --- /dev/null +++ b/ProjectLighthouse/Middlewares/Middleware.cs @@ -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); +} \ No newline at end of file diff --git a/ProjectLighthouse/Middlewares/RequestLogMiddleware.cs b/ProjectLighthouse/Middlewares/RequestLogMiddleware.cs index dfadfc9e..25361a00 100644 --- a/ProjectLighthouse/Middlewares/RequestLogMiddleware.cs +++ b/ProjectLighthouse/Middlewares/RequestLogMiddleware.cs @@ -6,19 +6,15 @@ using Microsoft.AspNetCore.Http; namespace LBPUnion.ProjectLighthouse.Middlewares; -public class RequestLogMiddleware +public class RequestLogMiddleware : Middleware { - private readonly RequestDelegate next; - - public RequestLogMiddleware(RequestDelegate next) - { - this.next = next; - } + public RequestLogMiddleware(RequestDelegate next) : base(next) + {} // Logs every request and the response to it // Example: "200, 13ms: GET /LITTLEBIGPLANETPS3_XML/news" // Example: "404, 127ms: GET /asdasd?query=osucookiezi727ppbluezenithtopplayhdhr" - public async Task InvokeAsync(HttpContext context) + public override async Task InvokeAsync(HttpContext context) { Stopwatch requestStopwatch = new(); requestStopwatch.Start();