Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions SPTarkov.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public static async Task Main(string[] args)

builder.Services.AddSingleton(builder);
builder.Services.AddSingleton<IReadOnlyList<SptMod>>(loadedMods);
builder.Services.AddHostedService<SptServerBackgroundService>();
// Configure Kestrel options
ConfigureKestrel(builder);

Expand All @@ -74,9 +73,9 @@ public static async Task Main(string[] args)
ConfigureWebApp(app);

// In case of exceptions we snatch a Server logger
var serverExceptionLogger = app.Services.GetService<ILoggerFactory>()!.CreateLogger("Server");
var serverExceptionLogger = app.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Server");
// We need any logger instance to use as a finalizer when the app closes
var loggerFinalizer = app.Services.GetService<ISptLogger<App>>()!;
var loggerFinalizer = app.Services.GetRequiredService<ISptLogger<App>>();
try
{
// Handle edge cases where reverse proxies might pass X-Forwarded-For, use this as the actual IP address
Expand All @@ -86,6 +85,8 @@ public static async Task Main(string[] args)

SetConsoleOutputMode();

await app.Services.GetRequiredService<SptServerStartupService>().Startup();

await app.RunAsync();
}
catch (Exception ex)
Expand All @@ -111,7 +112,7 @@ private static void ConfigureWebApp(WebApplication app)
app.Use(
async (HttpContext context, RequestDelegate _) =>
{
await context.RequestServices.GetService<HttpServer>()!.HandleRequest(context);
await context.RequestServices.GetRequiredService<HttpServer>().HandleRequest(context);
}
);
}
Expand All @@ -122,8 +123,8 @@ private static void ConfigureKestrel(WebApplicationBuilder builder)
(_, options) =>
{
// This method is not expected to be async so we need to wait for the Task instead of using await keyword
options.ApplicationServices.GetService<OnWebAppBuildModLoader>()!.OnLoad().Wait();
var httpConfig = options.ApplicationServices.GetService<ConfigServer>()?.GetConfig<HttpConfig>()!;
options.ApplicationServices.GetRequiredService<OnWebAppBuildModLoader>().OnLoad().Wait();
var httpConfig = options.ApplicationServices.GetRequiredService<ConfigServer>().GetConfig<HttpConfig>();

// Probe the http ip and port to see if its being used, this method will throw an exception and crash
// the server if the IP/Port combination is already in use
Expand All @@ -138,7 +139,7 @@ private static void ConfigureKestrel(WebApplicationBuilder builder)
listener?.Stop();
}

var certHelper = options.ApplicationServices.GetService<CertificateHelper>()!;
var certHelper = options.ApplicationServices.GetRequiredService<CertificateHelper>();
options.Listen(
IPAddress.Parse(httpConfig.Ip),
httpConfig.Port,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System.Runtime;
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Loaders;
using SPTarkov.Server.Core.Models.Spt.Mod;
using SPTarkov.Server.Core.Utils;

namespace SPTarkov.Server.Services;

public class SptServerBackgroundService(IReadOnlyList<SptMod> loadedMods, BundleLoader bundleLoader, App app) : BackgroundService
[Injectable(InjectionType.Singleton)]
public class SptServerStartupService(IReadOnlyList<SptMod> loadedMods, BundleLoader bundleLoader, App app)
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
public async Task Startup()
{
if (ProgramStatics.MODS())
{
Expand Down
Loading