205 lines
7.1 KiB
C#
205 lines
7.1 KiB
C#
using MagMan.UI.Data;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using MagMan.Data;
|
|
using Microsoft.AspNetCore.Localization;
|
|
using System.Globalization;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using StackExchange.Redis;
|
|
using MagMan.Data.Services;
|
|
using HealthChecks.UI.Client;
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
|
using Blazored.LocalStorage;
|
|
using Blazored.SessionStorage;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
using System.Configuration;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using MagMan.UI.Areas.Identity;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// gestione configurazione
|
|
var connectionString = builder.Configuration.GetConnectionString("UserIdentityDbContextConnection") ?? throw new InvalidOperationException("Connection string 'UserIdentityDbContextConnection' not found.");
|
|
|
|
// REDIS setup
|
|
var redisConnString = builder.Configuration.GetConnectionString("Redis");
|
|
string redisSrvAddr = redisConnString.Substring(0, redisConnString.IndexOf(":"));
|
|
string connStringRedis = redisConnString ?? "localhost:6379, DefaultDatabase=1, connectTimeout=5000, syncTimeout=5000, asyncTimeout=5000, abortConnect=false, ssl=false";
|
|
//string redisSrvAddr = connStringRedis.Substring(0, connStringRedis.IndexOf(":"));
|
|
// avvio oggetto shared x redis...
|
|
var redisMultiplexer = ConnectionMultiplexer.Connect(connStringRedis);
|
|
// Add services x accesso dati
|
|
builder.Services.AddSingleton<IConnectionMultiplexer>(redisMultiplexer);
|
|
|
|
// init info x DB
|
|
string dbServerAddr = builder.Configuration["DbConfig:Server"];
|
|
string nKey = builder.Configuration["DbConfig:nKey"];
|
|
string sKey = builder.Configuration["DbConfig:sKey"];
|
|
DbConfig.InitDb(dbServerAddr, nKey, sKey);
|
|
// inizializzo il DB e creo (se necessario) l'utente
|
|
DbConfig.CheckUser(nKey, sKey);
|
|
// verifico se serve applicazione migrazioni
|
|
DbConfig.ExecMigrationMain();
|
|
//DbConfig.ExecMigrationIdentity();
|
|
string connStringDB = DbConfig.CONNECTION_STRING;
|
|
|
|
// healthchecks
|
|
builder.Services.AddHealthChecks()
|
|
.AddMySql(connStringDB, "MySql instance")
|
|
.AddAsyncCheck($"DB PING ({dbServerAddr})", () => MagMan.UI.Health.Checks.PingCheck(dbServerAddr))
|
|
.AddAsyncCheck($"Redis PING ({redisSrvAddr})", () => MagMan.UI.Health.Checks.PingCheck(redisSrvAddr))
|
|
.AddProcessAllocatedMemoryHealthCheck(512, "Max Process memory (<512MB)", failureStatus: HealthStatus.Degraded) // 512 MB max allocated memory
|
|
.AddRedis(builder.Configuration.GetConnectionString("Redis"), "Redis", failureStatus: HealthStatus.Degraded)
|
|
.AddAsyncCheck($"MySql Root User", () => MagMan.UI.Health.Checks.DbUserRoot("MySql"))
|
|
#if false
|
|
.AddAsyncCheck($"MySql Identity", () => MagMan.UI.Health.Checks.DbIdentity(DbConfig.DATABASE_NAME))
|
|
.AddAsyncCheck($"MySql PlantLog", () => MagMan.UI.Health.Checks.DbPlantTable(DbConfig.DATABASE_NAME))
|
|
#endif
|
|
;
|
|
|
|
builder.Services.AddHealthChecksUI(s =>
|
|
{
|
|
s.AddHealthCheckEndpoint("MagMan_Services", "health");
|
|
s.SetEvaluationTimeInSeconds(60);
|
|
//s.SetEvaluationTimeInSeconds(60);
|
|
s.SetMinimumSecondsBetweenFailureNotifications(120);
|
|
s.SetApiMaxActiveRequests(5);
|
|
s.SetHeaderText("MagMan Health Check Status");
|
|
})
|
|
.AddInMemoryStorage();
|
|
|
|
|
|
// cookie applicazione da 14 gg (defaul) a 30
|
|
builder.Services.ConfigureApplicationCookie(o =>
|
|
{
|
|
o.ExpireTimeSpan = TimeSpan.FromDays(30);
|
|
o.SlidingExpiration = true;
|
|
});
|
|
// token di sicurezza dati a 3h
|
|
builder.Services.Configure<DataProtectionTokenProviderOptions>(o =>
|
|
o.TokenLifespan = TimeSpan.FromHours(3));
|
|
|
|
|
|
|
|
|
|
builder.Services.AddDbContext<UserIdentityDbContext>(options =>
|
|
options.UseSqlServer(connectionString));
|
|
|
|
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
|
|
.AddEntityFrameworkStores<UserIdentityDbContext>();
|
|
|
|
#if false
|
|
|
|
// setup MySql
|
|
//string connString = Configuration.GetConnectionString("AdminConnection");
|
|
var serverVersion = DbConfig.MysqlServerVersion(connStringDB);
|
|
services.AddDbContext<UserIdentityDbContext>(options =>
|
|
options.UseMySql(connStringDB, serverVersion));
|
|
|
|
// identity management
|
|
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
|
|
.AddRoles<IdentityRole>()
|
|
.AddEntityFrameworkStores<UserIdentityDbContext>();
|
|
|
|
#endif
|
|
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddRazorPages();
|
|
builder.Services.AddServerSideBlazor();
|
|
|
|
|
|
// servizi dati
|
|
builder.Services.AddSingleton<WeatherForecastService>();
|
|
//builder.Services.AddSingleton<StatusData>();
|
|
//builder.Services.AddSingleton<ListSelectDataSrv>();
|
|
//builder.Services.AddSingleton<OrderDataSrv>();
|
|
//builder.Services.AddSingleton<SharedMemService>();
|
|
builder.Services.AddScoped<MessageService>();
|
|
builder.Services.AddBlazoredLocalStorage();
|
|
builder.Services.AddBlazoredSessionStorage();
|
|
|
|
// gestione email
|
|
builder.Services.Configure<MailKitMailSettings>(builder.Configuration.GetSection(nameof(MailKitMailSettings)));
|
|
builder.Services.AddScoped<MailService>();
|
|
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
|
|
|
|
// servizi auth
|
|
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
|
|
#if false
|
|
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
|
|
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
|
|
|
|
# endif
|
|
|
|
builder.Services.AddSingleton<IConfiguration>(builder.Configuration);
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
// aggiunt base URL x routing corretto
|
|
app.UsePathBase(builder.Configuration["OptConf:BaseUrl"]);
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Error");
|
|
// 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.UseStaticFiles();
|
|
|
|
app.UseRouting();
|
|
|
|
// 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");
|
|
|
|
// fix forwarders
|
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
|
{
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
|
});
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
|
|
#if false
|
|
app.MapBlazorHub();
|
|
app.MapFallbackToPage("/_Host");
|
|
#endif
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
endpoints.MapBlazorHub();
|
|
endpoints.MapHealthChecksUI();
|
|
endpoints.MapHealthChecks("/health", new HealthCheckOptions
|
|
{
|
|
Predicate = _ => true,
|
|
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
|
});
|
|
endpoints.MapFallbackToPage("/_Host");
|
|
});
|
|
|
|
|
|
app.Run();
|