82 lines
2.6 KiB
C#
82 lines
2.6 KiB
C#
using LightlessSync.API.Routes;
|
|
using LightlessSyncShared.Utils.Configuration;
|
|
using LightlessSyncStaticFilesServer.Services;
|
|
using LightlessSyncShared.Models;
|
|
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<MainController> logger, IClientReadyMessageService lightlessHub,
|
|
MainServerShardRegistrationService shardRegistrationService) : base(logger)
|
|
{
|
|
_messageService = lightlessHub;
|
|
_shardRegistrationService = shardRegistrationService;
|
|
}
|
|
|
|
[HttpGet(LightlessFiles.Main_SendReady)]
|
|
public async Task<IActionResult> 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();
|
|
}
|
|
}
|
|
|
|
[HttpPost(LightlessFiles.Main_ShardFiles)]
|
|
public IActionResult ShardFilesUpdate([FromBody] ShardFileInventoryUpdateDto update)
|
|
{
|
|
try
|
|
{
|
|
var applied = _shardRegistrationService.ApplyFileInventoryUpdate(LightlessUser, update);
|
|
return Ok(new ShardFileInventoryUpdateAckDto { AppliedSequence = applied });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "Shard file inventory update failed: {shard}", LightlessUser);
|
|
return BadRequest();
|
|
}
|
|
}
|
|
} |