163 lines
5.2 KiB
C#
163 lines
5.2 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
using Microsoft.OpenApi.Models;
|
|
using MP.Core.Conf;
|
|
using MP.Data;
|
|
using MP.IOC.Components;
|
|
using MP.IOC.Data;
|
|
using MP.IOC.Services;
|
|
using NLog;
|
|
using NLog.Web;
|
|
using StackExchange.Redis;
|
|
using System.Reflection;
|
|
using ZiggyCreatures.Caching.Fusion;
|
|
using ZiggyCreatures.Caching.Fusion.Serialization;
|
|
using ZiggyCreatures.Caching.Fusion.Serialization.NewtonsoftJson;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// recupero env corrente
|
|
var env = builder.Environment;
|
|
var logger = LogManager.Setup()
|
|
.LoadConfigurationFromAppSettings()
|
|
.GetCurrentClassLogger();
|
|
|
|
var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString();
|
|
logger.Info($"MP.IOC | Program.cs: startup | v.{assemblyVersion}");
|
|
logger.Info($"Current ASPNETCORE_ENVIRONMENT: {env.EnvironmentName}");
|
|
|
|
// Config setup
|
|
ConfigurationManager configuration = builder.Configuration;
|
|
// REDIS setup
|
|
logger.Info("Config OK");
|
|
string confRedis = configuration.GetConnectionString("Redis") ?? "localhost:6379";
|
|
string redisSrvAddr = confRedis.Substring(0, confRedis.IndexOf(":"));
|
|
logger.Info("Setup REDIS OK");
|
|
|
|
|
|
builder.Services.Configure<RedisScriptsConfig>(
|
|
builder.Configuration.GetSection("RedisScripts"));
|
|
logger.Info("RedisScript Provider configured");
|
|
|
|
// Metodi principali x accesso dati
|
|
var connStr = builder.Configuration.GetConnectionString("MP.Data")
|
|
?? throw new InvalidOperationException("ConnString 'MP.Data' mancante.");
|
|
|
|
builder.Services.AddMemoryCache();
|
|
|
|
builder.Services.AddDbContextFactory<MoonProContext>(options =>
|
|
options.UseSqlServer(connStr)
|
|
.EnableSensitiveDataLogging(false) // true solo in Sviluppo
|
|
.ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)));
|
|
|
|
// MP.Data DbContext for Stats repositories
|
|
string utilsConnString = builder.Configuration.GetConnectionString("MP.Utils") ?? "Server=localhost;Database=MoonPro_Utils; integrated security=True; MultipleActiveResultSets=True; App=MP.IOC;";
|
|
builder.Services.AddDbContextFactory<MoonPro_UtilsContext>(options =>
|
|
options.UseSqlServer(utilsConnString));
|
|
|
|
// MP.Data Services Utils - Statistiche DB
|
|
builder.Services.AddIocDataLayer();
|
|
|
|
// Registra i servizi per Blazor
|
|
builder.Services.AddRazorComponents()
|
|
.AddInteractiveServerComponents();
|
|
// Aggiungi Health Checks
|
|
//builder.Services.AddHealthChecks()
|
|
// .AddSqlServer(builder.Configuration.GetConnectionString("CoreDb"));
|
|
|
|
// generic controller
|
|
builder.Services.AddControllers();
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
//builder.Services.AddSwaggerGen();
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
c.SwaggerDoc("v1", new OpenApiInfo
|
|
{
|
|
Title = "EgaWare's Mapo Rest API",
|
|
Version = $"v1 | {assemblyVersion}",
|
|
Description = $"API documentation for v1 | AssemblyVersion {assemblyVersion}"
|
|
});
|
|
});
|
|
|
|
// avvio oggetto shared x redis...
|
|
var redisMux = ConnectionMultiplexer.Connect(confRedis);
|
|
builder.Services.AddSingleton<IConnectionMultiplexer>(redisMux);
|
|
|
|
// oggetto principale accesso dati
|
|
//builder.Services.AddScoped<MpDataService>();
|
|
builder.Services.AddSingleton<MpDataService>();
|
|
|
|
// 1. Registra il serializzatore NewtonsoftJson per FusionCache
|
|
builder.Services.AddSingleton<IFusionCacheSerializer>(new FusionCacheNewtonsoftJsonSerializer());
|
|
|
|
// 2. Configura FusionCache solo per la memoria (L1)
|
|
builder.Services.AddFusionCache()
|
|
.WithDefaultEntryOptions(options =>
|
|
{
|
|
// Durata di default dei dati in memoria
|
|
options.Duration = TimeSpan.FromMinutes(1);
|
|
|
|
// Jitter: variazione casuale alla scadenza per evitare scadenze in blocco
|
|
options.JitterMaxDuration = TimeSpan.FromSeconds(5);
|
|
});
|
|
|
|
logger.Info("Standard service configured");
|
|
|
|
// WeightProvider: Redis/Memory da config
|
|
var weightOnRedis = builder.Configuration.GetValue<bool>("ServerConf:RedisWeight", false);
|
|
if (weightOnRedis)
|
|
{
|
|
builder.Services.AddSingleton<IConnectionMultiplexer>(redisMux);
|
|
builder.Services.AddSingleton<IWeightProvider, RedisWeightProvider>();
|
|
}
|
|
else
|
|
{
|
|
builder.Services.AddSingleton<IWeightProvider, InMemoryWeightProvider>();
|
|
}
|
|
logger.Info($"Weight service configured | use Redis: {weightOnRedis}");
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
// aggiunt base URL x routing corretto
|
|
string baseUrl = configuration.GetValue<string>("ServerConf:BaseUrlIoc") ?? "/MP/";
|
|
app.UsePathBase(baseUrl);
|
|
logger.Info($"BaseUrl: {baseUrl}");
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment() || app.Environment.IsStaging())
|
|
{
|
|
app.UseSwagger();
|
|
//app.UseSwaggerUI();
|
|
app.UseSwaggerUI(c =>
|
|
{
|
|
c.SwaggerEndpoint($"{baseUrl}swagger/v1/swagger.json", "EgalWare's MAPO API v1");
|
|
});
|
|
}
|
|
logger.Info("Added swagger");
|
|
|
|
//// redirect su https: disattiato x API
|
|
//app.UseHttpsRedirection();
|
|
|
|
app.UseAuthorization();
|
|
|
|
// aggiunta x index.html
|
|
app.UseDefaultFiles();
|
|
app.UseStaticFiles();
|
|
app.UseAntiforgery();
|
|
|
|
// Mappatura delle API
|
|
app.MapControllers();
|
|
|
|
// Mappatura della Dashboard Blazor
|
|
app.MapRazorComponents<App>()
|
|
.AddInteractiveServerRenderMode();
|
|
|
|
//app.MapHealthChecks("/health");
|
|
|
|
logger.Info("Run App");
|
|
|
|
app.Run();
|