Skip to content

Commit

Permalink
#115 chore: added first setup page and enabled settings edition
Browse files Browse the repository at this point in the history
  • Loading branch information
joseantmazonsb committed Mar 13, 2022
1 parent 2b8b72d commit 9d3ef58
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 38 deletions.
9 changes: 8 additions & 1 deletion Linguard/Core/Managers/ConfigurationManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected ConfigurationManagerBase(IConfiguration configuration, IWorkingDirecto
public IWorkingDirectory WorkingDirectory { get; set; }

public ILogTarget LoggingTarget { get; set; }
public bool IsSetupNeeded { get; set; } = true;

public void LoadDefaults() {
LoadWebDefaults();
Expand Down Expand Up @@ -64,6 +65,12 @@ private void LoadWireguardDefaults() {
}

public abstract void Load();
public abstract void Save();

public void Save() {
IsSetupNeeded = false;
DoSave();
}

protected abstract void DoSave();
public abstract string Export();
}
3 changes: 2 additions & 1 deletion Linguard/Core/Managers/FileConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public override void Load() {
);
}
}
public override void Save() {

protected override void DoSave() {
try {
File.WriteAllText(ConfigurationFile.FullName, Export());
}
Expand Down
1 change: 1 addition & 0 deletions Linguard/Core/Managers/IConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public interface IConfigurationManager {
/// Logging target of the application.
/// </summary>
ILogTarget LoggingTarget { get; set; }
bool IsSetupNeeded { get; set; }
/// <summary>
/// Load default options.
/// </summary>
Expand Down
15 changes: 0 additions & 15 deletions Linguard/Web/NLog.config

This file was deleted.

67 changes: 67 additions & 0 deletions Linguard/Web/Pages/Setup.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
@page "/setup"
@layout SetupLayout
@using Linguard.Core.Utils
@using Linguard.Core.Managers
@using Linguard.Core.Configuration
@using Linguard.Log
@using Linguard.Web.Services

<PageTitle>@($"{AssemblyInfo.Product} | {Title}")</PageTitle>

<RadzenHeading Text="@Title" class="mb-3 text-center"/>

<RadzenCard class="mb-3 mx-auto col-xxl-6">
<RadzenTemplateForm Data="@_configuration" Submit="@((IConfiguration args) => { Save(args); })">
<RadzenCard>
<h2>Web</h2>
<WebSettings Configuration="@_configuration.Web"/>
</RadzenCard>
<RadzenCard class="mt-2">
<h2>Wireguard</h2>
<WireguardSettings Configuration="@_configuration.Wireguard"/>
</RadzenCard>
<RadzenCard class="mt-2">
<h2>Logging</h2>
<LoggingSettings Configuration="@_configuration.Logging"/>
</RadzenCard>
<div class="row mt-3 d-flex">
<div class="col-md-12 align-items-end">
<RadzenButton ButtonType="ButtonType.Submit" Icon="save" Text="Save" class="me-2"
ButtonStyle="ButtonStyle.Primary"/>
</div>
</div>
</RadzenTemplateForm>
</RadzenCard>

@inject IConfigurationManager _configurationManager
@inject NotificationService _notificationService
@inject IWebService _webService
@inject ILinguardLogger _logger
@inject NavigationManager _navigationManager

@code {
const string Title = "Setup";
IConfiguration _configuration;

protected override void OnInitialized() {
_configuration = (IConfiguration) _configurationManager.Configuration.Clone();
}

private void Save(IConfiguration configuration) {
try {
_logger.LogDebug("Saving configuration...");
_configurationManager.Configuration = (IConfiguration)configuration.Clone();
_configurationManager.Save();
_logger.LogInformation("Setup completed. Redirecting...");
_navigationManager.NavigateTo("/");
}
catch (Exception e) {
_logger.LogError(e, "Setup failed: unable to save configuration.");
_notificationService.Notify(new NotificationMessage {
Severity = NotificationSeverity.Error,
Summary = "Failed to save configuration",
Detail = e.Message
});
}
}
}
8 changes: 7 additions & 1 deletion Linguard/Web/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
@{
Layout = "_Layout";
}
<component type="typeof(App)" render-mode="ServerPrerendered"/>

<environment include="Staging,Production">
<component render-mode="ServerPrerendered" type="typeof(App)" />
</environment>
<environment include="Development">
<component render-mode="Server" type="typeof(App)" />
</environment>

@section styles {
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css"/>
Expand Down
4 changes: 4 additions & 0 deletions Linguard/Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Linguard.Core.OS;
using Linguard.Core.Services;
using Linguard.Log;
using Linguard.Web.Middlewares;
using Linguard.Web.Services;
using QRCoder;
using Radzen;
Expand Down Expand Up @@ -37,11 +38,14 @@
builder.Services.AddScoped<NotificationService>();
builder.Services.AddScoped<TooltipService>();
builder.Services.AddScoped<ContextMenuService>();
builder.Services.AddScoped<LoginMiddleware>();

builder.Logging.AddSimpleFileLogger();

var app = builder.Build();

app.UseMiddleware<LoginMiddleware>();

app.Lifetime.ApplicationStarted.Register(() => {
app.Services.GetService<ILifetimeService>()?.OnAppStarted();
});
Expand Down
2 changes: 1 addition & 1 deletion Linguard/Web/Services/LifetimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ private void LoadConfiguration() {
_configurationManager.WorkingDirectory.BaseDirectory = GetWorkingDirectory();
try {
_configurationManager.Load();
_configurationManager.IsSetupNeeded = false;
_logger.LogInformation("Configuration loaded.");
}
catch (ConfigurationNotLoadedError e) {
_logger.LogWarning(e, "Unable to load configuration. Using defaults.");
_configurationManager.LoadDefaults();
_configurationManager.Save();
}
}

Expand Down
21 changes: 17 additions & 4 deletions Linguard/Web/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@using Linguard.Core.Utils
@using Linguard.Core.Managers
@using Linguard.Log
@inherits LayoutComponentBase

<RadzenDialog />
Expand All @@ -21,8 +23,8 @@
<RadzenLabel Text="@AssemblyInfo.Product"/>
}
</div>
<div class="col-6 text-right">
<ProfileMenu></ProfileMenu>
<div class="col-6">
<ProfileMenu/>
</div>
</div>
</ChildContent>
Expand Down Expand Up @@ -54,6 +56,10 @@
</ChildContent>
</RadzenLayout>

@inject IConfigurationManager _configurationManager
@inject NavigationManager _navigationManager
@inject ILinguardLogger _logger;

@code {
private RadzenSidebar _sidebar;
private RadzenBody _body;
Expand All @@ -65,8 +71,15 @@
await InvokeAsync(() => _body.Toggle());
}

protected override void OnParametersSet()
{
protected override void OnParametersSet() {
errorBoundary?.Recover();
}

protected override void OnInitialized() {
if (_configurationManager.IsSetupNeeded) {
_logger.LogInformation("Setup required. Redirecting...");
_navigationManager.NavigateTo("/setup");
}
}

}
4 changes: 2 additions & 2 deletions Linguard/Web/Shared/ProfileMenu.razor
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@using Linguard.Core.Models
<div class="d-flex align-items-center justify-content-end">
<RadzenDropDown TValue="string" AllowClear="false" class="mx-0 mx-lg-3 text-start"
<RadzenDropDown TValue="string" AllowClear="false" class="mx-0 mx-lg-3"
Data="@Enum.GetNames(typeof(Style)).Select(s => s.ToString())"
Value="Style.Default.ToString()"
ValueChanged="OnStyleChanged">
</RadzenDropDown>
<RadzenProfileMenu>
<RadzenProfileMenu class="text-end">
<Template>
<RadzenGravatar Email="@GravatarEmail"/>
</Template>
Expand Down
51 changes: 51 additions & 0 deletions Linguard/Web/Shared/SetupLayout.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@using Linguard.Core.Managers
@inherits LayoutComponentBase

<RadzenDialog />
<RadzenNotification />
<RadzenTooltip />
<RadzenContextMenu />

<RadzenLayout>
<ChildContent>
<RadzenBody @ref="_body">
<ChildContent>
<RadzenContentContainer Name="main">
<ErrorBoundary @ref="errorBoundary">
<ChildContent>
@Body
</ChildContent>
<ErrorContent>
<UnhandledError Exception="context"/>
</ErrorContent>
</ErrorBoundary>
</RadzenContentContainer>
</ChildContent>
</RadzenBody>
<RadzenFooter>
<ChildContent>
<Footer></Footer>
</ChildContent>
</RadzenFooter>
</ChildContent>
</RadzenLayout>

@inject IConfigurationManager _configurationManager
@inject NavigationManager _navigationManager

@code {
private RadzenBody _body;
private ErrorBoundary? errorBoundary;

protected override void OnParametersSet() {
errorBoundary?.Recover();
}

protected override void OnInitialized() {
base.OnInitialized();
if (!_configurationManager.IsSetupNeeded) {
_navigationManager.NavigateTo("/");
}
}

}
2 changes: 1 addition & 1 deletion Linguard/Web/Shared/WebSettings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="row mb-2">
<div class="col-xxl-10">
<RadzenNumeric TValue="int" Min="1" Placeholder="wg0" Trim="true" class="w-100"
Value="@Configuration.LoginAttempts"/>
@bind-Value="@Configuration.LoginAttempts"/>
</div>
</div>
</div>
Expand Down
21 changes: 15 additions & 6 deletions Linguard/Web/Shared/WireguardSettings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
</div>
<div class="row">
<div class="col">
<RadzenTextBox class="w-100" Value="@Configuration.Endpoint?.ToString()" Placeholder="vpn.example.com"/>
<RadzenTextBox class="w-100" Value="@Configuration.Endpoint?.ToString()"
Placeholder="vpn.example.com"
ValueChanged="s => Configuration.Endpoint = new Uri(s, UriKind.RelativeOrAbsolute)"/>
</div>
</div>
</div>
Expand All @@ -24,7 +26,9 @@
</div>
<div class="row">
<div class="col">
<RadzenTextBox class="w-100" Value="@Configuration.PrimaryDns?.ToString()" Placeholder="dns1.example.com"/>
<RadzenTextBox class="w-100" Value="@Configuration.PrimaryDns?.ToString()"
Placeholder="dns1.example.com"
ValueChanged="s => Configuration.PrimaryDns = new Uri(s, UriKind.RelativeOrAbsolute)"/>
</div>
</div>
</div>
Expand All @@ -36,7 +40,9 @@
</div>
<div class="row">
<div class="col">
<RadzenTextBox class="w-100" Value="@Configuration.SecondaryDns?.ToString()" Placeholder="dns2.example.com"/>
<RadzenTextBox class="w-100" Value="@Configuration.SecondaryDns?.ToString()"
Placeholder="dns2.example.com"
ValueChanged="s => Configuration.SecondaryDns = new Uri(s, UriKind.RelativeOrAbsolute)"/>
</div>
</div>
</div>
Expand All @@ -51,7 +57,8 @@
</div>
<div class="row">
<div class="col">
<RadzenTextBox class="w-100" Value="@Configuration.WireguardBin" Placeholder="/usr/bin/wg"/>
<RadzenTextBox class="w-100" @bind-Value="@Configuration.WireguardBin"
Placeholder="/usr/bin/wg"/>
</div>
</div>
</div>
Expand All @@ -63,7 +70,8 @@
</div>
<div class="row">
<div class="col">
<RadzenTextBox class="w-100" Value="@Configuration.WireguardQuickBin" Placeholder="/usr/bin/wg-quick"/>
<RadzenTextBox class="w-100" @bind-Value="@Configuration.WireguardQuickBin"
Placeholder="/usr/bin/wg-quick"/>
</div>
</div>
</div>
Expand All @@ -75,7 +83,8 @@
</div>
<div class="row">
<div class="col">
<RadzenTextBox class="w-100" Value="@Configuration.IptablesBin" Placeholder="/usr/sbin/iptables"/>
<RadzenTextBox class="w-100" @bind-Value="@Configuration.IptablesBin"
Placeholder="/usr/sbin/iptables"/>
</div>
</div>
</div>
Expand Down
6 changes: 0 additions & 6 deletions Linguard/Web/Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="NLog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<_ContentIncludedByDefault Remove="Pages\Components\SimpleAlert.razor" />
<_ContentIncludedByDefault Remove="wwwroot\ts\helper.ts" />
Expand Down

0 comments on commit 9d3ef58

Please sign in to comment.