diff options
Diffstat (limited to 'src/slskd/Core/API/Controllers')
| -rw-r--r-- | src/slskd/Core/API/Controllers/OptionsController.cs | 51 |
1 files changed, 46 insertions, 5 deletions
diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index 25e9095a..f5c1a5b6 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -53,7 +53,7 @@ namespace slskd.Core.API private IOptionsSnapshot<Options> OptionsSnapshot { get; } private OptionsAtStartup OptionsAtStartup { get; } private IStateMutator<State> StateMutator { get; } - private ILogger Logger { get; } = Log.ForContext(typeof(OptionsController)); + private ILogger Log { get; } = Serilog.Log.ForContext<OptionsController>(); /// <summary> /// Gets the current application options. @@ -68,6 +68,45 @@ namespace slskd.Core.API } /// <summary> + /// Overlay the application options with new values. + /// </summary> + /// <param name="overlay">The overlay to apply.</param> + /// <returns></returns> + [HttpPatch] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.AdministratorOnly)] + [ProducesResponseType(typeof(OptionsOverlay), 200)] + public IActionResult ApplyOverlay([FromBody] OptionsOverlay overlay) + { + if (!OptionsSnapshot.Value.RemoteConfiguration) + { + Log.Warning("Blocked options overlay application; remote configuration is disabled"); + return Forbid(); + } + + if (overlay is null) + { + return NoContent(); + } + + if (!overlay.TryValidate(out var result)) + { + Log.Warning("Options patch validation failed: {Message}", result.GetResultString()); + return BadRequest(result.GetResultString()); + } + + try + { + Program.ApplyConfigurationOverlay(overlay); + return Ok(Program.ConfigurationOverlay?.Redact()); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to apply options patch: {Message}", ex.Message); + return StatusCode(500, $"Failed to apply options patch: {ex.Message}"); + } + } + + /// <summary> /// Gets the application options provided at startup. /// </summary> /// <returns></returns> @@ -130,7 +169,7 @@ namespace slskd.Core.API return Ok(yaml); } - [HttpPost] + [HttpPut] [Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)] [Route("yaml")] public IActionResult UpdateYamlFile([FromBody] string yaml) @@ -142,17 +181,19 @@ namespace slskd.Core.API if (!TryValidateYaml(yaml, out var error)) { - Logger.Error(error, "Failed to validate YAML configuration"); + Log.Error(error, "Failed to validate YAML configuration"); return BadRequest(error); } try { + IOFile.Copy(Program.ConfigurationFile, $"{Program.ConfigurationFile}.bak", overwrite: true); + IOFile.WriteAllText(Program.ConfigurationFile, yaml); if (OptionsSnapshot.Value.Flags.NoConfigWatch) { - Logger.Information("Configuration watch is disabled; restart required for changes to take effect"); + Log.Information("Configuration watch is disabled; restart required for changes to take effect"); StateMutator.SetValue(state => state with { PendingRestart = true }); } @@ -203,7 +244,7 @@ namespace slskd.Core.API } catch (Exception ex) { - Logger.Warning(ex, "Configuration validation failed"); + Log.Warning(ex, "Configuration validation failed"); error = ex.Message; |
