51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using PlanTempusAdmin.Services;
|
|
|
|
namespace PlanTempusAdmin.Pages.Caddy;
|
|
|
|
public class IndexModel : PageModel
|
|
{
|
|
private readonly CaddyService _caddyService;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public bool IsRunning { get; set; }
|
|
public int HostCount { get; set; }
|
|
public string? RawConfig { get; set; }
|
|
public string CaddyAdminUrl { get; set; } = string.Empty;
|
|
|
|
public IndexModel(CaddyService caddyService, IConfiguration configuration)
|
|
{
|
|
_caddyService = caddyService;
|
|
_configuration = configuration;
|
|
}
|
|
|
|
public async Task OnGetAsync()
|
|
{
|
|
CaddyAdminUrl = _configuration.GetValue<string>("Caddy:AdminUrl") ?? "http://localhost:2019";
|
|
IsRunning = await _caddyService.IsRunningAsync();
|
|
|
|
if (IsRunning)
|
|
{
|
|
var hosts = await _caddyService.GetHostsAsync();
|
|
HostCount = hosts.Count;
|
|
RawConfig = await _caddyService.GetRawConfigAsync();
|
|
|
|
// Pretty print JSON
|
|
if (!string.IsNullOrEmpty(RawConfig))
|
|
{
|
|
try
|
|
{
|
|
var jsonDoc = System.Text.Json.JsonDocument.Parse(RawConfig);
|
|
RawConfig = System.Text.Json.JsonSerializer.Serialize(jsonDoc, new System.Text.Json.JsonSerializerOptions
|
|
{
|
|
WriteIndented = true
|
|
});
|
|
}
|
|
catch
|
|
{
|
|
// Keep raw config if parsing fails
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|