87defc38b8
- Implementato supporto per percorsi database specifici per OS (Windows, Linux, macOS) - Rimossi file temporanei e di test non necessari dal CredentialManager - Aggiunto supporto per directory di sistema standard per il database delle credenziali - Mantenuto il codice legacy commentato per retrocompatibilità - Aggiunte cartelle di test per future implementazioni
107 lines
3.2 KiB
C#
107 lines
3.2 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
using DataConnection;
|
|
using DataConnection.EF;
|
|
using DataConnection.Interfaces;
|
|
using DataConnection.EF.DatabaseDiscovery;
|
|
using DataConnection.Enums;
|
|
using DataConnection.CredentialManagement;
|
|
using CredentialManager;
|
|
using Data_Coupler.Services;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Data_Coupler.BackgrounServices;
|
|
|
|
// Registra il provider di encoding per ExcelDataReader (necessario per file .xls)
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddRazorPages();
|
|
builder.Services.AddServerSideBlazor();
|
|
builder.Services.AddWindowsService();
|
|
builder.Services.AddHostedService<BackgroundServices>();
|
|
|
|
#region Database Directory Path management
|
|
|
|
string dbPath = string.Empty;
|
|
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Data_Coupler", "credentials.db");
|
|
}
|
|
else if (OperatingSystem.IsLinux())
|
|
{
|
|
dbPath = "/var/lib/Data_Coupler/credentials.db";
|
|
}
|
|
else if (OperatingSystem.IsMacOS())
|
|
{
|
|
dbPath = "/Library/Application Support/Data_Coupler/credentials.db";
|
|
}
|
|
|
|
var dbDirectory = Path.GetDirectoryName(dbPath);
|
|
if (!Directory.Exists(dbDirectory))
|
|
{
|
|
Directory.CreateDirectory(dbDirectory!);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region OLD DB PATH
|
|
// // Add CredentialManager services
|
|
// var contentRoot = builder.Environment.ContentRootPath;
|
|
// var dataPath = Path.Combine(contentRoot, "wwwroot", "data");
|
|
// Directory.CreateDirectory(dataPath); // Assicurati che la directory esista
|
|
// var dbPath = Path.Combine(dataPath, "credentials.db");
|
|
#endregion
|
|
|
|
builder.Services.AddDataConnectionCredentialManagement($"Data Source={dbPath}");
|
|
|
|
// Register IHttpClientFactory
|
|
builder.Services.AddHttpClient();
|
|
|
|
// Register Data Connection Factory
|
|
builder.Services.AddScoped<IDataConnectionFactory, DataConnectionFactory>();
|
|
builder.WebHost.UseUrls("http://*:7550");
|
|
|
|
var app = builder.Build();
|
|
|
|
// Initialize database
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
|
|
try
|
|
{
|
|
logger.LogInformation("Inizializzazione database in corso...");
|
|
var dbInitializer = scope.ServiceProvider.GetRequiredService<CredentialManager.Services.IDatabaseInitializer>();
|
|
dbInitializer.InitializeAsync().GetAwaiter().GetResult();
|
|
logger.LogInformation("Database inizializzato con successo.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Errore durante l'inizializzazione del database: {Message}", ex.Message);
|
|
throw; // Rilancia l'eccezione per non far partire l'app con un database non funzionante
|
|
}
|
|
}
|
|
|
|
// 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();
|
|
|
|
app.UseRouting();
|
|
|
|
app.MapBlazorHub();
|
|
app.MapFallbackToPage("/_Host");
|
|
|
|
app.Run();
|
|
|