using LightlessSync.API.Routes; using LightlessSyncShared.Utils.Configuration; using LightlessSyncStaticFilesServer.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace LightlessSyncStaticFilesServer.Controllers; [Route(LightlessFiles.Main)] [Authorize(Policy = "Internal")] public class MainController : ControllerBase { private readonly IClientReadyMessageService _messageService; private readonly MainServerShardRegistrationService _shardRegistrationService; public MainController(ILogger logger, IClientReadyMessageService lightlessHub, MainServerShardRegistrationService shardRegistrationService) : base(logger) { _messageService = lightlessHub; _shardRegistrationService = shardRegistrationService; } [HttpGet(LightlessFiles.Main_SendReady)] public async Task SendReadyToClients(string uid, Guid requestId) { await _messageService.SendDownloadReady(uid, requestId).ConfigureAwait(false); return Ok(); } [HttpPost("shardRegister")] public IActionResult RegisterShard([FromBody] ShardConfiguration shardConfiguration) { try { _shardRegistrationService.RegisterShard(LightlessUser, shardConfiguration); return Ok(); } catch (Exception ex) { _logger.LogWarning(ex, "Shard could not be registered {shard}", LightlessUser); return BadRequest(); } } [HttpPost("shardUnregister")] public IActionResult UnregisterShard() { _shardRegistrationService.UnregisterShard(LightlessUser); return Ok(); } [HttpPost("shardHeartbeat")] public IActionResult ShardHeartbeat() { try { _shardRegistrationService.ShardHeartbeat(LightlessUser); return Ok(); } catch (Exception ex) { _logger.LogWarning(ex, "Shard not registered: {shard}", LightlessUser); return BadRequest(); } } }