163 lines
5.7 KiB
Text
163 lines
5.7 KiB
Text
@page "/FirstTimeSetup"
|
|
@page "/FirstTimeSetup/Database"
|
|
@layout FirstTimeSetupLayout
|
|
@using LANCommander.SDK
|
|
@using LANCommander.Server.Settings
|
|
@using LANCommander.Server.Settings.Enums
|
|
@using Microsoft.Data.Sqlite
|
|
@using Microsoft.Extensions.Options
|
|
@using MySqlConnector
|
|
@using Npgsql
|
|
@inject SetupService SetupService
|
|
@inject SettingsProvider<Settings> SettingsProvider
|
|
@inject IOptions<Settings> Settings
|
|
@inject NavigationManager NavigationManager
|
|
@inject IMessageService MessageService
|
|
@inject ILogger<Index> Logger
|
|
|
|
<PageTitle>Database Config - First Time Setup</PageTitle>
|
|
|
|
<Form Model="@Settings.Value" Loading="Loading" OnFinish="ValidateDatabaseConnection" Layout="FormLayout.Vertical">
|
|
<FormItem>
|
|
LANCommander requires a database to run. If you require something other than SQLite, configure that here.
|
|
</FormItem>
|
|
<FormItem Label="Database Provider">
|
|
<Select
|
|
@bind-Value="context.Server.Database.Provider"
|
|
TItem="DatabaseProvider"
|
|
TItemValue="DatabaseProvider"
|
|
DataSource="Enum.GetValues<DatabaseProvider>()"
|
|
OnSelectedItemChanged="OnDatabaseProviderChanged">
|
|
<LabelTemplate Context="Value">@Value.GetDisplayName()</LabelTemplate>
|
|
<ItemTemplate Context="Value">@Value.GetDisplayName()</ItemTemplate>
|
|
</Select>
|
|
</FormItem>
|
|
<DatabaseConnectionStringEditor Provider="context.Server.Database.Provider" @bind-ConnectionString="context.Server.Database.ConnectionString" />
|
|
<FormItem WrapperColOffset="8" WrapperColSpan="16">
|
|
<GridRow Justify="RowJustify.End" Style="margin-top: 16px;">
|
|
<GridCol>
|
|
<Button OnClick="TestDatabaseConnection" Loading="Loading">
|
|
Test
|
|
</Button>
|
|
<Button Type="ButtonType.Primary" HtmlType="submit" Loading="Loading" Style="margin-left: 8px;">
|
|
Connect
|
|
</Button>
|
|
</GridCol>
|
|
</GridRow>
|
|
</FormItem>
|
|
</Form>
|
|
|
|
@code {
|
|
[CascadingParameter] FirstTimeSetupLayout Layout { get; set; }
|
|
|
|
bool Loading = false;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await Layout.ChangeCurrentStep(FirstTimeSetupStep.Database);
|
|
|
|
if (Settings.Value.Server.Database.Provider != DatabaseProvider.Unknown)
|
|
{
|
|
var isSetupInitialized = await SetupService.IsSetupInitialized();
|
|
if (isSetupInitialized)
|
|
NavigationManager.NavigateTo("/");
|
|
}
|
|
}
|
|
|
|
void OnDatabaseProviderChanged()
|
|
{
|
|
switch (Settings.Value.Server.Database.Provider)
|
|
{
|
|
case DatabaseProvider.SQLite:
|
|
var dbPath = AppPaths.GetConfigPath("LANCommander.db");
|
|
|
|
Settings.Value.Server.Database.ConnectionString = $"Data Source={dbPath};Cache=Shared";
|
|
break;
|
|
|
|
case DatabaseProvider.MySQL:
|
|
Settings.Value.Server.Database.ConnectionString = "Server=localhost;Uid=root;Pwd=password;Database=LANCommander";
|
|
break;
|
|
|
|
case DatabaseProvider.PostgreSQL:
|
|
Settings.Value.Server.Database.ConnectionString = "Host=localhost;Port=5432;Database=LANCommander;User Id=postgres;Password=password";
|
|
break;
|
|
}
|
|
}
|
|
|
|
bool TestConnection()
|
|
{
|
|
try
|
|
{
|
|
SetupService.ValidateConnectionString(Settings.Value.Server.Database.Provider, Settings.Value.Server.Database.ConnectionString);
|
|
|
|
return true;
|
|
}
|
|
catch (SqliteException ex)
|
|
{
|
|
Logger?.LogError(ex, "Could not use SQLite database connection");
|
|
MessageService.Error($"Could not use SQLite database: {ex.Message}", 10);
|
|
}
|
|
catch (MySqlException ex)
|
|
{
|
|
Logger?.LogError(ex, "Could not connect to MySQL database");
|
|
MessageService.Error($"Could not connect to MySQL database: {ex.Message}", 10);
|
|
}
|
|
catch (NpgsqlException ex)
|
|
{
|
|
Logger?.LogError(ex, "Could not connect to PostgreSQL database");
|
|
MessageService.Error($"Could not connect to PostgreSQL database: {ex.Message}", 10);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger?.LogError(ex, "An unknown error occurred trying to access the database", 10);
|
|
MessageService.Error("Could not validate the connection string!");
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
async Task TestDatabaseConnection()
|
|
{
|
|
Loading = true;
|
|
StateHasChanged();
|
|
await Task.Yield();
|
|
|
|
if (TestConnection())
|
|
MessageService.Success("Database connection successful!");
|
|
|
|
Loading = false;
|
|
}
|
|
|
|
async Task ValidateDatabaseConnection()
|
|
{
|
|
Loading = true;
|
|
StateHasChanged();
|
|
await Task.Yield();
|
|
|
|
if (TestConnection())
|
|
{
|
|
try
|
|
{
|
|
await InvokeAsync(async () =>
|
|
{
|
|
await SetupService.ChangeProviderAsync(Settings.Value.Server.Database.Provider, Settings.Value.Server.Database.ConnectionString);
|
|
|
|
SettingsProvider.Update(s =>
|
|
{
|
|
s.Server.Database.Provider = Settings.Value.Server.Database.Provider;
|
|
s.Server.Database.ConnectionString = Settings.Value.Server.Database.ConnectionString;
|
|
});
|
|
|
|
NavigationManager.NavigateTo("/FirstTimeSetup/Paths", true);
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger?.LogError(ex, "Could not initialize database!");
|
|
MessageService.Error($"Could not initialize database: {ex.Message}", 10);
|
|
}
|
|
}
|
|
|
|
Loading = false;
|
|
}
|
|
}
|