Files
CaddyManager/CaddyManager/Components/Pages/Caddy/CaddyfileEditor/CaddyfileEditor.razor.cs
ebolo b5cbad3664
All checks were successful
Caddy Manager CI build / docker (push) Successful in 5m28s
refactor(caddy): centralize reverse proxy editor dialogs
2026-07-26 13:29:46 +07:00

167 lines
5.3 KiB
C#

using CaddyManager.Contracts.Caddy;
using BlazorMonaco.Editor;
using CaddyManager.Contracts.Models.Caddy;
using Microsoft.AspNetCore.Components;
using MudBlazor;
namespace CaddyManager.Components.Pages.Caddy.CaddyfileEditor;
/// <summary>
/// Caddyfile editor component that allows the user to edit the Caddy configuration file
/// </summary>
public partial class CaddyfileEditor : ComponentBase
{
private string _caddyConfigurationContent = string.Empty;
private StandaloneCodeEditor _codeEditor = null!;
/// <summary>
/// The file name the dialog was opened with, used to detect a rename since FileName follows the text field
/// </summary>
private string _originalFileName = string.Empty;
[CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = null!;
/// <summary>
/// Callback to be invoked when the Caddyfile is duplicated.
/// </summary>
[Parameter] public EventCallback<string> OnDuplicate { get; set; }
/// <summary>
/// Determines if the Caddy configuration file is new
/// </summary>
private bool IsNew { get; set; }
[Parameter] public string FileName { get; set; } = string.Empty;
[Parameter] public string InitialContent { get; set; } = string.Empty;
[Inject] private ICaddyService CaddyService { get; set; } = null!;
[Inject] private ISnackbar Snackbar { get; set; } = null!;
[Inject] private IDialogService DialogService { get; set; } = null!;
protected override Task OnInitializedAsync()
{
IsNew = string.IsNullOrWhiteSpace(FileName);
_originalFileName = FileName;
if (!IsNew)
{
// Load the content of the Caddy configuration file
_caddyConfigurationContent = CaddyService.GetCaddyConfigurationContent(FileName);
}
else if (!string.IsNullOrWhiteSpace(InitialContent))
{
_caddyConfigurationContent = InitialContent;
}
return base.OnInitializedAsync();
}
/// <summary>
/// Returns the construction options for the Caddy configuration file editor
/// </summary>
/// <param name="editor">The Caddy configuration file editor</param>
/// <returns>The construction options for the Caddy configuration file editor</returns>
private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor)
{
return new StandaloneEditorConstructionOptions
{
AutomaticLayout = true,
Language = "graphql",
Value = _caddyConfigurationContent,
Theme = "vs-dark",
Padding = new EditorPaddingOptions
{
Top = 8f,
Bottom = 8f,
}
};
}
/// <summary>
/// Renames the Caddy configuration file when needed, then saves its content
/// </summary>
/// <returns>True when the configuration was persisted</returns>
private async Task<bool> Save()
{
// Rename first so a failed rename never leaves the content written under a stale name
if (!IsNew && !string.Equals(_originalFileName, FileName, StringComparison.Ordinal))
{
var renameResponse = CaddyService.RenameCaddyConfiguration(_originalFileName, FileName);
if (!renameResponse.Success)
{
Snackbar.Add(renameResponse.Message, Severity.Error);
return false;
}
Snackbar.Add($"Renamed {_originalFileName} to {FileName}", Severity.Info);
if (CaddyService.GetCaddyGlobalConfigurationContent().Contains(_originalFileName))
{
Snackbar.Add($"The global Caddyfile still references {_originalFileName}, update its import",
Severity.Warning);
}
_originalFileName = FileName;
}
var response = CaddyService.SaveCaddyConfiguration(new CaddySaveConfigurationRequest
{
IsNew = IsNew,
FileName = FileName,
Content = await _codeEditor.GetValue(),
});
if (!response.Success)
{
Snackbar.Add(response.Message, Severity.Error);
return false;
}
Snackbar.Add($"{FileName} Caddy configuration saved successfully", Severity.Success);
return true;
}
/// <summary>
/// Saves the Caddy configuration file
/// </summary>
private async Task Submit()
{
if (await Save())
{
MudDialog.Close(DialogResult.Ok(false)); // Indicate successful save but no restart
}
}
/// <summary>
/// Cancels the Caddy configuration file editor
/// </summary>
private void Cancel()
{
MudDialog.Cancel();
}
/// <summary>
/// Saves the Caddy configuration file and reloads the Caddy configuration
/// </summary>
private async Task SaveAndReload()
{
if (await Save())
{
// Indicate successful save and that a reload is required by the calling component
MudDialog.Close(DialogResult.Ok(true));
}
}
/// <summary>
/// Duplicates the Caddy configuration file
/// </summary>
private async Task Duplicate()
{
var content = await _codeEditor.GetValue();
MudDialog.Close(DialogResult.Ok(false));
await OnDuplicate.InvokeAsync(content);
}
}