Skip to content

Commit 1c8a8e6

Browse files
committed
Cleanup Program.cs
1 parent af8a341 commit 1c8a8e6

7 files changed

Lines changed: 116 additions & 88 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace Ryujinx.Systems.Update.Server.Helpers;
2+
3+
public static class CommandLineState
4+
{
5+
#if DEBUG
6+
public static bool UseHttpLogging { get; private set; } = true;
7+
public static bool UseSwagger { get; private set; } = true;
8+
#else
9+
public static bool UseHttpLogging { get; private set; } = false;
10+
public static bool UseSwagger { get; private set; } = false;
11+
#endif
12+
public static int? ListenPort { get; private set; }
13+
14+
public static void Init(string[] args)
15+
{
16+
foreach (var (index, arg) in args.Index())
17+
{
18+
switch (arg.ToLower())
19+
{
20+
case "--port":
21+
case "-p":
22+
{
23+
if (index + 1 >= args.Length)
24+
throw new Exception("port argument expects a value");
25+
26+
if (!args[index + 1].TryParse<int>(out var port))
27+
throw new Exception("port argument must be an integer");
28+
29+
ListenPort = port;
30+
31+
break;
32+
}
33+
case "--http-logging":
34+
case "-l":
35+
{
36+
UseHttpLogging = true;
37+
break;
38+
}
39+
case "--enable-swagger":
40+
case "-s":
41+
{
42+
UseSwagger = true;
43+
break;
44+
}
45+
}
46+
}
47+
}
48+
}
File renamed without changes.

src/Server/Extensions/ServiceProviderExtensions.cs renamed to src/Server/Helpers/Extensions/ServiceProviderExtensions.cs

File renamed without changes.

src/Server/Controllers/Api/v1/Admin/AdminEndpointMetadata.cs renamed to src/Server/Initialization/AdminEndpointMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Gommon;
22

3-
namespace Ryujinx.Systems.Update.Server.Controllers.Admin;
3+
namespace Ryujinx.Systems.Update.Server;
44

