Files
mapo-core/MP.SPEC/Program.cs
T
Samuele Locatelli a66c0fdadf SPEC:
_ aggiunto gestione mimetype coinfigurabile
- fix AppUri base
2024-10-29 17:21:50 +01:00

145 lines
4.7 KiB
C#

using Blazored.LocalStorage;
using Blazored.SessionStorage;
using Microsoft.AspNetCore.Authentication.Negotiate;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using MP.SPEC.Components;
using MP.SPEC.Data;
using MP.SPEC.Services;
using NLog;
using NLog.Web;
using StackExchange.Redis;
var builder = WebApplication.CreateBuilder(args);
/*--------------------
* Note migrazione startup.cs --> program.cs:
*
* - https://stackoverflow.com/questions/69722872/asp-net-core-6-how-to-access-ConfMan-during-startup
* - https://docs.microsoft.com/en-us/aspnet/core/migration/50-to-60?view=aspnetcore-5.0&tabs=visual-studio#where-do-i-put-state-that-was-stored-as-fields-in-my-program-or-startup-class
*
* */
var logger = LogManager.Setup()
.LoadConfigurationFromAppSettings()
.GetCurrentClassLogger();
logger.Info("Program.cs: startup");
ConfigurationManager configuration = builder.Configuration;
// REDIS setup
logger.Info("Setup REDIS");
string connStringRedis = configuration.GetConnectionString("Redis");
//string connStringRedis = ConfMan.GetConnectionString("RedisAdmin");
string redisSrvAddr = connStringRedis.Substring(0, connStringRedis.IndexOf(":"));
// avvio oggetto shared x redis...
var redisMultiplexer = ConnectionMultiplexer.Connect(connStringRedis);
// Add services to the container.
logger.Info("Setup Auth");
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
builder.Services.AddAuthorization(options =>
{
// By default, all incoming requests will be authorized according to the default policy.
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<IConnectionMultiplexer>(redisMultiplexer);
builder.Services.AddSingleton<MpDataService>();
builder.Services.AddScoped<MessageService>();
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddBlazoredSessionStorage();
builder.Services.AddHttpClient();
builder.Services.AddSingleton<IOApiService>();
logger.Info("Aggiunti services");
var app = builder.Build();
logger.Info("Build App");
// aggiunt base URL x routing corretto
app.UsePathBase(configuration.GetValue<string>("SpecialConf:AppUrl"));
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
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();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
// gestione static files: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-8.0
string BasePathOdlReturn = configuration.GetValue<string>("ServerConf:BasePathOdlReturn") ?? configuration.GetValue<string>("OptConf:BasePathOdlReturn") ?? "";
if (!string.IsNullOrEmpty(BasePathOdlReturn))
{
// preparo mappings opzionali se presenti in conf...
var provider = new FileExtensionContentTypeProvider();
// vedere https://code-maze.com/dotnet-appsettings-json-content-to-dictionary/
var mimeSection = configuration.GetSection("ServerConf:MimeMappings");
if (mimeSection != null)
{
var mimeDict = mimeSection
.AsEnumerable()
.Where(x => !string.IsNullOrWhiteSpace(x.Value))
.ToDictionary(x => x.Key.Replace("ServerConf:MimeMappings:", ""), x => x.Value);
// se ne ho trovati
if (mimeDict != null && mimeDict.Count > 0)
{
// li aggiungo! vedere
// https://thechrisgreen.com/2022/05/add-a-mime-type-to-an-asp-net-core-net-6-app/
// https://harrybellamy.com/posts/getting-mime-types-from-file-extensions-in-net-core/
foreach (var item in mimeDict)
{
// Add new mappings
provider.Mappings[item.Key] = item.Value;
}
}
}
// verifico esista folder
if (Directory.Exists(BasePathOdlReturn))
{
// gestione cartella x file ritornati x ODL
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = provider,
FileProvider = new PhysicalFileProvider(BasePathOdlReturn),
RequestPath = "/RET_DATA",
});
}
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
//app.MapBlazorHub();
//app.MapFallbackToPage("/_Host");
logger.Info("Run App");
app.Run();