Files
lux/Lux.API/Program.cs
T
2025-07-22 19:13:53 +02:00

90 lines
2.8 KiB
C#

using EgwCoreLib.Lux.Data.Services;
using EgwMultiEngineManager;
using Lux.API.Services;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.OpenApi.Models;
using NLog;
using NLog.Web;
using StackExchange.Redis;
using System.Reflection;
using System.Runtime.Intrinsics.X86;
var builder = WebApplication.CreateBuilder(args);
var logger = LogManager.Setup()
.LoadConfigurationFromAppSettings()
.GetCurrentClassLogger();
var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version?.ToString();
ConfigurationManager configuration = builder.Configuration;
logger.Info($"Program.cs: startup | v.{assemblyVersion}");
// Add services to the container.
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 Lux API",
Version = $"v1 | {assemblyVersion}",
Description = $"API documentation for v1 | AssemblyVersion {assemblyVersion}"
});
});
// costruzione connectionMultiplexer redis...
string connStr = configuration.GetConnectionString("Redis") ?? "localhost";
ConnectionMultiplexer redisConn = ConnectionMultiplexer.Connect(connStr);
// registro connMultiplexer REDIS
builder.Services.AddSingleton<IConnectionMultiplexer>(redisConn);
// registro wrapper servizi REDIS
builder.Services.AddSingleton<IRedisService, RedisService>();
builder.Services.AddSingleton<RedisSubscriptionManager>();
// altri servizi!
builder.Services.AddSingleton<ImageCacheService>();
builder.Services.AddSingleton<ExternalMessageProcessor>();
builder.Services.AddHostedService<RedisSubscriberService>();
var app = builder.Build();
// aggiunt base URL x routing corretto
string baseUrl = configuration.GetValue<string>("ServerConf:BaseUrl") ?? "";
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 Lux API v1");
});
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
// inserisco evento in chiusura x fermare i thread di calcolo sottostanti:
// https://shazwazza.com/post/aspnet-core-application-shutdown-events/
// https://dotnetblog.asphostportal.com/how-to-handle-application-shutdown-in-asp-net-core/
app.Lifetime.ApplicationStopping.Register(() =>
{
#if false
if (myExecProcessManager != null)
{
myExecProcessManager.StopExecutionThread();
}
#endif
});
// avvio app
app.Run();