From ab804239bb0d772df3424a8fbd47bedf516f0307 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 27 Jul 2025 18:19:01 +0100 Subject: [PATCH] [Blazor] Use settings from appsettings.json to configure folders and log levels. --- Directory.Packages.props | 101 +++++++++--------- .../Components/Dialogs/ImportDats.razor | 2 +- .../Components/Dialogs/ImportDats.razor.cs | 3 +- .../Components/Dialogs/ImportRoms.razor | 2 +- .../Components/Dialogs/ImportRoms.razor.cs | 3 +- RomRepoMgr.Blazor/Consts.cs | 11 -- RomRepoMgr.Blazor/Program.cs | 92 ++++++++++------ RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj | 1 + RomRepoMgr.Blazor/appsettings.json | 17 ++- 9 files changed, 130 insertions(+), 102 deletions(-) delete mode 100644 RomRepoMgr.Blazor/Consts.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 725ec64..aa2bf6d 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -1,54 +1,55 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor b/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor index aa19ab2..f3a4209 100644 --- a/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor +++ b/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor @@ -1,8 +1,8 @@ -@using Microsoft.Extensions.Localization @using RomRepoMgr.Blazor.Resources @implements IDialogContentComponent @inject ILogger Logger @inject IStringLocalizer Localizer +@inject IConfiguration Configuration diff --git a/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor.cs b/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor.cs index 9a86e89..912fe1f 100644 --- a/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor.cs +++ b/RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor.cs @@ -1,6 +1,5 @@ using System.Diagnostics; using Microsoft.AspNetCore.Components; -using Microsoft.FluentUI.AspNetCore.Components; using RomRepoMgr.Core.EventArgs; using RomRepoMgr.Core.Workers; using Serilog; @@ -37,7 +36,7 @@ public partial class ImportDats : ComponentBase { base.OnInitialized(); - path = Path.Combine(Environment.CurrentDirectory, Consts.IncomingDatFolder); + path = Configuration["DataFolders:ImportDats"] ?? "incoming-dats"; StatusMessage = ""; StatusMessage2 = ""; IsBusy = false; diff --git a/RomRepoMgr.Blazor/Components/Dialogs/ImportRoms.razor b/RomRepoMgr.Blazor/Components/Dialogs/ImportRoms.razor index 328a0b6..280eff3 100644 --- a/RomRepoMgr.Blazor/Components/Dialogs/ImportRoms.razor +++ b/RomRepoMgr.Blazor/Components/Dialogs/ImportRoms.razor @@ -1,10 +1,10 @@ -@using Microsoft.Extensions.Localization @using RomRepoMgr.Blazor.Resources @using RomRepoMgr.Database @implements IDialogContentComponent @inject ILogger Logger @inject Context _ctx @inject IStringLocalizer Localizer +@inject IConfiguration Configuration diff --git a/RomRepoMgr.Blazor/Components/Dialogs/ImportRoms.razor.cs b/RomRepoMgr.Blazor/Components/Dialogs/ImportRoms.razor.cs index 789fa89..662984a 100644 --- a/RomRepoMgr.Blazor/Components/Dialogs/ImportRoms.razor.cs +++ b/RomRepoMgr.Blazor/Components/Dialogs/ImportRoms.razor.cs @@ -1,7 +1,6 @@ using System.Collections.Concurrent; using System.Diagnostics; using Microsoft.AspNetCore.Components; -using Microsoft.FluentUI.AspNetCore.Components; using RomRepoMgr.Core.EventArgs; using RomRepoMgr.Core.Workers; using RomRepoMgr.Database.Models; @@ -45,7 +44,7 @@ public partial class ImportRoms : ComponentBase protected override void OnInitialized() { base.OnInitialized(); - FolderPath = Path.Combine(Environment.CurrentDirectory, Consts.IncomingRomsFolder); + FolderPath = Configuration["DataFolders:ImportRoms"] ?? "incoming"; IsBusy = false; NotYetStarted = true; RemoveFilesChecked = false; diff --git a/RomRepoMgr.Blazor/Consts.cs b/RomRepoMgr.Blazor/Consts.cs deleted file mode 100644 index 0f12f44..0000000 --- a/RomRepoMgr.Blazor/Consts.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace RomRepoMgr.Blazor; - -public class Consts -{ - public const string DbFolder = "db"; - public const string DatFolder = "dats"; - public const string IncomingDatFolder = "incoming-dats"; - public const string RepositoryFolder = "repo"; - public const string IncomingRomsFolder = "incoming"; - public const string TemporaryFolder = "tmp"; -} \ No newline at end of file diff --git a/RomRepoMgr.Blazor/Program.cs b/RomRepoMgr.Blazor/Program.cs index 45f8225..725928a 100644 --- a/RomRepoMgr.Blazor/Program.cs +++ b/RomRepoMgr.Blazor/Program.cs @@ -1,12 +1,40 @@ using System.Diagnostics; -using Microsoft.EntityFrameworkCore; -using Microsoft.FluentUI.AspNetCore.Components; -using RomRepoMgr.Blazor; using RomRepoMgr.Blazor.Components; using RomRepoMgr.Database; using RomRepoMgr.Settings; using Serilog; +using Serilog.Events; +// Start the application +Log.Logger = new LoggerConfiguration().MinimumLevel.Debug().WriteTo.Console().Enrich.FromLogContext().CreateLogger(); + +Log.Information("Welcome to ROM Repository Manager!"); +Log.Information("Copyright © 2020-2025 Natalia Portillo"); + +// Configuration and settings +// We need the builder now +Log.Debug("Creating the builder..."); +WebApplicationBuilder builder = WebApplication.CreateBuilder(args); + +// Access the full configuration +Log.Debug("Creating the configuration reader..."); +ConfigurationManager config = builder.Configuration; + +string logFile = config["LogFile"] ?? "logs/rom-repo-mgr.log"; +string defaultLogLevel = config["Logging:LogLevel:Default"] ?? "Information"; + +// Parse to LogEventLevel +if(!Enum.TryParse(defaultLogLevel, true, out LogEventLevel level)) +{ + // Fallback if parsing fails +#if DEBUG + level = LogEventLevel.Debug; +#else + level = LogEventLevel.Information; +#endif +} + +// Now create a logger with the specified log level and log file Log.Logger = new LoggerConfiguration() #if DEBUG .MinimumLevel.Debug() @@ -14,23 +42,37 @@ Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() #endif .WriteTo.Console() + .WriteTo.File(logFile, rollingInterval: RollingInterval.Day, fileSizeLimitBytes: 10 * 1048576) .Enrich.FromLogContext() .CreateLogger(); -Log.Information("Welcome to ROM Repository Manager!"); -Log.Information("Copyright © 2020-2025 Natalia Portillo"); +// Read the rest of the configuration and settings +Log.Debug("Reading configuration settings..."); +string repoFolder = config["DataFolders:Repository"] ?? "repo"; +string importRoms = config["DataFolders:ImportRoms"] ?? "incoming"; +string importDats = config["DataFolders:ImportDats"] ?? "incoming-dats"; +string exportRoms = config["DataFolders:ExportRoms"] ?? "export"; +string exportDats = config["DataFolders:ExportDats"] ?? "export-dats"; +string databaseFolder = config["DataFolders:Database"] ?? "db"; +string temporaryFolder = config["DataFolders:Temporary"] ?? "tmp"; +string compressionTypeString = config["CompressionType"] ?? "Zstd"; + +// Parse the compression type +if(!Enum.TryParse(compressionTypeString, true, out CompressionType compressionType)) +{ + // Fallback if parsing fails + compressionType = CompressionType.Zstd; +} // Ensure the folders exist Log.Information("Ensuring folders exist..."); -string[] folders = -[ - Consts.DbFolder, Consts.DatFolder, Consts.IncomingDatFolder, Consts.RepositoryFolder, Consts.IncomingRomsFolder -]; +string[] folders = [repoFolder, importRoms, importDats, databaseFolder, exportRoms, exportDats, temporaryFolder]; foreach(string folder in folders) { - if(!Directory.Exists(folder)) + // Check File.Exists for symlinks or junctions + if(!Directory.Exists(folder) && !File.Exists(folder)) { Log.Debug("Creating folder: {Folder}", folder); Directory.CreateDirectory(folder); @@ -39,19 +81,8 @@ foreach(string folder in folders) Log.Debug("Folder already exists: {Folder}", folder); } -// Ensure the temporary folder exists but it can also be a symlink -if(!Directory.Exists(Consts.TemporaryFolder) && !File.Exists(Consts.TemporaryFolder)) -{ - Log.Debug("Creating folder: {TemporaryFolder}", Consts.TemporaryFolder); - Directory.CreateDirectory(Consts.TemporaryFolder); -} -else - Log.Debug("Folder already exists: {TemporaryFolder}", Consts.TemporaryFolder); - -Log.Debug("Creating the builder..."); -WebApplicationBuilder builder = WebApplication.CreateBuilder(args); - -builder.Host.UseSerilog(); // ✅ Plug Serilog into the host +// ✅ Plug Serilog into the host +builder.Host.UseSerilog(); // Add services to the container. builder.Services.AddRazorComponents().AddInteractiveServerComponents(); @@ -64,7 +95,7 @@ Log.Debug("Creating database context..."); builder.Services.AddDbContextFactory(options => { - options.UseSqlite($"Data Source={Consts.DbFolder}/database.db"); + options.UseSqlite($"Data Source={databaseFolder}/database.db"); #if DEBUG options.EnableSensitiveDataLogging(); options.LogTo(Log.Debug); @@ -79,11 +110,11 @@ Log.Debug("Setting the settings..."); Settings.Current = new SetSettings { - DatabasePath = Path.Combine(Environment.CurrentDirectory, Consts.DbFolder, "database.db"), - TemporaryFolder = Path.Combine(Environment.CurrentDirectory, Consts.TemporaryFolder), - RepositoryPath = Path.Combine(Environment.CurrentDirectory, Consts.RepositoryFolder), + DatabasePath = Path.Combine(Environment.CurrentDirectory, databaseFolder, "database.db"), + TemporaryFolder = Path.Combine(Environment.CurrentDirectory, temporaryFolder), + RepositoryPath = Path.Combine(Environment.CurrentDirectory, repoFolder), UseInternalDecompressor = true, - Compression = CompressionType.Zstd // Todo: Read from configuration + Compression = compressionType }; Log.Debug("Building the application..."); @@ -104,10 +135,7 @@ app.MapStaticAssets(); app.MapRazorComponents().AddInteractiveServerRenderMode(); // Localization -string[] supportedCultures = -[ - "en", "es" -]; +string[] supportedCultures = ["en", "es"]; RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions().SetDefaultCulture("en") .AddSupportedCultures(supportedCultures) diff --git a/RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj b/RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj index cf0cf03..fd05696 100644 --- a/RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj +++ b/RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj @@ -20,6 +20,7 @@ + diff --git a/RomRepoMgr.Blazor/appsettings.json b/RomRepoMgr.Blazor/appsettings.json index 39dbe23..4faa1c7 100644 --- a/RomRepoMgr.Blazor/appsettings.json +++ b/RomRepoMgr.Blazor/appsettings.json @@ -1,9 +1,20 @@ { - "Logging": { + "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" -} + "LogFile": "logs/romrepomgr.log", + "AllowedHosts": "*", + "DataFolders": { + "Repository": "repo", + "ImportRoms": "incoming", + "ImportDats": "incoming-dats", + "ExportRoms": "export", + "ExportDats": "export-dats", + "Database": "db", + "Temporary": "tmp" + }, + "CompressionType": "Zstd" +} \ No newline at end of file