using MongoDB.Driver; using OnlyOneAccessTemplate.Services; var builder = WebApplication.CreateBuilder(args); // Add services to the container builder.Services.AddControllersWithViews(); // Session support builder.Services.AddDistributedMemoryCache(); builder.Services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(30); options.Cookie.HttpOnly = true; options.Cookie.IsEssential = true; }); // MongoDB Configuration builder.Services.AddSingleton(sp => { var connectionString = builder.Configuration.GetConnectionString("MongoDB"); return new MongoClient(connectionString); }); builder.Services.AddScoped(sp => { var client = sp.GetRequiredService(); var databaseName = builder.Configuration.GetValue("MongoDB:DatabaseName"); return client.GetDatabase(databaseName); }); // Custom Services builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); // Converter Services - Registrar todos os conversores disponíveis builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); // Adicione aqui novos conversores conforme necessário: // builder.Services.AddScoped(); // HttpClient for external calls builder.Services.AddHttpClient(); builder.Services.AddHttpClient(); // Response Compression builder.Services.AddResponseCompression(options => { options.EnableForHttps = true; }); // Response Caching builder.Services.AddResponseCaching(); // Memory Cache builder.Services.AddMemoryCache(); // Loggingbuilder.Logging.ClearProviders(); builder.Logging.AddConsole(); builder.Logging.AddDebug(); var app = builder.Build(); // Configure the HTTP request pipeline if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); // Session app.UseSession(); // Response Compression & Caching app.UseResponseCompression(); app.UseResponseCaching(); app.UseRouting(); // Custom routing for multilingual support app.MapControllerRoute( name: "converter-api", pattern: "converter/api/{action}/{converterType?}", defaults: new { controller = "Converter" }); app.MapControllerRoute( name: "converter-config", pattern: "converter/config/{converterType}", defaults: new { controller = "Converter", action = "GetConverterConfig" }); app.MapControllerRoute( name: "default-pt", pattern: "{controller=Home}/{action=Index}/{id?}"); app.MapControllerRoute( name: "pt-explicit", pattern: "pt/{controller=Home}/{action=Index}/{id?}"); app.MapControllerRoute( name: "english", pattern: "en/{controller=En}/{action=Index}/{id?}"); app.MapControllerRoute( name: "spanish", pattern: "es/{controller=Es}/{action=Index}/{id?}"); // SEO Routes app.MapGet("/sitemap.xml", async (ISiteConfigurationService siteConfig) => { var sitemap = await siteConfig.GenerateSitemapAsync(); return Results.Content(sitemap, "application/xml"); }); app.MapGet("/robots.txt", async (ISiteConfigurationService siteConfig) => { var robots = await siteConfig.GenerateRobotsAsync(); return Results.Content(robots, "text/plain"); }); // Health check endpoint app.MapGet("/health", () => Results.Ok(new { status = "healthy", timestamp = DateTime.UtcNow })); // Initialize default data on startup (only in development) if (app.Environment.IsDevelopment()) { using var scope = app.Services.CreateScope(); var siteConfigService = scope.ServiceProvider.GetRequiredService(); var logger = scope.ServiceProvider.GetRequiredService>(); try { // Verificar se já existem configurações var languages = new[] { "pt", "en", "es" }; foreach (var lang in languages) { var configExists = await siteConfigService.ConfigurationExistsAsync(lang); if (!configExists) { logger.LogInformation("Criando configuração padrão para idioma: {Language}", lang); _ = await siteConfigService.GetConfigurationAsync(lang); // Isso criará a configuração padrão } } } catch (Exception ex) { logger.LogError(ex, "Erro ao inicializar dados padrão"); } } app.Run();