feat: server health and statistics endpoints added
This commit is contained in:
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;
|
||||
}
|
||||
Reference in New Issue
Block a user