45cb6b9f59
- aggiunta migrations - correzioni versione ef6 da ef8 - correzioni init varie
187 lines
6.0 KiB
C#
187 lines
6.0 KiB
C#
using Microsoft.AspNetCore.Http.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.OpenApi.Models;
|
|
using MP.Data;
|
|
using MP.Data.Repository.Utils;
|
|
using MP.Data.Services.Utils;
|
|
using MP.IOC.Data;
|
|
using MP.IOC.Services;
|
|
using NLog;
|
|
using NLog.Web;
|
|
using StackExchange.Redis;
|
|
using System.Net;
|
|
using System.Reflection;
|
|
|
|
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 connStringRedis = configuration.GetConnectionString("Redis");
|
|
string redisSrvAddr = connStringRedis.Substring(0, connStringRedis.IndexOf(":"));
|
|
logger.Info("Setup REDIS OK");
|
|
|
|
|
|
// YARP base config
|
|
builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
|
|
builder.Services.AddHttpForwarder();
|
|
|
|
// HttpMessageInvoker (SocketsHttpHandler)
|
|
builder.Services.AddSingleton(sp =>
|
|
{
|
|
var handler = new SocketsHttpHandler
|
|
{
|
|
AllowAutoRedirect = false,
|
|
UseCookies = false,
|
|
PooledConnectionLifetime = TimeSpan.FromMinutes(5),
|
|
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
|
|
};
|
|
|
|
// Accetta certificati non validi (dev only)
|
|
handler.SslOptions = new System.Net.Security.SslClientAuthenticationOptions
|
|
{
|
|
RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true
|
|
};
|
|
|
|
return new HttpMessageInvoker(handler);
|
|
});
|
|
logger.Info("YARP reverse proxy configured");
|
|
|
|
// base services
|
|
builder.Services.AddSingleton<PreserveBodyTransformer>();
|
|
builder.Services.AddSingleton<RouteStatsManager>();
|
|
builder.Services.AddHostedService<MetricsFlushService>();
|
|
|
|
// da rivedere
|
|
#if false
|
|
builder.Services.AddHostedService<MetricsDbFlushService>();
|
|
#endif
|
|
|
|
// 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.AddScoped<IStatsAggrRepository, StatsAggrRepository>();
|
|
builder.Services.AddScoped<IStatsDetailRepository, StatsDetailRepository>();
|
|
builder.Services.AddScoped<IStatsAggrService, StatsAggrService>();
|
|
builder.Services.AddScoped<IStatsDetailService, StatsDetailService>();
|
|
|
|
// 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(connStringRedis);
|
|
builder.Services.AddSingleton<IConnectionMultiplexer>(redisMux);
|
|
builder.Services.AddSingleton<MpDataService>();
|
|
|
|
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}");
|
|
|
|
// RouteManager registration (singleton)
|
|
builder.Services.AddSingleton<RouteManager>();
|
|
logger.Info("Singleton Route Manager registered");
|
|
|
|
var app = builder.Build();
|
|
|
|
// aggiunt base URL x routing corretto
|
|
string baseUrl = configuration.GetValue<string>("ServerConf:BaseUrl") ?? "";
|
|
app.UsePathBase(baseUrl);
|
|
string routePath = configuration.GetValue<string>("ServerConf:RoutePath") ?? "/api/RIOB";
|
|
string fullPath = $"{baseUrl}{routePath}".Replace("//", "/");
|
|
logger.Info($"BaseUrl: {baseUrl}");
|
|
|
|
app.Use(async (ctx, next) =>
|
|
{
|
|
logger.Debug($"Incoming request PathBase='{ctx.Request.PathBase}' Path='{ctx.Request.Path}' RawTarget='{ctx.Request.GetEncodedUrl()}'");
|
|
await next();
|
|
});
|
|
|
|
|
|
|
|
// Map route to RouteManager
|
|
//app.Map("/MP/IOC/api/RIOB/{**catchAll}", async (HttpContext ctx) =>
|
|
//{
|
|
// var routeManager = ctx.RequestServices.GetRequiredService<RouteManager>();
|
|
// await routeManager.HandleAsync(ctx);
|
|
//});
|
|
|
|
// parametrizzare?!?!?
|
|
app.MapWhen(ctx =>
|
|
ctx.Request.Path.StartsWithSegments(fullPath, StringComparison.OrdinalIgnoreCase) ||
|
|
ctx.Request.Path.StartsWithSegments(routePath, StringComparison.OrdinalIgnoreCase) ||
|
|
(ctx.Request.PathBase.HasValue && ctx.Request.Path.StartsWithSegments(routePath, StringComparison.OrdinalIgnoreCase)),
|
|
builder =>
|
|
{
|
|
builder.Run(async ctx =>
|
|
{
|
|
var routeManager = ctx.RequestServices.GetRequiredService<RouteManager>();
|
|
await routeManager.HandleAsync(ctx);
|
|
});
|
|
});
|
|
|
|
logger.Info("App: route Mapped");
|
|
|
|
|
|
// 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");
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseAuthorization();
|
|
|
|
// aggiunta x index.html
|
|
app.UseDefaultFiles();
|
|
app.UseStaticFiles();
|
|
|
|
app.MapControllers();
|
|
|
|
logger.Info("Run App");
|
|
|
|
app.Run();
|