From a8274ce3b183de31d7be5188dacfd0fe1afb1f7f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 18 Dec 2021 16:30:44 -0600 Subject: add the ConfigurationFile property back --- src/slskd/Program.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index b109c76b..66729556 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -123,12 +123,19 @@ namespace slskd /// [Argument(default, "app-dir", "path where application data is saved")] [EnvironmentVariable("APP_DIR")] - public static string AppDirectory { get; private set; } = DefaultAppDirectory; + public static string AppDirectory { get; private set; } = null; /// /// Gets the fully qualified path to the application configuration file. /// - public static string ConfigurationFile => Path.Combine(AppDirectory, $"{AppName}.yml"); + [Argument('c', "config", "path to configuration file")] + [EnvironmentVariable("CONFIG")] + public static string ConfigurationFile { get; private set; } = null; + + /// + /// Gets the default fully qualified path to the configuration file. + /// + public static string DefaultConfigurationFile => Path.Combine(AppDirectory, $"{AppName}.yml"); /// /// Gets the default downloads directory. @@ -174,6 +181,9 @@ namespace slskd EnvironmentVariables.Populate(prefix: EnvironmentVariablePrefix); Arguments.Populate(clearExistingValues: false); + AppDirectory ??= DefaultAppDirectory; + ConfigurationFile ??= DefaultConfigurationFile; + if (ShowVersion) { Console.WriteLine(FullVersion); -- cgit v1.2.3 From d8a1661c1ef47a5645d1c9a2616c5a6c6e5c8e52 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 18 Dec 2021 16:30:57 -0600 Subject: wrap IO exceptions --- src/slskd/Core/API/Controllers/OptionsController.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index 201869e1..cb3eec36 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -20,6 +20,7 @@ using Microsoft.Extensions.Options; namespace slskd.Core.API { using System; + using System.IO; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Serilog; @@ -111,8 +112,15 @@ namespace slskd.Core.API return BadRequest(error); } - IOFile.WriteAllText(Program.ConfigurationFile, yaml); - return Ok(); + try + { + IOFile.WriteAllText(Program.ConfigurationFile, yaml); + return Ok(); + } + catch (Exception ex) + { + throw new IOException($"Failed to write configuration file {Program.ConfigurationFile}: {ex.Message}"); + } } [HttpPost] -- cgit v1.2.3 From 2b307601cd37a58e79090517bdb385bce98ff3ef Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 20 Dec 2021 09:03:45 -0600 Subject: fix initialization issues --- src/slskd/Program.cs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 66729556..29477120 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -101,22 +101,22 @@ namespace slskd /// /// Gets the semantic application version. /// - public static string SemanticVersion => InformationalVersion.Split('-').First(); + public static string SemanticVersion { get; } = InformationalVersion.Split('-').First(); /// /// Gets the full application version, including both assembly and informational versions. /// - public static string FullVersion => $"{SemanticVersion} ({InformationalVersion})"; + public static string FullVersion { get; } = $"{SemanticVersion} ({InformationalVersion})"; /// /// Gets a value indicating whether the current version is a Canary build. /// - public static bool IsCanary => AssemblyVersion.Revision == 65534; + public static bool IsCanary { get; } = AssemblyVersion.Revision == 65534; /// /// Gets a value indicating whether the current version is a Development build. /// - public static bool IsDevelopment => new Version(0, 0, 0, 0) == AssemblyVersion; + public static bool IsDevelopment { get; } = new Version(0, 0, 0, 0) == AssemblyVersion; /// /// Gets the path where application data is saved. @@ -135,17 +135,17 @@ namespace slskd /// /// Gets the default fully qualified path to the configuration file. /// - public static string DefaultConfigurationFile => Path.Combine(AppDirectory, $"{AppName}.yml"); + public static string DefaultConfigurationFile { get; private set; } /// /// Gets the default downloads directory. /// - public static string DefaultDownloadsDirectory => Path.Combine(AppDirectory, "downloads"); + public static string DefaultDownloadsDirectory { get; private set; } /// /// Gets the default incomplete download directory. /// - public static string DefaultIncompleteDirectory => Path.Combine(AppDirectory, "incomplete"); + public static string DefaultIncompleteDirectory { get; private set; } /// /// Gets a buffer containing the last few log events. @@ -181,9 +181,6 @@ namespace slskd EnvironmentVariables.Populate(prefix: EnvironmentVariablePrefix); Arguments.Populate(clearExistingValues: false); - AppDirectory ??= DefaultAppDirectory; - ConfigurationFile ??= DefaultConfigurationFile; - if (ShowVersion) { Console.WriteLine(FullVersion); @@ -216,7 +213,19 @@ namespace slskd return; } - // the application isn't being run in command mode. verify (create if needed) default application + // the application isn't being run in command mode. derive the application directory value + // and defaults that are dependent upon it + AppDirectory ??= DefaultAppDirectory; + + DefaultConfigurationFile = Path.Combine(AppDirectory, $"{AppName}.yml"); + DefaultDownloadsDirectory = Path.Combine(AppDirectory, "downloads"); + DefaultIncompleteDirectory = Path.Combine(AppDirectory, "incomplete"); + + // the location of the configuration file might have been overriden by command line or envar. + // if not, set it to the default. + ConfigurationFile ??= DefaultConfigurationFile; + + // verify(create if needed) default application // directories. if the downloads or complete directories are overridden in config, those will be // validated after the config is loaded. try -- cgit v1.2.3 From 7e294935ab2e04bff7f57bbee894decb986d0971 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 20 Dec 2021 10:41:07 -0600 Subject: add UseExceptionHandler --- src/slskd/Startup.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index ac4a2789..765afa18 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -25,6 +25,8 @@ namespace slskd using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; + using Microsoft.AspNetCore.Diagnostics; + using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; @@ -271,6 +273,11 @@ namespace slskd // DbContext explicitly. InitializeDbContexts(app); + app.UseExceptionHandler(a => a.Run(async context => + { + await context.Response.WriteAsJsonAsync(context.Features.Get().Error.Message); + })); + app.UseCors("AllowAll"); if (OptionsAtStartup.Web.Https.Force) -- cgit v1.2.3 From 51d7c419fca778287d9a2510828eb66f0424777f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 20 Dec 2021 10:41:20 -0600 Subject: simplify exception message --- src/slskd/Core/API/Controllers/OptionsController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index cb3eec36..062de8f7 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -119,7 +119,7 @@ namespace slskd.Core.API } catch (Exception ex) { - throw new IOException($"Failed to write configuration file {Program.ConfigurationFile}: {ex.Message}"); + throw new IOException($"Failed to update configuration file: {ex.Message}"); } } -- cgit v1.2.3 From efed2c34a9266a58e8b729d60b0ae55278a7f779 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 20 Dec 2021 10:45:02 -0600 Subject: gracefully handle errors updating config --- src/web/src/components/System/Options/Edit.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/web/src/components/System/Options/Edit.js b/src/web/src/components/System/Options/Edit.js index a68b6fcf..54c0131f 100644 --- a/src/web/src/components/System/Options/Edit.js +++ b/src/web/src/components/System/Options/Edit.js @@ -10,6 +10,7 @@ const Edit = ({ cancelAction }) => { const [{ loading, error }, setLoading] = useState({ loading: true, error: false }); const [{ location, yaml, isDirty }, setYaml] = useState({ location: undefined, yaml: undefined, isDirty: false }); const [yamlError, setYamlError] = useState(); + const [updateError, setUpdateError] = useState(); useEffect(() => { get(); @@ -42,8 +43,13 @@ const Edit = ({ cancelAction }) => { await validate(yaml); if (!yamlError) { - await updateYaml({ yaml }); - cancelAction(); + try { + await updateYaml({ yaml }); + cancelAction(); + } + catch (error) { + setUpdateError(error.response.data); + } } } @@ -61,14 +67,14 @@ const Edit = ({ cancelAction }) => { Editing {location}
update(value)} />
- {yamlError && {yamlError}} + {(yamlError || updateError) && {(yamlError ?? '') + (updateError ?? '')}}
-- cgit v1.2.3 From d7617d461cb14eb593641df023c79634befab1c2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 20 Dec 2021 17:29:32 -0600 Subject: use a semaphore for options sync --- src/slskd/Application.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 6ce17bf5..d6ae4744 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -128,7 +128,7 @@ namespace slskd private Options Options => OptionsMonitor.CurrentValue; private OptionsAtStartup OptionsAtStartup { get; set; } private IOptionsMonitor OptionsMonitor { get; set; } - private ReaderWriterLockSlim OptionsSyncRoot { get; } = new ReaderWriterLockSlim(); + private SemaphoreSlim OptionsSyncRoot { get; } = new SemaphoreSlim(1, 1); private Options PreviousOptions { get; set; } private IPushbulletService Pushbullet { get; } private ISharedFileCache SharedFileCache { get; set; } @@ -565,7 +565,7 @@ namespace slskd // this code is known to fire more than once per update. i'm not sure whether these might be executed concurrently. // lock to be safe, because we need to accurately track the last value of Options for diffing purposes. threading // shenanigans here could lead to missed updates. - OptionsSyncRoot.EnterWriteLock(); + await OptionsSyncRoot.WaitAsync(); try { @@ -713,7 +713,7 @@ namespace slskd } finally { - OptionsSyncRoot.ExitWriteLock(); + OptionsSyncRoot.Release(); } } -- cgit v1.2.3 From cda89e6dcb7a518aa891f222e4b99d84c73d0068 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 20 Dec 2021 21:49:11 -0600 Subject: add short arg for app-dir --- src/slskd/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 29477120..126cff4c 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -121,7 +121,7 @@ namespace slskd /// /// Gets the path where application data is saved. /// - [Argument(default, "app-dir", "path where application data is saved")] + [Argument('a', "app-dir", "path where application data is saved")] [EnvironmentVariable("APP_DIR")] public static string AppDirectory { get; private set; } = null; -- cgit v1.2.3 From f04146d0daab0f79abcb0cd5e660195d154128fa Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 20 Dec 2021 21:49:23 -0600 Subject: add config options to docs --- docs/config.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/config.md b/docs/config.md index a5ace91f..47b4bf68 100644 --- a/docs/config.md +++ b/docs/config.md @@ -36,7 +36,7 @@ SLSKD_SOME_OPTION=1;2;3 ## YAML Configuration File -Configuration is loaded from the YAML file located at `/slskd.yml` after environment variables. +Configuration is loaded from the YAML file located at `/slskd.yml` after environment variables. The location can be changed by specifying the path in the `CONFIG` environment variable or `--config` command line argument. The application watches for changes in the YAML file and will reload configuration when they are detected. Options will be updated in real time and transmitted to the web UI. If a server reconnect or application restart is required for changes to fully take effect, a flag will be set indicating so. -- cgit v1.2.3