aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Application/Management/API
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/API
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/API')
-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
3 files changed, 82 insertions, 10 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>()
{