This repository was archived by the owner on Feb 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.cs
More file actions
355 lines (299 loc) · 10.4 KB
/
App.cs
File metadata and controls
355 lines (299 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using Discord;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using spapp_backend.Core;
using spapp_backend.Core.Models;
using spapp_backend.Db;
using System;
using System.Reflection;
using System.Text.Json;
using System.Text;
using Microsoft.EntityFrameworkCore;
using spapp_backend.Core.Enums;
using spapp_backend.Core.Controllers;
using spapp_backend.Utils;
using System.Globalization;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.Extensions.FileProviders;
using spapp_backend.Modules.Crowdfunding.Controllers;
using spapp_backend.Modules.Admin.Controllers;
using System.Net;
namespace spapp_backend
{
public class App
{
#pragma warning disable CS8618
public static WebApplication WebServer { get; private set; }
#pragma warning restore CS8618
public static Dictionary<string, IWebController> Modules { get; private set; } = new();
public static Logger Logger { get; private set; } = new();
public static PreviewGen PreviewGen { get; private set; } = new();
private static readonly Timer TimingTasksTimer = new(RunTimingTasks, null, Timeout.Infinite, Timeout.Infinite);
public static IConfiguration GetConfig(string? section = null)
{
if (section != null)
{
return WebServer.Configuration.GetSection(section);
}
else
{
return WebServer.Configuration;
}
}
public static T GetModule<T>(string key)
{
return (T)Modules[key];
}
public static string GetStaticPath()
{
return Path.Combine(
WebServer.Environment.ContentRootPath,
WebServer.Configuration["Settings:StaticDir"] ?? "Data/Static"
);
}
public static void Init(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
{
Logger.WriteExceptionLog((Exception)e.ExceptionObject);
};
var builder = WebApplication.CreateBuilder(args);
#if DEBUG
IdentityModelEventSource.ShowPII = true;
#else
IdentityModelEventSource.ShowPII = false;
#endif
var resetJwtKey = false;
using (var db = new SQLiteDbContext())
{
if (!File.Exists("Data/Main.db"))
{
resetJwtKey = true;
db.Database.Migrate();
}
db.Database.ExecuteSqlRaw("PRAGMA foreign_keys = ON;");
}
if (!Directory.Exists("Data/Logs"))
{
Directory.CreateDirectory("Data/Logs");
}
// Add OpenAPI
builder.Services.AddSwaggerGen(o =>
{
// using System.Reflection;
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
o.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
o.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Name = "Authorization",
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "JSON Web Token based security",
});
o.AddSecurityRequirement(
new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
}
);
o.TagActionsBy(api =>
{
var str = api.RelativePath?.Split("/")[0] ?? "unknown";
return new[] { char.ToUpper(str[0]) + str[1..] };
});
o.DocInclusionPredicate((name, api) => true);
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(o =>
{
o.SerializerOptions.PropertyNameCaseInsensitive = false;
o.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
o.SerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
});
// Add db context
builder.Services.AddDbContext<SQLiteDbContext>();
// Add auth
builder.Services.AddIdentity<User, IdentityRole<uint>>(o =>
{
o.Password.RequireDigit = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonAlphanumeric = false;
o.SignIn.RequireConfirmedEmail = false;
o.SignIn.RequireConfirmedPhoneNumber = false;
})
.AddRoleManager<RoleManager<IdentityRole<uint>>>()
.AddEntityFrameworkStores<SQLiteDbContext>();
// Add endpoints
var profileModule = new ProfileController();
if (resetJwtKey)
{
profileModule.ResetJWTSecret();
}
Modules.Add("base", new BaseController());
Modules.Add("file", new FileController());
Modules.Add("profile", profileModule);
Modules.Add("payment", new PaymentController());
Modules.Add("crowdfund", new CrowdfundController());
Modules.Add("mainAdmin", new MainAdminController());
builder.Services
.AddAuthorization()
.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Settings:AppUrl"],
ValidAudience = builder.Configuration["Settings:AppUrl"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(profileModule.JwtSecret ?? ""))
};
o.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
var path = context.HttpContext.Request.Path;
if (!string.IsNullOrEmpty(accessToken) &&
(path.StartsWithSegments("/signalr")))
{
// Read the token out of the query string
context.Token = accessToken;
}
return Task.CompletedTask;
}
};
});
// Create default roles
var serviceProvider = builder.Services.BuildServiceProvider();
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole<uint>>>();
foreach (var role in (int[])Enum.GetValues(typeof(Role)))
{
bool roleExists = roleManager.RoleExistsAsync(role.ToString()).Result;
if (!roleExists)
{
roleManager.CreateAsync(new IdentityRole<uint> { Name = role.ToString() });
}
}
// Add services to the container
//builder.Services.AddSignalR();
var spApi = new SPApi();
spApi.SetCreds(
MCServer.SP,
builder.Configuration.GetSection("ThirdParty")["SPId"] ?? "",
builder.Configuration.GetSection("ThirdParty")["SPToken"] ?? ""
);
spApi.SetCreds(
MCServer.SPM,
builder.Configuration.GetSection("ThirdParty")["SPMId"] ?? "",
builder.Configuration.GetSection("ThirdParty")["SPMToken"] ?? ""
);
builder.Services.AddSingleton(spApi);
builder.Services.AddSingleton(PreviewGen);
builder.Services.AddSingleton(new MojangApi());
builder.Services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", b =>
{
b.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.WithOrigins(builder.Configuration.GetSection("Settings")["FrontendUrl"] ?? "http://localhost");
});
});
WebServer = builder.Build();
if (!Directory.Exists(GetStaticPath()))
{
Directory.CreateDirectory(GetStaticPath());
Directory.CreateDirectory(Path.Combine(GetStaticPath(), "uploads"));
}
}
public static void Run()
{
var cultureInfo = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
Logger.LogToConsole("Starting...", "App");
ConfigureWebServer();
//Signal = WebServer.Services.GetRequiredService<IHubContext<MainHub>>();
//RunTimingTasks(null);
PreviewGen.Init().Wait();
TimingTasksTimer.Change(0, 60 * 60 * 1000);
WebServer.Run();
}
private static void RunTimingTasks(object? state)
{
var db = new SQLiteDbContext();
foreach (var module in Modules)
{
module.Value.RunTimingTask(db).Wait();
}
}
private static void ConfigureWebServer()
{
if (!Directory.Exists(WebServer.Configuration["Settings:StaticDir"]))
{
Directory.CreateDirectory(WebServer.Configuration["Settings:StaticDir"] ?? "Data/Static");
}
WebServer.UseDeveloperExceptionPage();
WebServer.UseCors("CorsPolicy");
WebServer.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
GetStaticPath()
),
RequestPath = "/static"
});
// Add endpoints
foreach (var module in Modules)
{
module.Value.Inject(WebServer);
}
//WebServer.MapHub<MainHub>("/signalr");
WebServer.UseAuthentication();
WebServer.UseAuthorization();
if (bool.Parse(WebServer.Configuration.GetSection("Settings")["EnableSwagger"] ?? "false"))
{
WebServer.UseSwagger();
WebServer.UseSwaggerUI();
}
WebServer.UseExceptionHandler(e =>
{
e.Run(async ec =>
{
var exception =
ec.Features.Get<IExceptionHandlerPathFeature>();
if (exception != null)
{
Logger.WriteExceptionLog(exception.Error, "webserver.txt");
}
await ec.Response.WriteAsJsonAsync(new ErrorResult(ResponseError.Unknown, HttpStatusCode.InternalServerError, exception?.Error.ToString()));
});
});
}
}
}