From 71f2af285b8f77fdde68475e3b43c8407165c9a1 Mon Sep 17 00:00:00 2001 From: CWX Date: Sat, 18 Jan 2025 13:09:17 +0000 Subject: [PATCH] use prim ctor and fixed warnings: HttpCallbacks & HttpServer --- Core/Callbacks/HttpCallbacks.cs | 16 +++----- Core/Servers/HttpServer.cs | 69 +++++++++++++-------------------- 2 files changed, 31 insertions(+), 54 deletions(-) diff --git a/Core/Callbacks/HttpCallbacks.cs b/Core/Callbacks/HttpCallbacks.cs index 5f48814e..737b51b2 100644 --- a/Core/Callbacks/HttpCallbacks.cs +++ b/Core/Callbacks/HttpCallbacks.cs @@ -6,20 +6,14 @@ using Core.Servers; namespace Core.Callbacks; [Injectable(InjectionType.Singleton, InjectableTypeOverride = typeof(OnLoad), TypePriority = OnLoadOrder.HttpCallbacks)] -public class HttpCallbacks : OnLoad +public class HttpCallbacks(HttpServer _httpServer, ApplicationContext _applicationContext) : OnLoad { - protected HttpServer _httpServer; - protected ApplicationContext _applicationContext; - public HttpCallbacks(HttpServer httpServer, ApplicationContext applicationContext) + public Task OnLoad() { - _httpServer = httpServer; - _applicationContext = applicationContext; - } - - public async Task OnLoad() - { - _httpServer.Load( _applicationContext.GetLatestValue(ContextVariableType.APP_BUILDER).GetValue()); + _httpServer.Load( _applicationContext.GetLatestValue(ContextVariableType.APP_BUILDER)?.GetValue()); _applicationContext.ClearValues(ContextVariableType.APP_BUILDER); + + return Task.CompletedTask; } public string GetRoute() diff --git a/Core/Servers/HttpServer.cs b/Core/Servers/HttpServer.cs index 98330ded..6950a4e3 100644 --- a/Core/Servers/HttpServer.cs +++ b/Core/Servers/HttpServer.cs @@ -1,10 +1,8 @@ -using System.Net.WebSockets; -using Core.Context; +using Core.Context; using Core.Servers.Http; using Core.Services; using Microsoft.Extensions.Primitives; using Core.Annotations; -using Core.Models.Enums; using Core.Models.Spt.Config; using Core.Models.Utils; @@ -12,52 +10,35 @@ using Core.Models.Utils; namespace Core.Servers; [Injectable(InjectionType.Singleton)] -public class HttpServer +public class HttpServer( + ISptLogger _logger, + LocalisationService _localisationService, + ConfigServer _configServer, + ApplicationContext _applicationContext, + WebSocketServer _webSocketServer, + IEnumerable _httpListeners +) { - protected HttpConfig httpConfig; - protected bool started; + private readonly HttpConfig _httpConfig = _configServer.GetConfig(); + private bool started; - protected ISptLogger _logger; - protected LocalisationService _localisationService; - protected ConfigServer _configServer; - protected ApplicationContext _applicationContext; - protected WebSocketServer _webSocketServer; - protected IEnumerable _httpListeners; - - public HttpServer( - ISptLogger logger, - LocalisationService localisationService, - ConfigServer configServer, - ApplicationContext applicationContext, - WebSocketServer webSocketServer, - IEnumerable httpListeners - ) + public void Load(WebApplicationBuilder? builder) { - _logger = logger; - _localisationService = localisationService; - _configServer = configServer; - _applicationContext = applicationContext; - _webSocketServer = webSocketServer; - _httpListeners = httpListeners; - - httpConfig = _configServer.GetConfig(); - } - - public void Load(WebApplicationBuilder builder) - { - builder.WebHost.UseKestrel(); + builder?.WebHost.UseKestrel(); //builder.Services.AddControllers(); // At the end - var app = builder.Build(); + var app = builder?.Build(); // enable web socket - app.UseWebSockets(); + app?.UseWebSockets(); - app.Use((HttpContext req, RequestDelegate _) => - { - return Task.Factory.StartNew(() => HandleFallback(req)); - }); + app?.Use((HttpContext req, RequestDelegate _) => { return Task.Factory.StartNew(() => HandleFallback(req)); }); started = true; + if (app is null) + { + throw new Exception($"Application context is null in HttpServer.Load()"); + } + _applicationContext.AddValue(ContextVariableType.WEB_APPLICATION, app); } @@ -86,7 +67,7 @@ public class HttpServer ? forwardedFor.Value.First()!.Split(",")[0].Trim() : context.Connection.RemoteIpAddress!.ToString(); - if (httpConfig.LogRequests) + if (_httpConfig.LogRequests) { var isLocalRequest = IsLocalRequest(clientIp); if (isLocalRequest.HasValue) @@ -95,9 +76,11 @@ public class HttpServer _logger.Info(_localisationService.GetText("client_request", context.Request.Path.Value)); else _logger.Info( - _localisationService.GetText("client_request_ip", + _localisationService.GetText( + "client_request_ip", new Dictionary - { { "ip", clientIp }, { "url", context.Request.Path.Value } }) + { { "ip", clientIp }, { "url", context.Request.Path.Value } } + ) ); } }