From 6483bea96b7c372762161642a99c97a91fb082fd Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Fri, 8 Sep 2023 22:25:12 -0500 Subject: [PATCH] Defaults in settings model. Allow force load of settings from file --- LANCommander/Models/Settings.cs | 12 ++++---- LANCommander/Program.cs | 2 +- LANCommander/Services/SettingService.cs | 39 ++++++++++++------------- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/LANCommander/Models/Settings.cs b/LANCommander/Models/Settings.cs index 95ead7b..3554537 100644 --- a/LANCommander/Models/Settings.cs +++ b/LANCommander/Models/Settings.cs @@ -10,10 +10,10 @@ { public int Port { get; set; } = 1337; public bool Beacon { get; set; } = true; - public string DatabaseConnectionString { get; set; } = ""; + public string DatabaseConnectionString { get; set; } = "Data Source=LANCommander.db;Cache=Shared"; public string IGDBClientId { get; set; } = ""; public string IGDBClientSecret { get; set; } = ""; - public LANCommanderTheme Theme { get; set; } + public LANCommanderTheme Theme { get; set; } = LANCommanderTheme.Light; public LANCommanderAuthenticationSettings Authentication { get; set; } = new LANCommanderAuthenticationSettings(); } @@ -23,10 +23,10 @@ public bool RequireApproval { get; set; } = false; public string TokenSecret { get; set; } = ""; public int TokenLifetime { get; set; } = 30; - public bool PasswordRequireNonAlphanumeric { get; set; } - public bool PasswordRequireLowercase { get; set; } - public bool PasswordRequireUppercase { get; set; } - public bool PasswordRequireDigit { get; set; } + public bool PasswordRequireNonAlphanumeric { get; set; } = false; + public bool PasswordRequireLowercase { get; set; } = false; + public bool PasswordRequireUppercase { get; set; } = false; + public bool PasswordRequireDigit { get; set; } = true; public int PasswordRequiredLength { get; set; } = 8; } } diff --git a/LANCommander/Program.cs b/LANCommander/Program.cs index 2dac2da..cfe8675 100644 --- a/LANCommander/Program.cs +++ b/LANCommander/Program.cs @@ -19,7 +19,7 @@ ConfigurationManager configuration = builder.Configuration; // Add services to the container. Logger.Debug("Loading settings"); -var settings = SettingService.GetSettings(); +var settings = SettingService.GetSettings(true); Logger.Debug("Loaded!"); Logger.Debug("Configuring MVC and Blazor"); diff --git a/LANCommander/Services/SettingService.cs b/LANCommander/Services/SettingService.cs index c5170f6..7dddc7a 100644 --- a/LANCommander/Services/SettingService.cs +++ b/LANCommander/Services/SettingService.cs @@ -8,7 +8,9 @@ namespace LANCommander.Services { private const string SettingsFilename = "Settings.yml"; - public static LANCommanderSettings GetSettings() + private static LANCommanderSettings Settings { get; set; } + + public static LANCommanderSettings LoadSettings() { if (File.Exists(SettingsFilename)) { @@ -19,31 +21,24 @@ namespace LANCommander.Services .WithNamingConvention(PascalCaseNamingConvention.Instance) .Build(); - return deserializer.Deserialize(contents); + Settings = deserializer.Deserialize(contents); } else { - var settings = new LANCommanderSettings - { - Port = 1337, - Beacon = true, - DatabaseConnectionString = "Data Source=LANCommander.db;Cache=Shared", - Authentication = new LANCommanderAuthenticationSettings - { - TokenSecret = Guid.NewGuid().ToString(), - TokenLifetime = 30, - PasswordRequireNonAlphanumeric = false, - PasswordRequireLowercase = false, - PasswordRequireUppercase = false, - PasswordRequireDigit = true, - PasswordRequiredLength = 6 - } - }; + Settings = new LANCommanderSettings(); - SaveSettings(settings); - - return settings; + SaveSettings(Settings); } + + return Settings; + } + + public static LANCommanderSettings GetSettings(bool forceLoad = false) + { + if (Settings == null || forceLoad) + Settings = LoadSettings(); + + return Settings; } public static void SaveSettings(LANCommanderSettings settings) @@ -53,6 +48,8 @@ namespace LANCommander.Services .Build(); File.WriteAllText(SettingsFilename, serializer.Serialize(settings)); + + Settings = settings; } } }