Ajout de HelloFresh

This commit is contained in:
2025-09-03 20:17:50 +02:00
parent bcef0a472b
commit d287112b7d
429 changed files with 82881 additions and 22074 deletions

View File

@@ -1,8 +1,13 @@
using administration.Models;
using administration.Models.Finances;
using administration.Models.HelloFresh;
using administration.Services;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using IngredientsAI.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
using User = administration.Models.User;
namespace administration
{
@@ -10,51 +15,65 @@ namespace administration
{
public static void Main(string[] args)
{
// Charger les variables d'environnement depuis .env
DotNetEnv.Env.Load();
var builder = WebApplication.CreateBuilder(args);
// ==============================================
// 1Configurer la base de données
// 1Base de données
// ==============================================
var dbConnection = Environment.GetEnvironmentVariable("ADMIN_DB_CONNECTION");
if (string.IsNullOrEmpty(dbConnection))
throw new Exception("❌ ADMIN_DB_CONNECTION est introuvable dans les variables d'environnement.");
throw new Exception("❌ ADMIN_DB_CONNECTION est introuvable.");
builder.Services.AddDbContext<FinancesContext>(options =>
options.UseSqlServer(dbConnection, sqlOptions => sqlOptions.EnableRetryOnFailure())
);
options.UseSqlServer(dbConnection, sqlOptions => sqlOptions.EnableRetryOnFailure()));
builder.Services.AddDbContext<HelloFreshContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("HelloFreshConnection")));
builder.Services.AddDbContext<LayoutDataContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
AppSettings.Initialize(builder.Configuration);
builder.Services.AddSingleton<OllamaService>();
// ==============================================
// 2Ajouter la session
// 2Session
// ==============================================
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(300); // Expiration session
options.IdleTimeout = TimeSpan.FromHours(8);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
// ==============================================
// 3⃣ Ajouter lauthentification Basic
// 3⃣ Authentification par cookie
// ==============================================
builder.Services.AddAuthentication("BasicAuthentication")
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Connections/Login";
options.AccessDeniedPath = "/Connections/Login";
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromDays(14);
});
builder.Services.AddAuthorization();
builder.Services.AddControllers().AddJsonOptions(o =>
{
o.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
// ==============================================
// 4Ajouter MVC + dépendances
// 4MVC + services
// ==============================================
builder.Services.AddControllersWithViews();
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<IUserSessionService, UserSessionService>();
builder.Services.AddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
// ==============================================
// 5⃣ CORS pour le frontend
// 5⃣ CORS
// ==============================================
builder.Services.AddCors(options =>
{
@@ -66,14 +85,31 @@ namespace administration
});
});
// ==============================================
// 6⃣ Construire l'application
// ==============================================
var app = builder.Build();
builder.Services.AddHttpClient(); // déjà présent chez toi
builder.Services.AddHttpClient("ollama", (sp, client) =>
{
var cfg = sp.GetRequiredService<IConfiguration>();
var baseUrl = cfg["Ollama:Url"] ?? "http://ollama:11434";
client.BaseAddress = new Uri(baseUrl);
client.Timeout = TimeSpan.FromSeconds(25);
})
#if DEBUG
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
});
#endif
// ...
// ...
builder.Logging.AddFilter("administration.Services.BasicAuthenticationHandler", LogLevel.Warning);
var app = builder.Build();
// ==============================================
// 7️⃣ Middleware Pipeline
// ==============================================
// 6️⃣ Pipeline
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
@@ -85,14 +121,11 @@ namespace administration
app.UseRouting();
app.UseCors("AllowFrontend");
app.UseSession();
app.UseAuthentication();
app.UseSession(); // ✅ toujours avant auth
app.UseAuthentication(); // ✅ s'applique à tout le site
app.UseAuthorization();
// ==============================================
// 8⃣ Routes
// ==============================================
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");