From 1566949913b429d92f74260caeaf17d32c62a55f Mon Sep 17 00:00:00 2001 From: AlanMoonbase Date: Wed, 27 Nov 2024 11:28:31 -0800 Subject: [PATCH] start api before listener starts, stop and dispose of it when it stops, add ``HttpApiPort`` to config --- src/Core/Configuration.cs | 1 + src/Server.cs | 19 ++++++++++++++++++- src/appsettings.json | 5 ++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Core/Configuration.cs b/src/Core/Configuration.cs index 727e7dd..1e09126 100644 --- a/src/Core/Configuration.cs +++ b/src/Core/Configuration.cs @@ -29,6 +29,7 @@ internal static class Configuration { internal sealed class ServerConfiguration { public string ListenIP { get; set; } = string.Empty; public int Port { get; set; } = 9933; + public int HttpApiPort { get; set; } = 9934; public string EventName { get; set; } = "ScoutAttack"; public int EventTimer { get; set; } = 30; public int FirstEventTimer { get; set; } = 10; diff --git a/src/Server.cs b/src/Server.cs index b08dc98..0f55f13 100644 --- a/src/Server.cs +++ b/src/Server.cs @@ -1,4 +1,5 @@ -using sodoffmmo.Core; +using sodoffmmo.API; +using sodoffmmo.Core; using sodoffmmo.Data; using sodoffmmo.Management; using System; @@ -42,7 +43,14 @@ public class Server { } } + // start api before mmo listener is started + var apiBuilder = CreateApiBuilder().Build(); + await apiBuilder.StartAsync(); await Listen(listener); + + // when listener thread exists, stop and dispose api + await apiBuilder.StopAsync(); + apiBuilder.Dispose(); } private async Task Listen(Socket listener) { @@ -97,4 +105,13 @@ public class Server { } } } + + private static IHostBuilder CreateApiBuilder() + { + return Host.CreateDefaultBuilder().ConfigureWebHostDefaults(webHost => + { + webHost.UseUrls($"http://*:{Configuration.ServerConfiguration.HttpApiPort}"); + webHost.UseStartup(); + }); + } } diff --git a/src/appsettings.json b/src/appsettings.json index b4b10bf..4c06143 100644 --- a/src/appsettings.json +++ b/src/appsettings.json @@ -6,6 +6,9 @@ "// Port": "Listening port number for the MMO server", "Port": 9933, + "// HttpApiPort": "Listening port number for the MMO server's API", + "HttpApiPort": 9934, + "// PingDelay": "delay (in milliseconds) for PNG response", "PingDelay": 17, @@ -27,7 +30,7 @@ "LoungeInt" : [ ["3", 20.0, 30, 240, 0, 16] ], "Spaceport": [ ["1", 20.0, 300, 300, 60, 0], ["2", 120.0, 1800, 3600, 60, 0] ], "Academy": [ ["1", 20.0, 300, 300, 60, 0] ], - "ClubSSInt" : [ ["3", 20.0, 30, 240, 0, 16] ], + "ClubSSInt" : [ ["3", 20.0, 30, 240, 0, 16] ] }, "// RacingMaxPlayers": "maximum players allowed in Thunder Run Racing (no more than 6)",