55
internal static class AdminEndpointMetadata
66
{
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using NSwag;
2+
using Ryujinx.Systems.Update.Common;
3+
using Ryujinx.Systems.Update.Server.Helpers;
4+
5+
namespace Ryujinx.Systems.Update.Server;
6+
7+
public static class Swagger
8+
{
9+
public static void TrySetup(WebApplicationBuilder builder)
10+
{
11+
if (CommandLineState.UseSwagger)
12+
{
13+
builder.Services.AddOpenApi(Constants.CurrentApiVersion);
14+
builder.Services.AddOpenApiDocument(opt =>
15+
{
16+
opt.PostProcess = doc =>
17+
{
18+
doc.Info = new OpenApiInfo
19+
{
20+
Version = Constants.CurrentApiVersion,
21+
Title = "Ryujinx Updates",
22+
Description = "REST API for Ryubing updates powered by ASP.NET Core."
23+
};
24+
};
25+
});
26+
}
27+
}
28+
29+
public static void TryMapUi(WebApplication app)
30+
{
31+
if (CommandLineState.UseSwagger)
32+
{
33+
app.MapOpenApi();
34+
app.UseSwaggerUi(opt =>
35+
{
36+
opt.DocumentPath = $"/openapi/{Constants.CurrentApiVersion}.json";
37+
});
38+
}
39+
}
40+
}

src/Server/Program.cs

Lines changed: 10 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,34 @@
1-
using NGitLab.Models;
2-
using NSwag;
3-
using Ryujinx.Systems.Update.Server.Controllers.Admin;
1+
using Ryujinx.Systems.Update.Server;
2+
using Ryujinx.Systems.Update.Server.Helpers;
43
using Ryujinx.Systems.Update.Server.Services.GitLab;
54

6-
const string apiVersion = "v1";
7-
8-
#if DEBUG
9-
var useHttpLogging = true;
10-
var useSwagger = true;
11-
#else
12-
var useHttpLogging = false;
13-
var useSwagger = false;
14-
#endif
15-
16-
int? listenToPort = null;
17-
18-
foreach (var (index, arg) in args.Index())
19-
{
20-
switch (arg.ToLower())
21-
{
22-
case "--port":
23-
case "-p":
24-
{
25-
if (index + 1 >= args.Length)
26-
throw new Exception("port argument expects a value");
27-
28-
if (!int.TryParse(args[index + 1], out var port))
29-
throw new Exception("port argument must be an integer");
30-
31-
listenToPort = port;
32-
33-
break;
34-
}
35-
case "--http-logging":
36-
case "-l":
37-
{
38-
useHttpLogging = true;
39-
break;
40-
}
41-
case "--enable-swagger":
42-
case "-s":
43-
{
44-
useSwagger = true;
45-
break;
46-
}
47-
}
48-
}
5+
CommandLineState.Init(args);
496

507
var builder = WebApplication.CreateBuilder(args);
518

52-
if (listenToPort != null)
53-
builder.WebHost.ConfigureKestrel(options => options.ListenLocalhost(listenToPort.Value));
9+
if (CommandLineState.ListenPort != null)
10+
builder.WebHost.ConfigureKestrel(options => options.ListenLocalhost(CommandLineState.ListenPort.Value));
5411

55-
if (useHttpLogging)
12+
if (CommandLineState.UseHttpLogging)
5613
builder.Services.AddHttpLogging();
5714

5815
builder.Services.AddSingleton<GitLabService>();
5916
builder.Services.AddKeyedSingleton<VersionCache>("stableCache");
6017
builder.Services.AddKeyedSingleton<VersionCache>("canaryCache");
6118

62-
if (useSwagger)
63-
{
64-
builder.Services.AddOpenApi(apiVersion);
65-
builder.Services.AddOpenApiDocument(opt =>
66-
{
67-
opt.PostProcess = doc =>
68-
{
69-
doc.Info = new OpenApiInfo
70-
{
71-
Version = apiVersion,
72-
Title = "Ryujinx Updates",
73-
Description = "REST API for Ryubing updates powered by ASP.NET Core."
74-
};
75-
};
76-
});
77-
}
19+
Swagger.TrySetup(builder);
7820

7921
builder.Services.AddControllers();
8022

8123
var app = builder.Build();
8224

83-
if (useSwagger)
84-
{
85-
app.MapOpenApi();
86-
app.UseSwaggerUi(opt =>
87-
{
88-
opt.DocumentPath = $"/openapi/{apiVersion}.json";
89-
});
90-
}
91-
92-
var versionCacheSection = app.Configuration.GetSection("GitLab").GetRequiredSection("VersionCacheSources");
93-
94-
var stableSource = versionCacheSection.GetValue<string>("Stable");
95-
96-
if (stableSource is null)
97-
throw new Exception("Cannot start the server without a GitLab repository in GitLab:VersionCacheSources:Stable");
98-
99-
app.Services.GetRequiredKeyedService<VersionCache>("stableCache").Init(new ProjectId(stableSource));
100-
101-
var canarySource = versionCacheSection.GetValue<string>("Canary");
25+
Swagger.TryMapUi(app);
10226

103-
if (canarySource != null)
104-
app.Services.GetRequiredKeyedService<VersionCache>("canaryCache").Init(new ProjectId(canarySource));
27+
VersionCache.InitializeVersionCaches(app);
10528

10629
app.MapControllers();
10730

108-
if (useHttpLogging)
31+
if (CommandLineState.UseHttpLogging)
10932
app.UseHttpLogging();
11033

11134
TaskScheduler.UnobservedTaskException += (sender, eventArgs) =>

src/Server/Services/GitLab/VersionCache.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,21 @@ public async Task RefreshAsync()
153153

154154
_semaphore.Release();
155155
}
156+
157+
public static void InitializeVersionCaches(WebApplication app)
158+
{
159+
var versionCacheSection = app.Configuration.GetSection("GitLab").GetRequiredSection("VersionCacheSources");
160+
161+
var stableSource = versionCacheSection.GetValue<string>("Stable");
162+
163+
if (stableSource is null)
164+
throw new Exception("Cannot start the server without a GitLab repository in GitLab:VersionCacheSources:Stable");
165+
166+
app.Services.GetRequiredKeyedService<VersionCache>("stableCache").Init(new ProjectId(stableSource));
167+
168+
var canarySource = versionCacheSection.GetValue<string>("Canary");
169+
170+
if (canarySource != null)
171+
app.Services.GetRequiredKeyedService<VersionCache>("canaryCache").Init(new ProjectId(canarySource));
172+
}
156173
}

0 commit comments

Comments
 (0)