Skip to content

Commit af8a341

Browse files
committed
Use pattern matching & move admin endpoint metadata to a dedicated static helper
1 parent 562133a commit af8a341

7 files changed

Lines changed: 37 additions & 35 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Gommon;
2+
3+
namespace Ryujinx.Systems.Update.Server.Controllers.Admin;
4+
5+
internal static class AdminEndpointMetadata
6+
{
7+
public static bool Enabled { get; private set; }
8+
public static string AccessToken { get; private set; } = string.Empty;
9+
10+
public static void Set(string? accessToken)
11+
{
12+
Enabled = !accessToken.IsNullOrEmpty();
13+
14+
AccessToken = Enabled ? accessToken! : string.Empty;
15+
}
16+
}

src/Server/Controllers/Api/v1/Admin/RefreshCacheController.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
using System.Diagnostics.CodeAnalysis;
2+
using Gommon;
23
using Microsoft.AspNetCore.Mvc;
34
using Ryujinx.Systems.Update.Common;
4-
using Ryujinx.Systems.Update.Server;
5-
using Ryujinx.Systems.Update.Server.Services.GitLab;
65

76
namespace Ryujinx.Systems.Update.Server.Controllers.Admin;
87

98
[Route(Constants.FullRouteName_Api_Admin_RefreshCache)]
109
[ApiController]
1110
public class RefreshCacheController : ControllerBase
1211
{
13-
internal static (bool EndpointEnabled, string AccessToken) Meta = (false, "");
14-
1512
private static readonly Dictionary<ReleaseChannel, DateTimeOffset> LastRefreshes =
1613
new()
1714
{
@@ -27,23 +24,22 @@ public class RefreshCacheController : ControllerBase
2724
[SuppressMessage("ReSharper.DPA", "DPA0011: High execution time of MVC action")]
2825
public async Task<ActionResult> Action([FromQuery] string rc)
2926
{
30-
if (!Meta.EndpointEnabled)
27+
if (!AdminEndpointMetadata.Enabled)
3128
return Problem("This instance of Ryubing UpdateServer is not configured to support this endpoint.",
3229
statusCode: 418);
3330

3431
if (!rc.TryParseAsReleaseChannel(out var releaseChannel))
35-
return Problem("Unknown rc", statusCode: 404);
32+
return Problem(
33+
$"Unknown release channel '{rc}'; valid are '{Constants.StableRoute}' and '{Constants.CanaryRoute}'", statusCode: 404);
3634

37-
if (!Meta.AccessToken.Equals(HttpContext.Request.Headers.Authorization.ToString(), StringComparison.Ordinal))
35+
if (!AdminEndpointMetadata.AccessToken.EqualsIgnoreCase(HttpContext.Request.Headers.Authorization))
3836
return Unauthorized();
3937

4038
var minutesSinceLastRefresh = (DateTimeOffset.Now - LastRefreshes[releaseChannel]).TotalMinutes;
4139
if (minutesSinceLastRefresh <= 1)
4240
return Problem("Try again later.", statusCode: 429);
4341

44-
var vcache = HttpContext.RequestServices.GetCacheFor(releaseChannel);
45-
46-
await vcache.RefreshAsync();
42+
await HttpContext.RequestServices.GetCacheFor(releaseChannel).RefreshAsync();
4743

4844
LastRefreshes[releaseChannel] = DateTimeOffset.Now;
4945

src/Server/Controllers/Api/v1/VersionController.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ public async Task<ActionResult<VersionResponse>> GetLatestCanary(
4848
[FromQuery] string? arch = null
4949
)
5050
{
51-
var latest = await vcache.GetReleaseAsync(c => c.Latest);
52-
53-
if (latest is null)
51+
if (await vcache.GetReleaseAsync(c => c.Latest) is not { } latest)
5452
return NotFound();
5553

5654
if (!os.TryParseAsSupportedPlatform(out var supportedPlatform))

src/Server/Controllers/DownloadController.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ public async Task<ActionResult> DownloadLatestStable(
5858
[FromKeyedServices("stableCache")] VersionCache vcache
5959
)
6060
{
61-
var latest = await vcache.GetReleaseAsync(c => c.Latest);
62-
63-
if (latest is null)
61+
if (await vcache.GetReleaseAsync(c => c.Latest) is not {} latest)
6462
return NotFound();
6563

6664
var uaString = HttpContext.Request.Headers.UserAgent.ToString();
@@ -84,9 +82,7 @@ public async Task<ActionResult> DownloadLatestCanary(
8482
[FromKeyedServices("canaryCache")] VersionCache vcache
8583
)
8684
{
87-
var latest = await vcache.GetReleaseAsync(c => c.Latest);
88-
89-
if (latest is null)
85+
if (await vcache.GetReleaseAsync(c => c.Latest) is not {} latest)
9086
return NotFound();
9187

9288
var uaString = HttpContext.Request.Headers.UserAgent.ToString();

src/Server/Controllers/LatestController.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ public async Task<ActionResult<VersionResponse>> GetLatestCustom(
3838
$"Unknown release channel '{rc}'; valid are '{Constants.StableRoute}' and '{Constants.CanaryRoute}'");
3939

4040
var vcache = HttpContext.RequestServices.GetCacheFor(releaseChannel);
41-
42-
var latest = await vcache.GetReleaseAsync(c => c.Latest);
43-
44-
if (latest is null)
41+
42+
if (await vcache.GetReleaseAsync(c => c.Latest) is not { } latest)
4543
return NotFound();
4644

4745
return Ok(new VersionResponse
@@ -59,11 +57,10 @@ public async Task<ActionResult<VersionResponse>> GetLatestCustom(
5957
public async Task<ActionResult> RedirectLatestStable(
6058
[FromKeyedServices("stableCache")] VersionCache vcache)
6159
{
62-
var latest = await vcache.GetReleaseAsync(c => c.Latest);
60+
if (await vcache.GetReleaseAsync(c => c.Latest) is { } latest)
61+
return Redirect(latest.ReleaseUrl);
6362

64-
return latest is not null
65-
? Redirect(latest.ReleaseUrl)
66-
: NotFound();
63+
return NotFound();
6764
}
6865

6966
[HttpGet(Constants.CanaryRoute)]
@@ -72,10 +69,9 @@ public async Task<ActionResult> RedirectLatestStable(
7269
public async Task<ActionResult> RedirectLatestCanary(
7370
[FromKeyedServices("canaryCache")] VersionCache vcache)
7471
{
75-
var latest = await vcache.GetReleaseAsync(c => c.Latest);
72+
if (await vcache.GetReleaseAsync(c => c.Latest) is { } latest)
73+
return Redirect(latest.ReleaseUrl);
7674

77-
return latest is not null
78-
? Redirect(latest.ReleaseUrl)
79-
: NotFound();
75+
return NotFound();
8076
}
8177
}

src/Server/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
#pragma warning restore CA2254
115115

116116
var adminSec = app.Configuration.GetSection("Admin");
117-
if (adminSec.Exists() && adminSec.GetValue<string>("AccessToken") is { } accessToken and not "")
118-
RefreshCacheController.Meta = (true, accessToken);
117+
if (adminSec.Exists())
118+
AdminEndpointMetadata.Set(adminSec.GetValue<string>("AccessToken"));
119119

120120
app.Run();

src/Server/Services/GitLab/VersionCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ public void Init(ProjectId projectId) => Executor.ExecuteBackgroundAsync(async (
6969

7070
if (_refreshTimer == null)
7171
{
72-
string howToRefresh = RefreshCacheController.Meta.EndpointEnabled
72+
string howToRefresh = AdminEndpointMetadata.Enabled
7373
? $"using the {Constants.FullRouteName_Api_Admin_RefreshCache} endpoint or restarting the server."
7474
: "restarting the server. There is an admin-only endpoint available that has not been configured. Set an admin access token in appsettings.json to enable the endpoint.";
7575

76-
_logger.LogInformation("Periodic version cache refreshing is disabled for {project}. It can only be refreshed by {means}", _cachedProject!.Value.Name, howToRefresh);
76+
_logger.LogInformation("Periodic version cache refreshing is disabled for {project}. It can be refreshed by {means}", _cachedProject!.Value.Name, howToRefresh);
7777
return;
7878
}
7979

0 commit comments

Comments
 (0)