Files
lux/Lux.API/Program.cs
T

70 lines
2.1 KiB
C#

using EgwMultiEngineManager;
using Lux.API.Services;
using Lux.Data.Services;
using NLog;
using NLog.Web;
using StackExchange.Redis;
var builder = WebApplication.CreateBuilder(args);
var logger = LogManager.Setup()
.LoadConfigurationFromAppSettings()
.GetCurrentClassLogger();
ConfigurationManager configuration = builder.Configuration;
logger.Info("Program.cs: startup");
// 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();
// 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.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();