using EgwCoreLib.Lux.Data.Services; using Lux.UI.Client.Pages; using Lux.UI.Components; using Lux.UI.Components.Account; using Lux.UI.Data; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Localization; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.FileProviders; using NLog; using NLog.Web; using StackExchange.Redis; using System.Globalization; var builder = WebApplication.CreateBuilder(args); // recupero env corrente var env = builder.Environment; var logger = LogManager.Setup() .LoadConfigurationFromAppSettings() .GetCurrentClassLogger(); ConfigurationManager configuration = builder.Configuration; logger.Info("Program.cs: startup"); logger.Info($"Current ASPNETCORE_ENVIRONMENT: {env.EnvironmentName}"); // Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents() .AddInteractiveWebAssemblyComponents(); builder.Services.AddCascadingAuthenticationState(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddAuthentication(options => { options.DefaultScheme = IdentityConstants.ApplicationScheme; options.DefaultSignInScheme = IdentityConstants.ExternalScheme; }) .AddIdentityCookies(); var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); builder.Services.AddDbContext(options => options.UseSqlServer(connectionString)); builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddIdentityCore(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores() .AddSignInManager() .AddDefaultTokenProviders(); builder.Services.AddSingleton, IdentityNoOpEmailSender>(); // costruzione connectionMultiplexer redis... string connStr = configuration.GetConnectionString("Redis") ?? "localhost"; ConnectionMultiplexer redisConn = ConnectionMultiplexer.Connect(connStr); // registro connMultiplexer REDIS builder.Services.AddSingleton(redisConn); // registro wrapper servizi REDIS builder.Services.AddSingleton(); builder.Services.AddSingleton(); // Aggiunta servizi specifici builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); // init servizio gestone ReqIndex string cleanupDayTTL = configuration.GetValue("ServerConf:CleanupDayTTL") ?? "360"; string rBaseKey = configuration.GetValue("ServerConf:RedisBaseKey") ?? "Lux"; int dayTTL = 360; int archTTL = 2; int.TryParse(cleanupDayTTL, out dayTTL); builder.Services.AddSingleton(new CalcRuidService( configuration, redisConn, redisBaseKey: rBaseKey, retention: TimeSpan.FromDays(dayTTL), archivePeriod: TimeSpan.FromDays(archTTL) )); // lo gestisco solo via API... #if false builder.Services.AddHostedService(); #endif var app = builder.Build(); // aggiunt base URL x routing corretto string baseUrl = configuration.GetValue("ServerConf:BaseUrl") ?? ""; app.UsePathBase(baseUrl); logger.Info($"BaseUrl: {baseUrl}"); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseWebAssemblyDebugging(); app.UseMigrationsEndPoint(); } else { app.UseExceptionHandler("/Error", createScopeForErrors: 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(); } // cultura IT... var supportedCultures = new[]{ new CultureInfo("it-IT") }; app.UseRequestLocalization(new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("it-IT"), SupportedCultures = supportedCultures, FallBackToParentCultures = false }); CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("it-IT"); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseAntiforgery(); // gestione fileshare x uploads: verifico da conf se sia linux o windows x file da accedere... if (configuration["ServerConf:HostOs"] == "Win") { app.UseFileServer(new FileServerOptions { FileProvider = new PhysicalFileProvider(@"\\stor01\TEAM DRIVES\40_FileUpload\LuxUploads"), RequestPath = new PathString("/unsafe_uploads"), EnableDirectoryBrowsing = true //EnableDirectoryBrowsing = false }); } else { app.UseFileServer(new FileServerOptions { FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "unsafe_uploads")), RequestPath = new PathString("/unsafe_uploads"), EnableDirectoryBrowsing = true //EnableDirectoryBrowsing = false }); } app.MapRazorComponents() .AddInteractiveServerRenderMode() .AddInteractiveWebAssemblyRenderMode() .AddAdditionalAssemblies(typeof(Lux.UI.Client._Imports).Assembly); // Add additional endpoints required by the Identity /Account Razor components. app.MapAdditionalIdentityEndpoints(); app.Run();