191 lines
7.5 KiB
C#
191 lines
7.5 KiB
C#
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Localization;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.OpenApi.Models;
|
|
using MP.Data;
|
|
using MP.Data.Services;
|
|
using MP.Stats.Data;
|
|
using MP.TaskMan.Services;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using ZiggyCreatures.Caching.Fusion;
|
|
using ZiggyCreatures.Caching.Fusion.Backplane.StackExchangeRedis;
|
|
using ZiggyCreatures.Caching.Fusion.Serialization.NewtonsoftJson;
|
|
|
|
namespace MP.Stats
|
|
{
|
|
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)
|
|
{
|
|
// aggiunt base URL x routing corretto
|
|
app.UsePathBase(Configuration.GetValue<string>("SpecialConf:AppUrl"));
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
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();
|
|
|
|
// aggiunta swagger, alla pagina /swagger/index.html
|
|
if (env.IsDevelopment() || env.IsStaging())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
// Configure the HTTP request pipeline.
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
endpoints.MapBlazorHub();
|
|
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)
|
|
{
|
|
services.AddControllers();
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
services.AddEndpointsApiExplorer();
|
|
services.AddSwaggerGen(options =>
|
|
{
|
|
options.SwaggerDoc("v1", new OpenApiInfo
|
|
{
|
|
Version = "v1",
|
|
Title = "MP-STATS API",
|
|
Description = "ASP.NET Core Web API for managing MP-STATS items",
|
|
TermsOfService = new Uri("https://www.egalware.com/mp-stats-api-terms"),
|
|
//Contact = new OpenApiContact
|
|
//{
|
|
// Name = "Example Contact",
|
|
// Url = new Uri("https://example.com/contact")
|
|
//},
|
|
//License = new OpenApiLicense
|
|
//{
|
|
// Name = "Example License",
|
|
// Url = new Uri("https://example.com/license")
|
|
//}
|
|
});
|
|
|
|
// using System.Reflection;
|
|
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
|
|
});
|
|
|
|
//// 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<SqlErrorLog>(options =>
|
|
//{
|
|
// options.ConnectionString = elmaConn;
|
|
//});
|
|
|
|
// REDIS setup
|
|
var cString = Configuration.GetConnectionString("Redis");
|
|
string connStringRedis = cString ?? "localhost:6379, DefaultDatabase=5, connectTimeout=5000, syncTimeout=5000, asyncTimeout=5000, abortConnect=false, ssl=false";
|
|
// avvio oggetto shared x redis...
|
|
IConnectionMultiplexer redisMultiplexer = ConnectionMultiplexer.Connect(connStringRedis);
|
|
|
|
// ✅ FusionCache
|
|
services.AddFusionCache()
|
|
.WithDistributedCache(sp => sp.GetRequiredService<IDistributedCache>())
|
|
.WithSerializer(new FusionCacheNewtonsoftJsonSerializer())
|
|
.WithBackplane(new RedisBackplane(new RedisBackplaneOptions
|
|
{
|
|
ConnectionMultiplexerFactory = () => Task.FromResult(redisMultiplexer)
|
|
}));
|
|
|
|
// Add services x accesso dati
|
|
services.AddSingleton<IConnectionMultiplexer>(redisMultiplexer);
|
|
|
|
// aggiungo il costruttore x i vari DbContextFactory
|
|
var connStr = Configuration.GetConnectionString("MP.Stats")
|
|
?? throw new InvalidOperationException("ConnString 'MP.Stats' mancante.");
|
|
services.AddDbContextFactory<MoonPro_STATSContext>(options =>
|
|
options.UseSqlServer(connStr)
|
|
.EnableSensitiveDataLogging(false) // true solo in Sviluppo
|
|
.ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)));
|
|
|
|
var connStrVoc = Configuration.GetConnectionString("MP.Voc")
|
|
?? throw new InvalidOperationException("ConnString 'MP.Voc' mancante.");
|
|
services.AddDbContextFactory<MoonPro_VocContext>(options =>
|
|
options.UseSqlServer(connStrVoc)
|
|
.EnableSensitiveDataLogging(false) // true solo in Sviluppo
|
|
.ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)));
|
|
|
|
|
|
services.AddLocalization();
|
|
|
|
services.AddRazorPages();
|
|
services.AddServerSideBlazor();
|
|
services.AddSingleton<IConfiguration>(Configuration);
|
|
|
|
services.AddStatsDataLayer();
|
|
//services.AddSingleton<TranslateSrv>();
|
|
|
|
services.AddSingleton<MpStatsService>();
|
|
services.AddSingleton<TaskService>();
|
|
|
|
services.AddScoped<MP.Stats.Data.MessageService>();
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |