diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-12-20 21:53:17 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-20 21:53:17 -0600 |
| commit | 0aff8a7ba21927d051f4bd25eacb95084454567b (patch) | |
| tree | cd723221c378bcdb3c5e7de1ea6152f4476526bb | |
| parent | 93834bda3051a6edb4aefa495961ee387fe2b87b (diff) | |
| parent | f04146d0daab0f79abcb0cd5e660195d154128fa (diff) | |
| download | slskd-0aff8a7ba21927d051f4bd25eacb95084454567b.tar.gz | |
Merge pull request #311 from slskd/config-file
Allow the path to the configuration file to be specified
| -rw-r--r-- | docs/config.md | 2 | ||||
| -rw-r--r-- | src/slskd/Application.cs | 6 | ||||
| -rw-r--r-- | src/slskd/Core/API/Controllers/OptionsController.cs | 12 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 39 | ||||
| -rw-r--r-- | src/slskd/Startup.cs | 7 | ||||
| -rw-r--r-- | src/web/src/components/System/Options/Edit.js | 14 |
6 files changed, 60 insertions, 20 deletions
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 `<application directory>/slskd.yml` after environment variables. +Configuration is loaded from the YAML file located at `<application directory>/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. 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<Options> 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(); } } diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index 201869e1..062de8f7 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 update configuration file: {ex.Message}"); + } } [HttpPost] diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index b109c76b..126cff4c 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -101,44 +101,51 @@ namespace slskd /// <summary> /// Gets the semantic application version. /// </summary> - public static string SemanticVersion => InformationalVersion.Split('-').First(); + public static string SemanticVersion { get; } = InformationalVersion.Split('-').First(); /// <summary> /// Gets the full application version, including both assembly and informational versions. /// </summary> - public static string FullVersion => $"{SemanticVersion} ({InformationalVersion})"; + public static string FullVersion { get; } = $"{SemanticVersion} ({InformationalVersion})"; /// <summary> /// Gets a value indicating whether the current version is a Canary build. /// </summary> - public static bool IsCanary => AssemblyVersion.Revision == 65534; + public static bool IsCanary { get; } = AssemblyVersion.Revision == 65534; /// <summary> /// Gets a value indicating whether the current version is a Development build. /// </summary> - public static bool IsDevelopment => new Version(0, 0, 0, 0) == AssemblyVersion; + public static bool IsDevelopment { get; } = new Version(0, 0, 0, 0) == AssemblyVersion; /// <summary> /// Gets the path where application data is saved. /// </summary> - [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; } = DefaultAppDirectory; + public static string AppDirectory { get; private set; } = null; /// <summary> /// Gets the fully qualified path to the application configuration file. /// </summary> - 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; + + /// <summary> + /// Gets the default fully qualified path to the configuration file. + /// </summary> + public static string DefaultConfigurationFile { get; private set; } /// <summary> /// Gets the default downloads directory. /// </summary> - public static string DefaultDownloadsDirectory => Path.Combine(AppDirectory, "downloads"); + public static string DefaultDownloadsDirectory { get; private set; } /// <summary> /// Gets the default incomplete download directory. /// </summary> - public static string DefaultIncompleteDirectory => Path.Combine(AppDirectory, "incomplete"); + public static string DefaultIncompleteDirectory { get; private set; } /// <summary> /// Gets a buffer containing the last few log events. @@ -206,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 diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index 07c75fd3..f069c818 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; @@ -272,6 +274,11 @@ namespace slskd // DbContext explicitly. InitializeDbContexts(app); + app.UseExceptionHandler(a => a.Run(async context => + { + await context.Response.WriteAsJsonAsync(context.Features.Get<IExceptionHandlerPathFeature>().Error.Message); + })); + app.UseCors("AllowAll"); if (OptionsAtStartup.Web.Https.Force) 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 }) => { <Icon name='warning sign'/>Editing {location} </Message> <div - {...{ className: yamlError ? 'edit-code-container-error' : 'edit-code-container' }} + {...{ className: (yamlError || updateError) ? 'edit-code-container-error' : 'edit-code-container' }} > <CodeEditor value={yaml} onChange={(value) => update(value)} /> </div> - {yamlError && <Message className='no-grow' negative><Icon name='x'/>{yamlError}</Message>} + {(yamlError || updateError) && <Message className='no-grow' negative><Icon name='x'/>{(yamlError ?? '') + (updateError ?? '')}</Message>} <div className='footer-buttons'> <Button primary disabled={!isDirty} onClick={() => save(yaml)}><Icon name='save'/>Save</Button> <Button negative onClick={() => cancelAction()}><Icon name='close'/>Cancel</Button> |
