Started with servers sync to daemon

This commit is contained in:
2024-12-24 00:42:17 +01:00
parent 4326af2925
commit 9f8c1f6d24
19 changed files with 271 additions and 12 deletions

View File

@@ -0,0 +1,38 @@
using MoonCore.Attributes;
namespace MoonlightServers.Daemon.Services;
[Singleton]
public class ApplicationStateService : IHostedLifecycleService
{
private readonly ServerService ServerService;
private readonly ILogger<ApplicationStateService> Logger;
public ApplicationStateService(ServerService serverService, ILogger<ApplicationStateService> logger)
{
ServerService = serverService;
Logger = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
=> Task.CompletedTask;
public Task StopAsync(CancellationToken cancellationToken)
=> Task.CompletedTask;
public async Task StartedAsync(CancellationToken cancellationToken)
{
Logger.LogInformation("Performing initialization");
await ServerService.Initialize();
}
public Task StartingAsync(CancellationToken cancellationToken)
=> Task.CompletedTask;
public Task StoppedAsync(CancellationToken cancellationToken)
=> Task.CompletedTask;
public Task StoppingAsync(CancellationToken cancellationToken)
=> Task.CompletedTask;
}

View File

@@ -0,0 +1,44 @@
using MoonCore.Attributes;
using MoonCore.Models;
using MoonlightServers.Daemon.Models;
using MoonlightServers.DaemonShared.PanelSide.Http.Responses;
namespace MoonlightServers.Daemon.Services;
[Singleton]
public class ServerService
{
private readonly List<ServerData> Servers = new();
private readonly RemoteService RemoteService;
private readonly ILogger<ServerService> Logger;
public ServerService(RemoteService remoteService, ILogger<ServerService> logger)
{
RemoteService = remoteService;
Logger = logger;
}
public async Task Initialize() //TODO: Add initialize call from panel
{
//TODO: Handle block creating servers while initializing
Logger.LogInformation("Loading servers from panel");
var apiClient = await RemoteService.CreateHttpClient();
var servers = await PagedData<ServerDataResponse>.All(async (page, pageSize) =>
await apiClient.GetJson<PagedData<ServerDataResponse>>(
$"api/servers/remote/servers?page={page}&pageSize={pageSize}"
)
);
Logger.LogInformation("Initializing {count} servers", servers.Length);
}
public Task ImportServer(ServerData serverData)
{
lock (Servers)
Servers.Add(serverData);
return Task.CompletedTask;
}
}