Ajout de HelloFresh
This commit is contained in:
87
Program.cs
87
Program.cs
@@ -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);
|
||||
|
||||
// ==============================================
|
||||
// 1️⃣ Configurer la base de données
|
||||
// 1️⃣ Base 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>();
|
||||
|
||||
// ==============================================
|
||||
// 2️⃣ Ajouter la session
|
||||
// 2️⃣ Session
|
||||
// ==============================================
|
||||
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 l’authentification 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;
|
||||
});
|
||||
|
||||
// ==============================================
|
||||
// 4️⃣ Ajouter MVC + dépendances
|
||||
// 4️⃣ MVC + 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?}");
|
||||
|
||||
Reference in New Issue
Block a user