feat: server health and statistics endpoints added
This commit is contained in:
parent
d41246b6f6
commit
afd4ae8327
39
Platforms/Server/PongEndpoints.cs
Normal file
39
Platforms/Server/PongEndpoints.cs
Normal file
@ -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<INetworkCommunicatorServer>();
|
||||||
|
protected override void OnExitedUniverse(IUniverse universe) => server = null;
|
||||||
|
}
|
@ -13,6 +13,9 @@ universe.InstantiateUniverseObject().SetUniverseObject("Logger").BehaviourContro
|
|||||||
Pong.PongUniverse.ApplyPongServer(universe, int.Parse(Environment.GetEnvironmentVariable("PORT") ?? "8888"));
|
Pong.PongUniverse.ApplyPongServer(universe, int.Parse(Environment.GetEnvironmentVariable("PORT") ?? "8888"));
|
||||||
Pong.PongUniverse.ApplyPongUniverse(universe);
|
Pong.PongUniverse.ApplyPongUniverse(universe);
|
||||||
|
|
||||||
|
universe.InstantiateUniverseObject().SetUniverseObject("Endpoints").BehaviourController
|
||||||
|
.AddBehaviour<Server.PongEndpoints>();
|
||||||
|
|
||||||
DateTime lastRun = DateTime.UtcNow;
|
DateTime lastRun = DateTime.UtcNow;
|
||||||
TimeSpan interval = new(0, 0, 0, 0, 16);
|
TimeSpan interval = new(0, 0, 0, 0, 16);
|
||||||
TimeSpan timeSinceStart = new(0);
|
TimeSpan timeSinceStart = new(0);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
@ -12,6 +12,8 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore" Version="2.3.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="10.0.0-preview.5.25277.114" />
|
||||||
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.2.1105" />
|
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.2.1105" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user