diff --git a/Platforms/Server/PongEndpoints.cs b/Platforms/Server/PongEndpoints.cs new file mode 100644 index 0000000..484d278 --- /dev/null +++ b/Platforms/Server/PongEndpoints.cs @@ -0,0 +1,39 @@ +using System; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; + +using Syntriax.Engine.Core; +using Syntriax.Engine.Network; + +namespace Server; + +public class PongEndpoints : Behaviour, IFirstFrameUpdate +{ + private INetworkCommunicatorServer? server = null!; + + public PongEndpoints() + { + Task.Run(() => + { + WebApplicationBuilder builder = WebApplication.CreateBuilder(); + + builder.Services.AddHealthChecks(); + + WebApplication app = builder.Build(); + + app.MapHealthChecks("/health"); + + app.MapGet("/stats", GetStats); + + app.Run($"http://0.0.0.0:{Environment.GetEnvironmentVariable("PORT") ?? "8888"}"); + }); + } + + private IResult GetStats() => Results.Json(new { Count = server?.Connections.Count ?? 0 }); + + public void FirstActiveFrame() => server = Universe.FindRequiredBehaviour(); + protected override void OnExitedUniverse(IUniverse universe) => server = null; +} diff --git a/Platforms/Server/Program.cs b/Platforms/Server/Program.cs index 0dcf651..f67e9a2 100644 --- a/Platforms/Server/Program.cs +++ b/Platforms/Server/Program.cs @@ -13,6 +13,9 @@ universe.InstantiateUniverseObject().SetUniverseObject("Logger").BehaviourContro Pong.PongUniverse.ApplyPongServer(universe, int.Parse(Environment.GetEnvironmentVariable("PORT") ?? "8888")); Pong.PongUniverse.ApplyPongUniverse(universe); +universe.InstantiateUniverseObject().SetUniverseObject("Endpoints").BehaviourController + .AddBehaviour(); + DateTime lastRun = DateTime.UtcNow; TimeSpan interval = new(0, 0, 0, 0, 16); TimeSpan timeSinceStart = new(0); diff --git a/Platforms/Server/Server.csproj b/Platforms/Server/Server.csproj index 7c3d799..8918732 100644 --- a/Platforms/Server/Server.csproj +++ b/Platforms/Server/Server.csproj @@ -1,4 +1,4 @@ - + Exe @@ -12,6 +12,8 @@ + +