-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
85 lines (78 loc) · 3.01 KB
/
Program.cs
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
using System.Reflection;
using CurseForge.APIClient;
using GraphQL.AspNet.Configuration;
using Microsoft.EntityFrameworkCore;
using Modrinth;
using ModStats.API.Database;
using ModStats.API.Services;
using ModStats.API.Services.Curseforge;
using ModStats.API.Services.Modrinth;
using ModStats.API.Services.Mojang;
using ModStats.API.Util.Auth;
using ModStats.API.Util.Config;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddLogging();
builder.Services.AddHttpClient();
builder.Services.AddGraphQL(options =>
{
options.ExecutionOptions.MaxQueryDepth = 20;
});
builder.Services.AddAuthorization();
builder.Services.AddSingleton<ApiKeyAuthorizationFilter>();
builder.Services.AddSingleton<IApiKeyValidator, ApiKeyValidator>();
builder.Services.AddDbContext<AppDbContext>(options =>
{
options.UseLazyLoadingProxies();
var connString = builder.Configuration.GetConnectionString("Postgres") ?? throw new Exception("No connection string found!");
if (builder.Environment.IsDevelopment() && !connString.Contains("Include Error Detail"))
{
if (!connString.EndsWith(';'))
{
connString += ';';
}
connString += "Include Error Detail=true;";
}
var contextBuilder = options.UseNpgsql(connString);
if (builder.Environment.IsDevelopment())
{
contextBuilder.EnableSensitiveDataLogging();
}
});
builder.Services.AddTransient<ApiClient>(_ =>
{
var config = builder.Configuration.GetSection(CurseForgeConfig.SectionName).Get<CurseForgeConfig>()!;
return new ApiClient(config.ApiKey, config.PartnerId, config.ContactEmail);
});
builder.Services.AddTransient<ModrinthClient>(_ =>
{
var config = builder.Configuration.GetSection(ModrinthConfig.SectionName).Get<ModrinthConfig>()!;
var assemblyInfo = Assembly.GetEntryAssembly()!.GetName();
var userAgentString = $"{assemblyInfo.Name}/{assemblyInfo.Version} ({config.ContactEmail})";
return new ModrinthClient(new ModrinthClientConfig
{
ModrinthToken = config.ApiKey,
UserAgent = userAgentString,
});
});
builder.Services.AddScoped<IMcVersionService, McVersionService>();
builder.Services.AddScoped<ICurseforgeUpdateService, CurseforgeUpdateService>();
builder.Services.AddScoped<IModrinthUpdateService, ModrinthUpdateService>();
builder.Services.AddTransient<IDataInitService, DataInitService>();
builder.Services.AddHostedService<FetchMcMetaService>();
builder.Services.AddHostedService<CurseforgeBackgroundUpdateService>();
builder.Services.AddHostedService<ModrinthBackgroundUpdateService>();
var app = builder.Build();
app.UseAuthorization();
app.UseGraphQL();
Init(app).Wait();
app.Run();
return;
async Task Init(IHost host)
{
using var scope = host.Services.CreateScope();
// run pending database migrations
await scope.ServiceProvider.GetRequiredService<AppDbContext>().Database.MigrateAsync();
// fetch version manifest and update database
await scope.ServiceProvider.GetRequiredService<IDataInitService>().InitData();
}