Files
PaperlessAI/Program.cs
T
2026-10-12 01:14:13 +02:00

77 lines
2.5 KiB
C#

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<Configuration>();
builder.Services.AddSingleton<DocumentProcessingService>();
builder.Services.AddScoped<PaperlessDocumentService>();
builder.Services.AddScoped<LLMAgentService>();
builder.Services.AddHttpClient<ApiService>((serviceProvider, client) =>
{
var config = serviceProvider.GetRequiredService<IConfiguration>();
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<PaperlessDocumentRepository>();
// Other
builder.Services.AddControllers();
// Database
builder.Services.AddDbContextPool<MyDbContext>(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<CheckDocumentsToProcessJob>();
// App
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();