using System.Net.Http.Headers; using Microsoft.EntityFrameworkCore; using PaperlessAI.Context; using PaperlessAI.Interfaces; using PaperlessAI.Jobs; using PaperlessAI.Models; using PaperlessAI.Repositories; using PaperlessAI.Services; using Serilog; var builder = WebApplication.CreateBuilder(args); // Services builder.Services.AddScoped(); builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddHttpClient((serviceProvider, client) => { var config = serviceProvider.GetRequiredService(); var baseUrl = config["LlmAgent:Url"] ?? throw new InvalidOperationException("LlmAgent:Url configuration missing."); var token = config["LlmAgent:Token"] ?? throw new InvalidOperationException("LlmAgent:Token configuration missing."); var acceptMediaType = config["LlmAgent:MediaType"] ?? throw new InvalidOperationException("LlmAgent:MediaType configuration missing."); var timeoutSeconds = int.Parse(config["LlmAgent:TimeoutSeconds"] ?? throw new InvalidOperationException("LlmAgent:TimeoutSeconds configuration missing.")); client.Timeout = TimeSpan.FromSeconds(timeoutSeconds); client.BaseAddress = new Uri(baseUrl); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue(acceptMediaType) ); }); // Repositories builder.Services.AddScoped(); // Other builder.Services.AddControllers(); // Database builder.Services.AddDbContextPool(options => { options.UseMySql( builder.Configuration.GetConnectionString("MariaDbConnectionString"), ServerVersion.Parse("12.0.2-mariadb") ) .LogTo(Console.WriteLine, LogLevel.Information) .EnableSensitiveDataLogging() .EnableDetailedErrors(); }); // Logging Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console() .WriteTo.File( Path.Combine(AppContext.BaseDirectory, "logs", "log-.txt"), rollingInterval: RollingInterval.Day, retainedFileCountLimit: 30) .CreateLogger(); builder.Host.UseSerilog(); // Jobs builder.Services.AddHostedService(); // App var app = builder.Build(); // Configure the HTTP request pipeline. app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();