//
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
// │ Copyright (c) JP Dillingham.
// │
// │ 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, version 3.
// │
// │ 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/.
// │
// │ This program is distributed with Additional Terms pursuant to Section 7
// │ of the AGPLv3. See the LICENSE file in the root directory of this
// │ project for the complete terms and conditions.
// │
// │ https://slskd.org
// │
// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
// │ SPDX-FileCopyrightText: JP Dillingham
// │ SPDX-License-Identifier: AGPL-3.0-only
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
//
using Microsoft.Extensions.Options;
namespace slskd.Core.API
{
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using Serilog;
using slskd.Authentication;
///
/// Application.
///
[Route("api/v{version:apiVersion}/[controller]")]
[ApiVersion("0")]
[ApiController]
[Produces("application/json")]
[Consumes("application/json")]
public class ApplicationController : ControllerBase
{
public ApplicationController(
IHostApplicationLifetime lifetime,
IApplication application,
IOptionsMonitor optionsMonitor,
OptionsAtStartup optionsAtStartup,
IStateMonitor applicationStateMonitor)
{
Lifetime = lifetime;
Application = application;
OptionsMonitor = optionsMonitor;
OptionsAtStartup = optionsAtStartup;
ApplicationStateMonitor = applicationStateMonitor;
}
private IApplication Application { get; }
private IStateMonitor ApplicationStateMonitor { get; }
private IOptionsMonitor OptionsMonitor { get; }
private OptionsAtStartup OptionsAtStartup { get; }
private IHostApplicationLifetime Lifetime { get; }
private ILogger Log { get; } = Serilog.Log.ForContext();
///
/// Gets the current state of the application.
///
///
[HttpGet]
[Authorize(Policy = AuthPolicy.Any)]
public IActionResult State()
{
return Ok(ApplicationStateMonitor.CurrentValue);
}
///
/// Stops the application.
///
///
[HttpDelete]
[Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)]
public IActionResult Shutdown()
{
Program.MasterCancellationTokenSource.Cancel(swallowObjectDisposedException: true);
Lifetime.StopApplication();
Task.Run(async () =>
{
await Task.Delay(500);
Environment.Exit(0);
});
return NoContent();
}
///
/// Restarts the application.
///
///
[HttpPut]
[Authorize(Policy = AuthPolicy.JwtOnly, Roles = AuthRole.AdministratorOnly)]
public IActionResult Restart()
{
Program.MasterCancellationTokenSource.Cancel(swallowObjectDisposedException: true);
Process.Start(Process.GetCurrentProcess().MainModule.FileName, Environment.CommandLine);
Lifetime.StopApplication();
return NoContent();
}
///
/// Gets the current application version.
///
///
[HttpGet("version")]
[Authorize(Policy = AuthPolicy.Any)]
public IActionResult GetVersion()
{
return Ok(Program.SemanticVersion);
}
///
/// Checks for updates.
///
///
[HttpGet("version/latest")]
[Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.Any)]
public async Task CheckVersion([FromQuery] bool forceCheck = false)
{
if (forceCheck)
{
if (!User.IsInRole(nameof(Role.ReadWrite)) && !User.IsInRole(nameof(Role.Administrator)))
{
return Forbid();
}
await Application.CheckVersionAsync();
}
return Ok(ApplicationStateMonitor.CurrentValue.Version);
}
///
/// Forces garbage collection.
///
///
[ApiExplorerSettings(IgnoreApi = true)]
[HttpPost("gc")]
[Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.AdministratorOnly)]
public IActionResult CollectGarbage()
{
Application.CollectGarbage();
return Ok();
}
[ApiExplorerSettings(IgnoreApi = true)]
[HttpPost("loopback")]
[Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)]
public IActionResult Loopback([FromBody] object body)
{
Log.Information("Loopback POST: {Body}", body);
return Ok();
}
}
}