Files
ha-shopping-list/HA_Shopping_List.Server/Program.cs
T
2026-11-08 20:30:40 +01:00

59 lines
1.6 KiB
C#

using HA_Shopping_List.Server.Components;
using HA_Shopping_List.Server.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Blazor Server interactif
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents(options => options.DetailedErrors = true);
// Persistance dans /data (Home Assistant) ou dans le dossier local en Dev
var dbFolder = Directory.Exists("/data") ? "/data" : AppContext.BaseDirectory;
Directory.CreateDirectory(dbFolder);
var dbPath = Path.Combine(dbFolder, "shopping.db");
builder.Services.AddDbContextFactory<AppDbContext>(options =>
options.UseSqlite($"Data Source={dbPath}"));
var app = builder.Build();
// Création automatique de la base SQLite au démarrage
using (var scope = app.Services.CreateScope())
{
var factory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<AppDbContext>>();
using var db = factory.CreateDbContext();
db.Database.EnsureCreated();
}
#if DEBUG
app.Use(async (context, next) =>
{
context.Response.Headers.Append("Permissions-Policy", "unload=(self)");
await next();
});
#endif
// Gestion du préfixe dynamique Ingress de Home Assistant
app.Use(async (context, next) =>
{
if (context.Request.Headers.TryGetValue("X-Ingress-Path", out var ingressPath))
{
context.Request.PathBase = ingressPath.ToString();
}
await next();
});
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
}
app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
// Port Ingress standard
app.Run("http://0.0.0.0:8099");