Added container rebuild flow with real-time logs and updated UI, backend implementation, config options, and container helper API integration.
This commit is contained in:
7
Moonlight.Api/Configuration/ContainerHelperOptions.cs
Normal file
7
Moonlight.Api/Configuration/ContainerHelperOptions.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Moonlight.Api.Configuration;
|
||||
|
||||
public class ContainerHelperOptions
|
||||
{
|
||||
public bool IsEnabled { get; set; }
|
||||
public string Url { get; set; } = "http://helper:8080";
|
||||
}
|
||||
27
Moonlight.Api/Http/Controllers/Admin/ChController.cs
Normal file
27
Moonlight.Api/Http/Controllers/Admin/ChController.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moonlight.Api.Services;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers.Admin;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/admin/ch")]
|
||||
public class ChController : Controller
|
||||
{
|
||||
private readonly ContainerHelperService ContainerHelperService;
|
||||
|
||||
public ChController(ContainerHelperService containerHelperService)
|
||||
{
|
||||
ContainerHelperService = containerHelperService;
|
||||
}
|
||||
|
||||
[HttpPost("rebuild")]
|
||||
public Task<IResult> RebuildAsync()
|
||||
{
|
||||
var result = ContainerHelperService.RebuildAsync();
|
||||
|
||||
return Task.FromResult<IResult>(
|
||||
TypedResults.ServerSentEvents(result)
|
||||
);
|
||||
}
|
||||
}
|
||||
13
Moonlight.Api/Http/Controllers/PingController.cs
Normal file
13
Moonlight.Api/Http/Controllers/PingController.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/ping")]
|
||||
public class PingController : Controller
|
||||
{
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public IActionResult Get() => Ok("Pong");
|
||||
}
|
||||
@@ -47,7 +47,7 @@ public class ApplicationService : IHostedLifecycleService
|
||||
// TODO: Update / version check
|
||||
|
||||
VersionName = "v2.1.0 (a2d4edc0e5)";
|
||||
IsUpToDate = true;
|
||||
IsUpToDate = false;
|
||||
|
||||
OperatingSystem = OsHelper.GetName();
|
||||
}
|
||||
|
||||
67
Moonlight.Api/Services/ContainerHelperService.cs
Normal file
67
Moonlight.Api/Services/ContainerHelperService.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System.Text.Json;
|
||||
using Moonlight.Shared.Http;
|
||||
using Moonlight.Shared.Http.Events;
|
||||
|
||||
namespace Moonlight.Api.Services;
|
||||
|
||||
public class ContainerHelperService
|
||||
{
|
||||
private readonly IHttpClientFactory HttpClientFactory;
|
||||
|
||||
public ContainerHelperService(IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
HttpClientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<RebuildEvent> RebuildAsync()
|
||||
{
|
||||
var options = new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
options.TypeInfoResolverChain.Add(SerializationContext.Default);
|
||||
|
||||
var client = HttpClientFactory.CreateClient("ContainerHelper");
|
||||
|
||||
var response = await client.GetAsync("api/rebuild", HttpCompletionOption.ResponseHeadersRead);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var responseText = await response.Content.ReadAsStringAsync();
|
||||
yield return new RebuildEvent()
|
||||
{
|
||||
Type = RebuildEventType.Failed,
|
||||
Data = responseText
|
||||
};
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
await using var responseStream = await response.Content.ReadAsStreamAsync();
|
||||
using var streamReader = new StreamReader(responseStream);
|
||||
|
||||
do
|
||||
{
|
||||
var line = await streamReader.ReadLineAsync();
|
||||
|
||||
if(line == null)
|
||||
break;
|
||||
|
||||
if(string.IsNullOrWhiteSpace(line))
|
||||
continue;
|
||||
|
||||
var data = line.Trim("data: ");
|
||||
|
||||
var deserializedData = JsonSerializer.Deserialize<RebuildEvent>(data, options);
|
||||
|
||||
yield return deserializedData;
|
||||
} while (true);
|
||||
|
||||
yield return new RebuildEvent()
|
||||
{
|
||||
Type = RebuildEventType.Succeeded,
|
||||
Data = string.Empty
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,15 @@ public partial class Startup
|
||||
|
||||
builder.Services.AddOptions<FrontendOptions>().BindConfiguration("Moonlight:Frontend");
|
||||
builder.Services.AddScoped<FrontendService>();
|
||||
|
||||
builder.Services.AddOptions<ContainerHelperOptions>().BindConfiguration("Moonlight:ContainerHelper");
|
||||
builder.Services.AddSingleton<ContainerHelperService>();
|
||||
|
||||
builder.Services.AddHttpClient("ContainerHelper", (provider, client) =>
|
||||
{
|
||||
var options = provider.GetRequiredService<IOptions<ContainerHelperOptions>>();
|
||||
client.BaseAddress = new Uri(options.Value.IsEnabled ? options.Value.Url : "http://you-should-fail.invalid");
|
||||
});
|
||||
}
|
||||
|
||||
private static void UseBase(WebApplication application)
|
||||
|
||||
Reference in New Issue
Block a user