Application cleanup (#485)

* Changed application to use background services and removed hacky http server startup

* Small improvements and method removals

* Removed Core dependency on Web application SDK

* Fixed wrong imported type

---------

Co-authored-by: Alex <clodanSPT@hotmail.com>
Co-authored-by: Chomp <27521899+chompDev@users.noreply.github.com>
This commit is contained in:
clodanSPT
2025-07-18 16:21:24 +01:00
committed by GitHub
parent 67b886127c
commit 1af50bfd34
24 changed files with 133 additions and 166 deletions
@@ -1,4 +1,6 @@
namespace SPTarkov.Server.Core.Servers.Http;
using Microsoft.AspNetCore.Http;
namespace SPTarkov.Server.Core.Servers.Http;
public interface IHttpListener
{
@@ -1,6 +1,7 @@
using System.Collections.Immutable;
using System.IO.Compression;
using System.Text;
using Microsoft.AspNetCore.Http;
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.DI;
using SPTarkov.Server.Core.Models.Enums;
@@ -1,10 +1,7 @@
using System.Net;
using System.Security.Authentication;
using Microsoft.AspNetCore.Server.Kestrel.Https;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using SPTarkov.Common.Extensions;
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Helpers;
using SPTarkov.Server.Core.Models.Spt.Config;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Servers.Http;
@@ -14,82 +11,17 @@ namespace SPTarkov.Server.Core.Servers;
[Injectable(InjectionType.Singleton)]
public class HttpServer(
WebApplicationBuilder _builder,
ISptLogger<HttpServer> _logger,
ServerLocalisationService _serverLocalisationService,
ConfigServer _configServer,
CertificateHelper _certificateHelper,
WebSocketServer _webSocketServer,
ProfileActivityService _profileActivityService,
IEnumerable<IHttpListener> _httpListeners
)
{
private readonly HttpConfig _httpConfig = _configServer.GetConfig<HttpConfig>();
private bool _started;
private WebApplication? _webApplication;
/// <summary>
/// Handle server loading event
/// </summary>
/// <exception cref="Exception"> Throws Exception when WebApplicationBuilder or WebApplication are null </exception>
public void Load()
{
if (_builder is null)
{
throw new Exception("WebApplicationBuilder is null in HttpServer.Load()");
}
_builder.WebHost.ConfigureKestrel(options =>
{
options.Listen(
IPAddress.Parse(_httpConfig.Ip),
_httpConfig.Port,
listenOptions =>
{
listenOptions.UseHttps(opts =>
{
opts.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13;
opts.ServerCertificate = _certificateHelper.LoadOrGenerateCertificatePfx();
opts.ClientCertificateMode = ClientCertificateMode.NoCertificate;
});
}
);
});
_webApplication = _builder.Build();
if (_webApplication is null)
{
throw new Exception("WebApplication is null in HttpServer.Load()");
}
// Enable web socket
_webApplication.UseWebSockets(
new WebSocketOptions
{
// Every minute a heartbeat is sent to keep the connection alive.
KeepAliveInterval = TimeSpan.FromSeconds(60),
}
);
_webApplication.Use(
async (HttpContext req, RequestDelegate _) =>
{
await HandleFallback(req);
}
);
}
public async Task StartAsync()
{
if (_webApplication != null && !_started)
{
_started = true;
await _webApplication.RunAsync();
}
}
private async Task HandleFallback(HttpContext context)
public async Task HandleRequest(HttpContext context)
{
if (context.WebSockets.IsWebSocketRequest)
{
@@ -203,11 +135,6 @@ public class HttpServer(
return found;
}
public bool IsStarted()
{
return _started;
}
public string ListeningUrl()
{
return $"https://{_httpConfig.Ip}:{_httpConfig.Port}";
@@ -1,4 +1,5 @@
using System.Net.WebSockets;
using Microsoft.AspNetCore.Http;
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Servers.Ws;
@@ -1,4 +1,5 @@
using System.Net.WebSockets;
using Microsoft.AspNetCore.Http;
namespace SPTarkov.Server.Core.Servers.Ws;
@@ -1,5 +1,6 @@
using System.Net.WebSockets;
using System.Text;
using Microsoft.AspNetCore.Http;
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Helpers;
using SPTarkov.Server.Core.Models.Eft.Ws;