158 lines
4.6 KiB
C#
158 lines
4.6 KiB
C#
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<IMongoClient>(sp =>
|
|
{
|
|
var connectionString = builder.Configuration.GetConnectionString("MongoDB");
|
|
return new MongoClient(connectionString);
|
|
});
|
|
|
|
builder.Services.AddScoped(sp =>
|
|
{
|
|
var client = sp.GetRequiredService<IMongoClient>();
|
|
var databaseName = builder.Configuration.GetValue<string>("MongoDB:DatabaseName");
|
|
return client.GetDatabase(databaseName);
|
|
});
|
|
|
|
// Custom Services
|
|
builder.Services.AddScoped<ISiteConfigurationService, SiteConfigurationService>();
|
|
builder.Services.AddScoped<ILanguageService, LanguageService>();
|
|
builder.Services.AddScoped<ISeoService, SeoService>();
|
|
builder.Services.AddScoped<IConversionService, ConversionService>();
|
|
|
|
// Converter Services - Registrar todos os conversores disponíveis
|
|
builder.Services.AddScoped<TextCaseConverterService>();
|
|
builder.Services.AddScoped<CsvToJsonConverterService>();
|
|
builder.Services.AddScoped<ImageToTextConverterService>();
|
|
|
|
// Adicione aqui novos conversores conforme necessário:
|
|
// builder.Services.AddScoped<SeuNovoConverterService>();
|
|
|
|
// HttpClient for external calls
|
|
builder.Services.AddHttpClient<ConversionService>();
|
|
builder.Services.AddHttpClient<ImageToTextConverterService>();
|
|
|
|
// Response Compression
|
|
builder.Services.AddResponseCompression(options =>
|
|
{
|
|
options.EnableForHttps = true;
|
|
});
|
|
|
|
// Response Caching
|
|
builder.Services.AddResponseCaching();
|
|
|
|
// Memory Cache
|
|
builder.Services.AddMemoryCache();
|
|
|
|
// Logging
|
|
builder.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<ISiteConfigurationService>();
|
|
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
|
|
|
|
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(); |