@page "/admin/servers/nodes" @using System.Diagnostics @using MoonCore.Exceptions @using Moonlight.Client.App.Models.Crud @using Moonlight.Client.App.Services @using Moonlight.Shared.Http.Resources @using MoonlightServers.Client.UI.Components.Forms @using MoonlightServers.Shared.Http.Requests.Admin.Nodes @using MoonlightServers.Shared.Http.Responses.Admin.Nodes @using MoonlightServers.Client.UI.Components.Partials @inject HttpApiClient HttpApiClient @inject AlertService AlertService @attribute [RequirePermission("admin.servers.nodes.get")]
@code { private readonly Dictionary StatusCache = new(); private void OnConfigure(CrudOptions options) { options.Loader = async (page, pageSize) => { var response = await HttpApiClient .GetJson>($"admin/servers/nodes?page={page}&pageSize={pageSize}"); lock (StatusCache) StatusCache.Clear(); Task.Run(async () => { foreach (var node in response.Items) { try { var status = await HttpApiClient.GetJson($"admin/servers/nodes/{node.Id}/status"); lock (StatusCache) { StatusCache.Add(node.Id, new() { Response = status }); } } catch (Exception e) { lock (StatusCache) { StatusCache.Add(node.Id, new() { Exception = e }); } } await InvokeAsync(StateHasChanged); } }); return response; }; options.CreateFunction = async request => await HttpApiClient.Post("admin/servers/nodes", request); options.UpdateFunction = async (request, item) => await HttpApiClient.Patch($"admin/servers/nodes/{item.Id}", request); options.DeleteFunction = async item => await HttpApiClient.Delete($"admin/servers/nodes/{item.Id}"); options.ShowCreateAsModal = false; options.ShowUpdateAsModal = false; options.ShowDetailsAsModal = false; options.ShowDetailsBar = false; options.OnConfigureCreate = option => { option .DefaultPage .DefaultSection .AddProperty(x => x.Name); option .DefaultPage .DefaultSection .AddProperty(x => x.Fqdn); option .DefaultPage .DefaultSection .AddProperty(x => x.ApiPort); option .DefaultPage .DefaultSection .AddProperty(x => x.SslEnabled); }; options.OnConfigureUpdate = (option, item) => { option .DefaultPage .DefaultSection .AddProperty(x => x.Name); option .DefaultPage .DefaultSection .AddProperty(x => x.Fqdn); option .DefaultPage .DefaultSection .AddProperty(x => x.ApiPort); option .DefaultPage .DefaultSection .AddProperty(x => x.SslEnabled); }; } private async Task ShowDetails(Exception e) { await AlertService.ErrorLog("Node connection error", e.ToStringDemystified()); } class NodeFetchState { public StatusNodeResponse? Response { get; set; } public Exception? Exception { get; set; } } }