diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-12-08 08:19:13 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-08 08:19:13 -0600 |
| commit | ec60cfbd397badca9a76d3923c653ad7cb6561c5 (patch) | |
| tree | 7da287e6ece3104bdf0dcd17757c584a3ad6c46a /src/slskd | |
| parent | 39e26f9a940400102bb0b86e259d836362a95cea (diff) | |
| parent | 75b8075f480be97209435ad454d9c23d03d0b6c9 (diff) | |
| download | slskd-ec60cfbd397badca9a76d3923c653ad7cb6561c5.tar.gz | |
Merge pull request #297 from slskd/controls
Add controls for restarting and shutting down, add button for version check
Diffstat (limited to 'src/slskd')
| -rw-r--r-- | src/slskd/Application.cs | 19 | ||||
| -rw-r--r-- | src/slskd/Core/API/Controllers/ApplicationController.cs | 24 | ||||
| -rw-r--r-- | src/slskd/Core/API/Controllers/ConfigurationController.cs | 76 | ||||
| -rw-r--r-- | src/slskd/Integrations/Pushbullet/PushbulletService.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 55 | ||||
| -rw-r--r-- | src/slskd/State.cs | 4 |
6 files changed, 74 insertions, 106 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index d0cbe361..021a6c40 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -141,10 +141,17 @@ namespace slskd /// <returns>The operation context.</returns> public async Task CheckVersionAsync() { - if (Program.InformationalVersion.EndsWith("65534")) + if (Program.IsDevelopment) + { + Logger.Information("Skipping version check for Development build"); + return; + } + + if (Program.IsCanary) { // todo: use the docker hub API to find the latest canary tag Logger.Information("Skipping version check for Canary build; check for updates manually."); + return; } Logger.Information("Checking GitHub Releases for latest version"); @@ -154,17 +161,17 @@ namespace slskd var latestVersion = await GitHub.GetLatestReleaseVersion( organization: Program.AppName, repository: Program.AppName, - userAgent: $"{Program.AppName} v{Program.InformationalVersion}"); + userAgent: $"{Program.AppName} v{Program.FullVersion}"); - if (latestVersion > Version.Parse(Program.InformationalVersion)) + if (latestVersion > Version.Parse(Program.SemanticVersion)) { State.SetValue(state => state with { Version = state.Version with { Latest = latestVersion.ToString(), IsUpdateAvailable = true } }); - Logger.Information("A new version is available! {CurrentVersion} -> {LatestVersion}", Program.InformationalVersion, latestVersion); + Logger.Information("A new version is available! {CurrentVersion} -> {LatestVersion}", Program.SemanticVersion, latestVersion); } else { - State.SetValue(state => state with { Version = state.Version with { Latest = Program.InformationalVersion, IsUpdateAvailable = false } }); - Logger.Information("Version {CurrentVersion} is up to date.", Program.InformationalVersion); + State.SetValue(state => state with { Version = state.Version with { Latest = Program.SemanticVersion, IsUpdateAvailable = false } }); + Logger.Information("Version {CurrentVersion} is up to date.", Program.SemanticVersion); } } catch (Exception ex) diff --git a/src/slskd/Core/API/Controllers/ApplicationController.cs b/src/slskd/Core/API/Controllers/ApplicationController.cs index 82b1d6c5..501f8a9c 100644 --- a/src/slskd/Core/API/Controllers/ApplicationController.cs +++ b/src/slskd/Core/API/Controllers/ApplicationController.cs @@ -69,6 +69,13 @@ namespace slskd.Core.API public IActionResult Shutdown() { Lifetime.StopApplication(); + + Task.Run(async () => + { + await Task.Delay(500); + Environment.Exit(0); + }); + return NoContent(); } @@ -94,7 +101,7 @@ namespace slskd.Core.API [Authorize] public IActionResult GetVersion() { - return Ok(Program.InformationalVersion); + return Ok(Program.SemanticVersion); } /// <summary> @@ -126,5 +133,20 @@ namespace slskd.Core.API return Ok(); } + + [HttpPut] + [Route("shares")] + [Authorize] + public IActionResult RescanSharesAsync() + { + if (ApplicationStateMonitor.CurrentValue.SharedFileCache.Filling) + { + return Conflict("A share scan is already in progress."); + } + + _ = Application.RescanSharesAsync(); + + return Ok(); + } } } diff --git a/src/slskd/Core/API/Controllers/ConfigurationController.cs b/src/slskd/Core/API/Controllers/ConfigurationController.cs deleted file mode 100644 index a7925174..00000000 --- a/src/slskd/Core/API/Controllers/ConfigurationController.cs +++ /dev/null @@ -1,76 +0,0 @@ -// <copyright file="ConfigurationController.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.Core.API -{ - using System; - using Microsoft.AspNetCore.Authorization; - using Microsoft.AspNetCore.Mvc; - using Serilog; - using IOFile = System.IO.File; - - /// <summary> - /// Configuration. - /// </summary> - [Route("api/v{version:apiVersion}/[controller]")] - [ApiVersion("0")] - [ApiController] - [Produces("application/json")] - [Consumes("application/json")] - public class ConfigurationController : ControllerBase - { - public ConfigurationController( - IApplication application, - IOptionsSnapshot<Options> optionsShapshot, - IStateMonitor<State> applicationStateMonitor) - { - Application = application; - OptionsSnapshot = optionsShapshot; - ApplicationStateMonitor = applicationStateMonitor; - } - - private IApplication Application { get; } - private IOptionsSnapshot<Options> OptionsSnapshot { get; } - private IStateMonitor<State> ApplicationStateMonitor { get; } - private ILogger Logger { get; set; } = Log.ForContext(typeof(Program)); - - [HttpGet] - [Route("")] - [Authorize] - public IActionResult GetOptions() - { - return Ok(OptionsSnapshot.Value); - } - - [HttpPut] - [Route("shares")] - [Authorize] - public IActionResult RescanSharesAsync() - { - if (ApplicationStateMonitor.CurrentValue.SharedFileCache.Filling) - { - return Conflict("A share scan is already in progress."); - } - - _ = Application.RescanSharesAsync(); - - return Ok(); - } - } -} diff --git a/src/slskd/Integrations/Pushbullet/PushbulletService.cs b/src/slskd/Integrations/Pushbullet/PushbulletService.cs index 1ec092eb..5328f4d6 100644 --- a/src/slskd/Integrations/Pushbullet/PushbulletService.cs +++ b/src/slskd/Integrations/Pushbullet/PushbulletService.cs @@ -140,7 +140,7 @@ namespace slskd.Integrations.Pushbullet using var http = HttpClientFactory.CreateClient(); - http.DefaultRequestHeaders.UserAgent.TryParseAdd($"{Program.AppName} v{Program.Version}"); + http.DefaultRequestHeaders.UserAgent.TryParseAdd($"{Program.AppName} v{Program.FullVersion}"); Log.LogDebug("Sending Pushbullet notification {Title} {Body}", title, body); diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index e4257037..b109c76b 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -74,45 +74,49 @@ namespace slskd public static readonly string DefaultAppDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.DoNotVerify), AppName); /// <summary> - /// Gets the assembly version of the application. + /// Gets the unique Id of this application invocation. /// </summary> + public static readonly Guid InvocationId = Guid.NewGuid(); + + /// <summary> + /// Gets the Id of the current application process. + /// </summary> + public static readonly int ProcessId = Environment.ProcessId; + /// <remarks> /// Inaccurate when running locally. /// </remarks> - public static readonly Version AssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.Equals(new Version(1, 0, 0, 0)) ? new Version(0, 0, 0, 0) : Assembly.GetExecutingAssembly().GetName().Version; + private static readonly Version AssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.Equals(new Version(1, 0, 0, 0)) ? new Version(0, 0, 0, 0) : Assembly.GetExecutingAssembly().GetName().Version; - /// <summary> - /// Gets the informational version of the application, including the git sha at the latest commit. - /// </summary> /// <remarks> /// Inaccurate when running locally. /// </remarks> - public static readonly string InformationalVersion = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion == "1.0.0" ? "0.0.0" : Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion; + private static readonly string InformationalVersion = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion == "1.0.0" ? "0.0.0" : Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion; /// <summary> - /// Gets the full application version, including both assembly and informational versions. + /// Occurs when a new log event is emitted. /// </summary> - public static readonly string Version = $"{AssemblyVersion} ({InformationalVersion})"; + public static event EventHandler<LogRecord> LogEmitted; /// <summary> - /// Gets a value indicating whether the current version is a Canary build. + /// Gets the semantic application version. /// </summary> - public static readonly bool IsCanary = AssemblyVersion.Revision == 65534; + public static string SemanticVersion => InformationalVersion.Split('-').First(); /// <summary> - /// Gets the unique Id of this application invocation. + /// Gets the full application version, including both assembly and informational versions. /// </summary> - public static readonly Guid InvocationId = Guid.NewGuid(); + public static string FullVersion => $"{SemanticVersion} ({InformationalVersion})"; /// <summary> - /// Gets the Id of the current application process. + /// Gets a value indicating whether the current version is a Canary build. /// </summary> - public static readonly int ProcessId = Environment.ProcessId; + public static bool IsCanary => AssemblyVersion.Revision == 65534; /// <summary> - /// Occurs when a new log event is emitted. + /// Gets a value indicating whether the current version is a Development build. /// </summary> - public static event EventHandler<LogRecord> LogEmitted; + public static bool IsDevelopment => new Version(0, 0, 0, 0) == AssemblyVersion; /// <summary> /// Gets the path where application data is saved. @@ -172,7 +176,7 @@ namespace slskd if (ShowVersion) { - Console.WriteLine(Version); + Console.WriteLine(FullVersion); return; } @@ -180,7 +184,7 @@ namespace slskd { if (!NoLogo) { - PrintLogo(Version); + PrintLogo(FullVersion); } if (ShowHelp) @@ -249,7 +253,6 @@ namespace slskd .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("System.Net.Http.HttpClient", OptionsAtStartup.Debug ? LogEventLevel.Warning : LogEventLevel.Fatal) .MinimumLevel.Override("slskd.Authentication.PassthroughAuthenticationHandler", LogEventLevel.Warning) - .Enrich.WithProperty("Version", Version) .Enrich.WithProperty("InstanceName", OptionsAtStartup.InstanceName) .Enrich.WithProperty("InvocationId", InvocationId) .Enrich.WithProperty("ProcessId", ProcessId) @@ -285,10 +288,15 @@ namespace slskd if (!OptionsAtStartup.NoLogo) { - PrintLogo(Version); + PrintLogo(FullVersion); } - logger.Information("Version: {Version}", Version); + logger.Information("Version: {Version}", FullVersion); + + if (IsDevelopment) + { + logger.Warning("This is a Development build; YMMV"); + } if (IsCanary) { @@ -558,6 +566,11 @@ namespace slskd │ │ │{centeredVersion}│"; + if (IsDevelopment) + { + banner += "\n│■■■■■■■■■■■■■■■■■■■■► DEVELOPMENT ◄■■■■■■■■■■■■■■■■■■■■■│"; + } + if (IsCanary) { banner += "\n│■■■■■■■■■■■■■■■■■■■■■■■► CANARY ◄■■■■■■■■■■■■■■■■■■■■■■■│"; diff --git a/src/slskd/State.cs b/src/slskd/State.cs index bff2dc5b..d07c603a 100644 --- a/src/slskd/State.cs +++ b/src/slskd/State.cs @@ -38,10 +38,12 @@ namespace slskd public record VersionState { - public string Current { get; init; } = Program.InformationalVersion; + public string Full { get; init; } = Program.FullVersion; + public string Current { get; init; } = Program.SemanticVersion; public string Latest { get; init; } = null; public bool? IsUpdateAvailable { get; init; } = null; public bool IsCanary { get; init; } = Program.IsCanary; + public bool IsDevelopment { get; init; } = Program.IsDevelopment; } public record ServerState |
