OneConversorTemplate/OnlyOneAccessTemplate/Program.cs
Ricardo Carneiro b1d75213ab first commit
2025-05-30 23:48:28 -03:00

104 lines
2.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>();
// HttpClient for external calls
builder.Services.AddHttpClient<ConversionService>();
// Response Compression
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
});
// Response Caching
builder.Services.AddResponseCaching();
// Memory Cache
builder.Services.AddMemoryCache();
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: "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");
});
app.Run();