using LinqToDB; using LinqToDB.Data; using Azaion.Annotations.Auth; using Azaion.Annotations.Database; using Azaion.Annotations.Middleware; using Azaion.Annotations.Services; var builder = WebApplication.CreateBuilder(args); var databaseUrl = builder.Configuration["DATABASE_URL"] ?? Environment.GetEnvironmentVariable("DATABASE_URL") ?? "Host=localhost;Database=azaion;Username=postgres;Password=changeme"; var connectionString = databaseUrl.StartsWith("postgresql://") ? ConvertPostgresUrl(databaseUrl) : databaseUrl; var jwtSecret = builder.Configuration["JWT_SECRET"] ?? Environment.GetEnvironmentVariable("JWT_SECRET") ?? "development-secret-key-min-32-chars!!"; builder.Services.AddScoped(_ => { var options = new DataOptions() .UsePostgreSQL(connectionString); return new AppDataConnection(options); }); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddSingleton(); builder.Services.AddSingleton(new TokenService(jwtSecret)); var rabbitMqConfig = new RabbitMqConfig { Host = Environment.GetEnvironmentVariable("RABBITMQ_HOST") ?? "127.0.0.1", Port = int.TryParse(Environment.GetEnvironmentVariable("RABBITMQ_STREAM_PORT"), out var rmqPort) ? rmqPort : 5552, Username = Environment.GetEnvironmentVariable("RABBITMQ_PRODUCER_USER") ?? "azaion_producer", Password = Environment.GetEnvironmentVariable("RABBITMQ_PRODUCER_PASS") ?? "producer_pass", StreamName = Environment.GetEnvironmentVariable("RABBITMQ_STREAM_NAME") ?? "azaion-annotations" }; builder.Services.AddSingleton(rabbitMqConfig); builder.Services.AddHostedService(); builder.Services.AddJwtAuth(jwtSecret); builder.Services.AddCors(options => options.AddDefaultPolicy(policy => policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader())); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); using (var scope = app.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); DatabaseMigrator.Migrate(db); } app.UseMiddleware(); app.UseCors(); app.UseAuthentication(); app.UseAuthorization(); app.UseSwagger(); app.UseSwaggerUI(); app.MapControllers(); app.MapGet("/health", () => Results.Ok(new { status = "healthy" })); app.Run(); static string ConvertPostgresUrl(string url) { var uri = new Uri(url); var userInfo = uri.UserInfo.Split(':'); var host = uri.Host; var port = uri.Port > 0 ? uri.Port : 5432; var database = uri.AbsolutePath.TrimStart('/'); return $"Host={host};Port={port};Database={database};Username={userInfo[0]};Password={userInfo.ElementAtOrDefault(1) ?? ""}"; }