|
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; |
4 | 3 | using Ryujinx.Systems.Update.Server.Services.GitLab; |
5 | 4 |
|
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); |
49 | 6 |
|
50 | 7 | var builder = WebApplication.CreateBuilder(args); |
51 | 8 |
|
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)); |
54 | 11 |
|
55 | | -if (useHttpLogging) |
| 12 | +if (CommandLineState.UseHttpLogging) |
56 | 13 | builder.Services.AddHttpLogging(); |
57 | 14 |
|
58 | 15 | builder.Services.AddSingleton<GitLabService>(); |
59 | 16 | builder.Services.AddKeyedSingleton<VersionCache>("stableCache"); |
60 | 17 | builder.Services.AddKeyedSingleton<VersionCache>("canaryCache"); |
61 | 18 |
|
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); |
78 | 20 |
|
79 | 21 | builder.Services.AddControllers(); |
80 | 22 |
|
81 | 23 | var app = builder.Build(); |
82 | 24 |
|
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); |
102 | 26 |
|
103 | | -if (canarySource != null) |
104 | | - app.Services.GetRequiredKeyedService<VersionCache>("canaryCache").Init(new ProjectId(canarySource)); |
| 27 | +VersionCache.InitializeVersionCaches(app); |
105 | 28 |
|
106 | 29 | app.MapControllers(); |
107 | 30 |
|
108 | | -if (useHttpLogging) |
| 31 | +if (CommandLineState.UseHttpLogging) |
109 | 32 | app.UseHttpLogging(); |
110 | 33 |
|
111 | 34 | TaskScheduler.UnobservedTaskException += (sender, eventArgs) => |
|
0 commit comments