Files
Aaru.Server/Aaru.Server/Program.cs

185 lines
9.0 KiB
C#
Raw Normal View History

2025-06-21 11:52:29 +01:00
#pragma warning disable VSTHRD200
2024-05-05 02:20:43 +01:00
using System.Diagnostics;
using Aaru.CommonTypes.Interop;
2025-04-27 12:48:13 +01:00
using Aaru.Server.Components;
using Aaru.Server.Components.Account;
using Aaru.Server.Services;
2024-05-03 03:24:40 +01:00
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Serilog;
2025-06-21 11:52:29 +01:00
using Serilog.Events;
2024-05-04 02:27:56 +01:00
using DbContext = Aaru.Server.Database.DbContext;
2024-05-05 02:20:43 +01:00
using Version = System.Version;
Console.Clear();
2025-05-10 12:41:44 +01:00
// ReSharper disable once StringLiteralTypo
Console.Write("""
 . ,,
 ;,. '0d.
 oc oWd 
 ;X. 'WN' 
 oMo cMM: 
 ;MM. .MMM; 
 NMM WMMW 
 'MMM MMMM; 
 ,MMM: dMMMM: 
 .MMMW. :MMMMM. 
 XMMMW: .:xKNMMMMMMN0d, lMMMMMd 
 :MMMMMK; cWMNkl:;;;:lxKMXc .0MMMMMO
 ..KMMMMMMNo,. ,OMMMMMMW:,.  Aaru Website
 .;d0NMMMMMMMMMMMMMMW0d:' .;lOWMMMMMMMMMMMMMXkl.  Version {0}-{1}
 :KMMMMMMMMMMMMMMMMMMMMMMMMc WMMMMMMMMMMMMMMMMMMMMMMWk'
 ;NMMMMWX0kkkkO0XMMMMMMMMMMM0' dNMMMMMMMMMMW0xl:;,;:oOWMMX;  Running under {2}, {3}-bit in {4}-bit mode.
 xMMWk:. .c0MMMMMW' OMMMMMM0c'.. .oNMO  Using {5} version {6}
 OMNc .MNc oWMMk 'WMMNl. .MMK ;KX.
 xMO WMN ; ., , ': ,MMx lK
 ,Md cMMl .XMMMWWMMMO XMW. :
 Ok xMMl XMMMMMMMMc 0MW,
 0 oMM0' lMMMMMMMM. :NMN'
 . .0MMKl ;MMMMMMMM oNMWd
 .dNW cMMMMMMMM, XKl
 0MMMMMMMMK
 ;MMMMMMMMMMO  Proudly presented to you by:
 'WMMMMKxMMMMM0  Natalia Portillo
 oMMMMNc :WMMMMN:
 .dWMMM0; dWMMMMXl.  Thanks to all contributors, collaborators, translators, donators and friends.
 .......,cd0WMMNk: c0MMMMMWKkolc:clodc'
 .';loddol:'. ':oxkkOkkxoc,.

""",
2024-05-05 02:20:43 +01:00
Aaru.CommonTypes.Interop.Version.GetVersion(),
#if DEBUG
"DEBUG",
#else
"RELEASE",
#endif
DetectOS.GetPlatformName(DetectOS.GetRealPlatformID()),
Environment.Is64BitOperatingSystem ? 64 : 32,
Environment.Is64BitProcess ? 64 : 32,
DetectOS.IsMono ? "Mono" : ".NET Core",
DetectOS.IsMono
? Aaru.CommonTypes.Interop.Version.GetMonoVersion()
: Aaru.CommonTypes.Interop.Version.GetNetCoreVersion());
2025-05-10 12:41:44 +01:00
Console.WriteLine("\e[31;1mBuilding web application...\e[0m");
2024-05-02 07:43:47 +01:00
2024-05-03 03:24:40 +01:00
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
2024-05-02 07:43:47 +01:00
#if DEBUG
builder.Logging.AddSerilog(new LoggerConfiguration().WriteTo.Console().CreateLogger());
#endif
2025-06-21 11:52:29 +01:00
builder.Logging.AddSerilog(new LoggerConfiguration().WriteTo
.LocalSyslog("aaru-server",
restrictedToMinimumLevel: LogEventLevel.Information)
.CreateLogger());
2024-05-02 07:43:47 +01:00
// Add services to the container.
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<IdentityUserAccessor>();
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();
2025-05-10 13:38:59 +01:00
builder.Services.AddAuthentication(static options =>
2024-05-02 07:43:47 +01:00
{
options.DefaultScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
})
.AddIdentityCookies();
2024-05-03 03:24:40 +01:00
string connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ??
throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
2024-05-02 07:43:47 +01:00
2024-05-04 02:27:56 +01:00
builder.Services.AddDbContextFactory<DbContext>(options => options
.UseMySql(connectionString,
new MariaDbServerVersion(new Version(10, 4, 0)))
.UseLazyLoadingProxies());
2024-05-02 07:43:47 +01:00
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
2024-05-05 02:20:43 +01:00
builder.Services.AddIdentityCore<IdentityUser>(static options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<DbContext>()
2024-05-02 07:43:47 +01:00
.AddSignInManager()
.AddDefaultTokenProviders();
builder.Services.AddSingleton<IEmailSender<IdentityUser>, IdentityNoOpEmailSender>();
2024-05-02 07:43:47 +01:00
builder.Services.AddBlazorBootstrap();
2024-05-11 01:32:52 +01:00
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddHostedService<UpdateTask>();
2024-05-03 03:24:40 +01:00
WebApplication app = builder.Build();
2024-05-02 07:43:47 +01:00
// Configure the HTTP request pipeline.
if(app.Environment.IsDevelopment())
app.UseMigrationsEndPoint();
else
{
2024-05-03 03:24:40 +01:00
app.UseExceptionHandler("/Error", true);
2024-05-02 07:43:47 +01:00
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
2024-05-11 01:32:52 +01:00
app.UseAuthorization();
2025-04-27 11:28:51 +01:00
app.MapStaticAssets();
2024-05-02 07:43:47 +01:00
app.UseAntiforgery();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
// Add additional endpoints required by the Identity /Account Razor components.
app.MapAdditionalIdentityEndpoints();
2024-05-11 01:32:52 +01:00
app.MapControllers();
2024-05-02 07:43:47 +01:00
2024-05-05 02:21:04 +01:00
using(IServiceScope scope = app.Services.CreateScope())
{
IServiceProvider services = scope.ServiceProvider;
try
{
Stopwatch stopwatch = new();
stopwatch.Start();
2025-05-10 12:41:44 +01:00
Console.WriteLine("\e[31;1mUpdating database with Entity Framework...\e[0m");
2024-05-05 02:21:04 +01:00
DbContext context = services.GetRequiredService<DbContext>();
await context.Database.MigrateAsync();
stopwatch.Stop();
2025-05-10 12:41:44 +01:00
Console.WriteLine("\e[31;1mTook \e[32;1m{0} seconds\e[31;1m...\e[0m", stopwatch.Elapsed.TotalSeconds);
2024-05-05 02:25:34 +01:00
stopwatch.Restart();
2025-05-10 12:41:44 +01:00
Console.WriteLine("\e[31;1mSeeding Identity...\e[0m");
2024-05-05 02:25:34 +01:00
await Seeder.SeedAsync(context, services);
2025-04-27 13:13:56 +01:00
await context.Database.MigrateAsync();
2024-05-05 02:25:34 +01:00
stopwatch.Stop();
2025-05-10 12:41:44 +01:00
Console.WriteLine("\e[31;1mTook \e[32;1m{0} seconds\e[31;1m...\e[0m", stopwatch.Elapsed.TotalSeconds);
2024-05-05 02:21:04 +01:00
}
catch(Exception ex)
{
2025-05-10 12:41:44 +01:00
Console.WriteLine("\e[31;1mCould not open database...\e[0m");
2024-05-05 02:21:04 +01:00
#if DEBUG
2025-05-10 13:38:59 +01:00
Console.WriteLine("\e[31;1mException: {0}\e[0m", ex);
2024-05-05 02:21:04 +01:00
#endif
return;
}
}
2025-05-10 12:41:44 +01:00
Console.WriteLine("\e[31;1mStarting web server...\e[0m");
2024-05-05 02:21:04 +01:00
2025-06-21 11:52:29 +01:00
await app.RunAsync();
#pragma warning restore VSTHRD200