using Microsoft.AspNetCore.Mvc; using static CompServ.CompServConst; namespace CompServ.Hub { [ApiController] public class HubController(HubWorkerHolder hubWorkerHolder) : ControllerBase { [HttpGet] [Route(ApiPathHubRootForCheckAlive)] public ActionResult CheckAlive() => Content(AliveMessageHub); [HttpPost] [Route(ApiPathHubRegisterWorker)] public async Task Register() { var wr = HttpContext.Request.ExtractWorkerRegister(); if (wr.port == null || wr.ratio == null) return StatusCode(400, "missing required header"); var url = $"http://{Request.HttpContext.Connection.RemoteIpAddress}:{wr.port}"; hubWorkerHolder.RegisterWorker(url, wr.ratio.Value); return Empty; } [HttpPost] [Route(ApiPathHubUnRegisterWorker)] public async Task UnRegister() { var wr = HttpContext.Request.ExtractWorkerRegister(); if (wr.port == null) return StatusCode(400, "missing required header"); var url = $"http://{Request.HttpContext.Connection.RemoteIpAddress}:{wr.port}"; hubWorkerHolder.UnRegisterWorker(url); return Empty; } [HttpGet] [Route(ApiPathHubGetWorker)] public async Task GetWorker() { var url = await hubWorkerHolder.GetWorkerAsync(); return Content(url); } } }