using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; using ElmahCore.Mvc; using GWMS.Data; using GWMS.UI.Areas.Identity; using GWMS.UI.Data; using HealthChecks.UI.Client; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.UI; using Microsoft.AspNetCore.Localization; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Hosting; using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Threading.Tasks; namespace GWMS.UI { public class Startup { #region Public Constructors public Startup(IConfiguration configuration) { Configuration = configuration; } #endregion Public Constructors #region Public Properties public IConfiguration Configuration { get; } #endregion Public Properties #region Public Methods // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseMigrationsEndPoint(); } else { 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(); } // 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"); // Registrazione Elmah: // https://github.com/ElmahCore/ElmahCore app.UseElmah(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapBlazorHub(); endpoints.MapHealthChecksUI(); endpoints.MapHealthChecks("/health", new HealthCheckOptions { Predicate = _ => true, ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); endpoints.MapFallbackToPage("/_Host"); }); } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { // init info x DB string dbServerAddr = Configuration["DbConfig:Server"]; string nKey = Configuration["DbConfig:nKey"]; string sKey = 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(); // altri parametri per check vari string connStringDB = DbConfig.CONNECTION_STRING; string connStringRedis = Configuration.GetConnectionString("Redis"); string redisSrvAddr = connStringRedis.Substring(0, connStringRedis.IndexOf(":")); var qrCodeUri = new Uri(Configuration["ZCodeUrl"]); string qrCodeAddr = qrCodeUri.Host; // healthchecks services.AddHealthChecks() .AddMySql(connStringDB, "MySql instance") .AddAsyncCheck($"DB PING ({dbServerAddr})", () => Health.Checks.PingCheck(dbServerAddr)) .AddAsyncCheck($"Redis PING ({redisSrvAddr})", () => Health.Checks.PingCheck(redisSrvAddr)) .AddAsyncCheck($"QrCode PING ({qrCodeAddr})", () => Health.Checks.PingCheck(qrCodeAddr)) .AddProcessAllocatedMemoryHealthCheck(512, "Max Process memory (<512MB)", failureStatus: HealthStatus.Degraded) // 512 MB max allocated memory .AddRedis(Configuration.GetConnectionString("Redis"), "Redis", failureStatus: HealthStatus.Degraded) .AddUrlGroup(new Uri(Configuration["ZCodeUrl"]), name: $"QrCode Gen ({Configuration["ZCodeUrl"]})", failureStatus: HealthStatus.Degraded) .AddAsyncCheck($"MySql Root User", () => Health.Checks.DbUserRoot("MySql")) .AddAsyncCheck($"MySql App Users", () => Health.Checks.DbUserApp(DbConfig.DATABASE_NAME)) .AddAsyncCheck($"MySql Identity", () => Health.Checks.DbIdentity(DbConfig.DATABASE_NAME)) .AddAsyncCheck($"MySql PlantLog", () => Health.Checks.DbPlantLogTable(DbConfig.DATABASE_NAME)) ; //.AddDiskStorageHealthCheck(s => s.AddDrive("C:\\", 1024)) // 1024 MB (1 GB) free minimum //.AddProcessHealthCheck("ProcessName", p => p.Length > 0) // check if process is running //.AddWindowsServiceHealthCheck("someservice", s => s.Status == ServiceControllerStatus.Running); services .AddHealthChecksUI(s => { s.AddHealthCheckEndpoint("GWMS_Services", "health"); s.SetEvaluationTimeInSeconds(15); s.SetMinimumSecondsBetweenFailureNotifications(60); s.SetApiMaxActiveRequests(5); s.SetHeaderText("GWMS Health Check Status"); }) .AddInMemoryStorage(); // setup MySql //string connString = Configuration.GetConnectionString("AdminConnection"); var serverVersion = DbConfig.MysqlServerVersion(connStringDB); services.AddDbContext(options => options.UseMySql(connStringDB, serverVersion)); services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores(); services.AddBlazorise(options => { options.ChangeTextOnKeyPress = true; // optional }) .AddBootstrapProviders() .AddFontAwesomeIcons(); // Elmah services.AddElmah(); //string elmaConn = "Data Source=SQL2016DEV;Initial Catalog=Elmah;User ID=sa;Password=keyhammer16;integrated security=False;MultipleActiveResultSets=True;App=SHERPA.BBM;"; //services.AddElmah(options => //{ // options.ConnectionString = elmaConn; //}); services.AddStackExchangeRedisCache(options => { //options.Configuration = "localhost:6379"; options.ConfigurationOptions = new StackExchange.Redis.ConfigurationOptions() { KeepAlive = 180, DefaultDatabase = 12, EndPoints = { { "localhost", 6379 } } }; options.InstanceName = "GWMS:"; }); services.AddLocalization(); services.AddRazorPages(); services.AddServerSideBlazor(); services.AddScoped>(); services.AddDatabaseDeveloperPageExceptionFilter(); services.AddSingleton(Configuration); //services.AddTransient(); services.AddSingleton(); services.AddScoped(); #if false // aggiunta quartz scheduler var scheduler = StdSchedulerFactory.GetDefaultScheduler().GetAwaiter().GetResult(); services.AddSingleton(scheduler); services.AddHostedService(); #endif } #endregion Public Methods } }