mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-02 10:08:39 +00:00
Add base class for middlewares to enforce consistency
This commit is contained in:
parent
08be90aadc
commit
e998e59607
3 changed files with 26 additions and 15 deletions
|
@ -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;
|
||||||
|
|
||||||
|
|
18
ProjectLighthouse/Middlewares/Middleware.cs
Normal file
18
ProjectLighthouse/Middlewares/Middleware.cs
Normal 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);
|
||||||
|
}
|
|
@ -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();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue