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

185 lines
9.0 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma warning disable VSTHRD200
using System.Diagnostics;
using Aaru.CommonTypes.Interop;
using Aaru.Server.Components;
using Aaru.Server.Components.Account;
using Aaru.Server.Services;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Serilog;
using Serilog.Events;
using DbContext = Aaru.Server.Database.DbContext;
using Version = System.Version;
Console.Clear();
// 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,.

""",
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());
Console.WriteLine("\e[31;1mBuilding web application...\e[0m");
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
#if DEBUG
builder.Logging.AddSerilog(new LoggerConfiguration().WriteTo.Console().CreateLogger());
#endif
builder.Logging.AddSerilog(new LoggerConfiguration().WriteTo
.LocalSyslog("aaru-server",
restrictedToMinimumLevel: LogEventLevel.Information)
.CreateLogger());
// 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>();
builder.Services.AddAuthentication(static options =>
{
options.DefaultScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
})
.AddIdentityCookies();
string connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ??
throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContextFactory<DbContext>(options => options
.UseMySql(connectionString,
new MariaDbServerVersion(new Version(10, 4, 0)))
.UseLazyLoadingProxies());
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddIdentityCore<IdentityUser>(static options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<DbContext>()
.AddSignInManager()
.AddDefaultTokenProviders();
builder.Services.AddSingleton<IEmailSender<IdentityUser>, IdentityNoOpEmailSender>();
builder.Services.AddBlazorBootstrap();
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddHostedService<UpdateTask>();
WebApplication app = builder.Build();
// Configure the HTTP request pipeline.
if(app.Environment.IsDevelopment())
app.UseMigrationsEndPoint();
else
{
app.UseExceptionHandler("/Error", true);
// 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();
app.UseAuthorization();
app.MapStaticAssets();
app.UseAntiforgery();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
// Add additional endpoints required by the Identity /Account Razor components.
app.MapAdditionalIdentityEndpoints();
app.MapControllers();
using(IServiceScope scope = app.Services.CreateScope())
{
IServiceProvider services = scope.ServiceProvider;
try
{
Stopwatch stopwatch = new();
stopwatch.Start();
Console.WriteLine("\e[31;1mUpdating database with Entity Framework...\e[0m");
DbContext context = services.GetRequiredService<DbContext>();
await context.Database.MigrateAsync();
stopwatch.Stop();
Console.WriteLine("\e[31;1mTook \e[32;1m{0} seconds\e[31;1m...\e[0m", stopwatch.Elapsed.TotalSeconds);
stopwatch.Restart();
Console.WriteLine("\e[31;1mSeeding Identity...\e[0m");
await Seeder.SeedAsync(context, services);
await context.Database.MigrateAsync();
stopwatch.Stop();
Console.WriteLine("\e[31;1mTook \e[32;1m{0} seconds\e[31;1m...\e[0m", stopwatch.Elapsed.TotalSeconds);
}
catch(Exception ex)
{
Console.WriteLine("\e[31;1mCould not open database...\e[0m");
#if DEBUG
Console.WriteLine("\e[31;1mException: {0}\e[0m", ex);
#endif
return;
}
}
Console.WriteLine("\e[31;1mStarting web server...\e[0m");
await app.RunAsync();
#pragma warning restore VSTHRD200