aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Application/Management
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-07-24 14:39:40 -0500
committerGitHub <noreply@github.com>2021-07-24 14:39:40 -0500
commit58bc401dab5d7eb3bc6cb07a22e0ee9c97ddf4bd (patch)
tree64072f5eaa896335cc04de8ffe84fa79a50a2be7 /src/slskd/Application/Management
parentfc7800409dfbe0cf26a72dd1de17cbae23d0cb35 (diff)
parent78b3b953d9c36c2cc2939ad00881b0cbf388865a (diff)
downloadslskd-58bc401dab5d7eb3bc6cb07a22e0ee9c97ddf4bd.tar.gz
Merge pull request #125 from slskd/options
Refactor configuration components to allow for run-time updates
Diffstat (limited to 'src/slskd/Application/Management')
-rw-r--r--src/slskd/Application/Management/API/Controllers/ApplicationController.cs4
-rw-r--r--src/slskd/Application/Management/API/Controllers/OptionsController.cs71
-rw-r--r--src/slskd/Application/Management/API/Controllers/SessionController.cs17
-rw-r--r--src/slskd/Application/Management/IManagementService.cs14
-rw-r--r--src/slskd/Application/Management/ManagementService.cs25
5 files changed, 103 insertions, 28 deletions
diff --git a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs
index f0121af9..ddac832a 100644
--- a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs
+++ b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs
@@ -51,8 +51,6 @@ namespace slskd.Management.API
[Authorize]
public IActionResult Shutdown()
{
- Client.Disconnect("Shut down via API");
-
Lifetime.StopApplication();
return NoContent();
}
@@ -65,8 +63,6 @@ namespace slskd.Management.API
[Authorize]
public IActionResult Restart()
{
- Client.Disconnect("Restarted via API");
-
Process.Start(Process.GetCurrentProcess().MainModule.FileName, Environment.CommandLine);
Lifetime.StopApplication();
diff --git a/src/slskd/Application/Management/API/Controllers/OptionsController.cs b/src/slskd/Application/Management/API/Controllers/OptionsController.cs
new file mode 100644
index 00000000..6f6275d1
--- /dev/null
+++ b/src/slskd/Application/Management/API/Controllers/OptionsController.cs
@@ -0,0 +1,71 @@
+// <copyright file="OptionsController.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+using Microsoft.Extensions.Options;
+
+namespace slskd.Management.API
+{
+ using Microsoft.AspNetCore.Authorization;
+ using Microsoft.AspNetCore.Mvc;
+
+ /// <summary>
+ /// Options.
+ /// </summary>
+ [Route("api/v{version:apiVersion}/[controller]")]
+ [ApiVersion("0")]
+ [ApiController]
+ [Produces("application/json")]
+ [Consumes("application/json")]
+ public class OptionsController : ControllerBase
+ {
+ public OptionsController(
+ OptionsAtStartup optionsAtStartup,
+ IOptionsSnapshot<Options> optionsSnapshot)
+ {
+ OptionsAtStartup = optionsAtStartup;
+ OptionsSnapshot = optionsSnapshot;
+ }
+
+ private IOptionsSnapshot<Options> OptionsSnapshot { get; }
+ private OptionsAtStartup OptionsAtStartup { get; }
+
+ /// <summary>
+ /// Gets the current application options.
+ /// </summary>
+ /// <returns></returns>
+ [HttpGet]
+ [Authorize]
+ [ProducesResponseType(typeof(Options), 200)]
+ public IActionResult Current()
+ {
+ return Ok(OptionsSnapshot.Value);
+ }
+
+ /// <summary>
+ /// Gets the application options provided at startup.
+ /// </summary>
+ /// <returns></returns>
+ [HttpGet]
+ [Route("startup")]
+ [Authorize]
+ [ProducesResponseType(typeof(Options), 200)]
+ public IActionResult Startup()
+ {
+ return Ok(OptionsAtStartup);
+ }
+ }
+}
diff --git a/src/slskd/Application/Management/API/Controllers/SessionController.cs b/src/slskd/Application/Management/API/Controllers/SessionController.cs
index 09a95c24..02e8257e 100644
--- a/src/slskd/Application/Management/API/Controllers/SessionController.cs
+++ b/src/slskd/Application/Management/API/Controllers/SessionController.cs
@@ -15,6 +15,8 @@
// along with this program. If not, see https://www.gnu.org/licenses/.
// </copyright>
+using Microsoft.Extensions.Options;
+
namespace slskd.Management.API
{
using System;
@@ -37,15 +39,18 @@ namespace slskd.Management.API
public class SessionController : ControllerBase
{
public SessionController(
- Microsoft.Extensions.Options.IOptionsSnapshot<Options> optionsSnapshot,
+ IOptionsSnapshot<Options> optionsSnapshot,
+ OptionsAtStartup optionsAtStartup,
SymmetricSecurityKey jwtSigningKey)
{
- Options = optionsSnapshot.Value;
+ OptionsSnapshot = optionsSnapshot;
+ OptionsAtStartup = optionsAtStartup;
JwtSigningKey = jwtSigningKey;
}
private SymmetricSecurityKey JwtSigningKey { get; set; }
- private Options Options { get; set; }
+ private IOptionsSnapshot<Options> OptionsSnapshot { get; set; }
+ private OptionsAtStartup OptionsAtStartup { get; set; }
/// <summary>
/// Checks whether the provided authentication is valid.
@@ -75,7 +80,7 @@ namespace slskd.Management.API
[ProducesResponseType(typeof(bool), 200)]
public IActionResult Enabled()
{
- return Ok(!Options.Web.Authentication.Disable);
+ return Ok(!OptionsAtStartup.Web.Authentication.Disable);
}
/// <summary>
@@ -106,7 +111,7 @@ namespace slskd.Management.API
}
// only admin login for now
- if (Options.Web.Authentication.Username == login.Username && Options.Web.Authentication.Password == login.Password)
+ if (OptionsSnapshot.Value.Web.Authentication.Username == login.Username && OptionsSnapshot.Value.Web.Authentication.Password == login.Password)
{
return Ok(new TokenResponse(GetJwtSecurityToken(login.Username, Role.Administrator)));
}
@@ -117,7 +122,7 @@ namespace slskd.Management.API
private JwtSecurityToken GetJwtSecurityToken(string username, Role role)
{
var issuedUtc = DateTime.UtcNow;
- var expiresUtc = DateTime.UtcNow.AddMilliseconds(Options.Web.Authentication.Jwt.Ttl);
+ var expiresUtc = DateTime.UtcNow.AddMilliseconds(OptionsAtStartup.Web.Authentication.Jwt.Ttl);
var claims = new List<Claim>()
{
diff --git a/src/slskd/Application/Management/IManagementService.cs b/src/slskd/Application/Management/IManagementService.cs
index 4ee2185e..b3e13477 100644
--- a/src/slskd/Application/Management/IManagementService.cs
+++ b/src/slskd/Application/Management/IManagementService.cs
@@ -26,6 +26,12 @@ namespace slskd.Management
public interface IManagementService
{
/// <summary>
+ /// Connects the Soulseek client to the server using the configured username and password.
+ /// </summary>
+ /// <returns>The operation context.</returns>
+ Task ConnectServerAsync();
+
+ /// <summary>
/// Disconnects the Soulseek client from the server.
/// </summary>
/// <param name="message">An optional message containing the reason for the disconnect.</param>
@@ -33,15 +39,9 @@ namespace slskd.Management
void DisconnectServer(string message = null, Exception exception = null);
/// <summary>
- /// Connects the Soulseek client to the server using the configured username and password.
- /// </summary>
- /// <returns>The operation context.</returns>
- Task ConnectServerAsync();
-
- /// <summary>
/// Gets the current state of the connection to the Soulseek server.
/// </summary>
/// <returns>The current server state.</returns>
ServerState GetServerState();
}
-}
+} \ No newline at end of file
diff --git a/src/slskd/Application/Management/ManagementService.cs b/src/slskd/Application/Management/ManagementService.cs
index 84935879..66ed2b97 100644
--- a/src/slskd/Application/Management/ManagementService.cs
+++ b/src/slskd/Application/Management/ManagementService.cs
@@ -15,6 +15,8 @@
// along with this program. If not, see https://www.gnu.org/licenses/.
// </copyright>
+using Microsoft.Extensions.Options;
+
namespace slskd.Management
{
using System;
@@ -32,15 +34,23 @@ namespace slskd.Management
/// <param name="optionsMonitor">The options monitor used to derive application options.</param>
/// <param name="soulseekClient">The Soulseek client.</param>
public ManagementService(
- Microsoft.Extensions.Options.IOptionsMonitor<Options> optionsMonitor,
+ IOptionsMonitor<Options> optionsMonitor,
ISoulseekClient soulseekClient)
{
- Options = optionsMonitor.CurrentValue;
+ OptionsMonitor = optionsMonitor;
Client = soulseekClient;
}
private ISoulseekClient Client { get; }
- private Options Options { get; }
+ private Options Options => OptionsMonitor.CurrentValue;
+ private IOptionsMonitor<Options> OptionsMonitor { get; }
+
+ /// <summary>
+ /// Connects the Soulseek client to the server using the configured username and password.
+ /// </summary>
+ /// <returns>The operation context.</returns>
+ public Task ConnectServerAsync()
+ => Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password);
/// <summary>
/// Disconnects the Soulseek client from the server.
@@ -51,13 +61,6 @@ namespace slskd.Management
=> Client.Disconnect(message, exception ?? new IntentionalDisconnectException(message));
/// <summary>
- /// Connects the Soulseek client to the server using the configured username and password.
- /// </summary>
- /// <returns>The operation context.</returns>
- public Task ConnectServerAsync()
- => Client.ConnectAsync(Options.Soulseek.Username, Options.Soulseek.Password);
-
- /// <summary>
/// Gets the current state of the connection to the Soulseek server.
/// </summary>
/// <returns>The current server state.</returns>
@@ -70,4 +73,4 @@ namespace slskd.Management
Username = Client.Username,
};
}
-}
+} \ No newline at end of file