using GameIdeas.Resources; using GameIdeas.WebAPI.Context; using GameIdeas.WebAPI.Services.Categories; using GameIdeas.WebAPI.Services.Games; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); var services = builder.Services; #if DEBUG LoadEnvironmentVariable("../../../../.env"); #else LoadEnvironmentVariable(".env"); #endif Action dbContextOptions = options => { options.UseNpgsql( GetConnectionString(), npgOption => { npgOption.CommandTimeout(60); npgOption.MigrationsAssembly("GameIdeas.WebAPI"); }); }; // Add services to the container. services.AddDbContext(dbContextOptions); services.AddSingleton(); services.AddSingleton(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); services.AddControllers(); // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi services.AddOpenApi(); services.AddCors(option => option.AddDefaultPolicy(policy => policy.WithOrigins("http://localhost:5172", "http://localhost:7060") .AllowAnyHeader() .WithMethods("GET", "POST", "PUT", "DELETE"))); var app = builder.Build(); await LoadTranslations(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.MapOpenApi(); } app.UseCors(); app.UseAuthorization(); app.MapControllers(); app.Run(); async Task LoadTranslations() { var filesDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Files"); var translationFiles = Directory.GetFiles(filesDirectory, "*.json"); var dictionary = new Dictionary(); foreach (var file in translationFiles) { var name = file.Split('.'); var culture = name[^2]; var content = await File.ReadAllTextAsync(file); dictionary.Add(culture, content); } app.Services.GetRequiredService().Initialize(dictionary); ResourcesKey.Initialize(app.Services.GetRequiredService()); } string GetConnectionString() { var host = Environment.GetEnvironmentVariable("DB_HOST"); var login = Environment.GetEnvironmentVariable("DB_USERNAME"); var pass = Environment.GetEnvironmentVariable("DB_PASSWORD"); var database = Environment.GetEnvironmentVariable("DB_DATABASE"); return $"Host={host};Username={login};Password={pass};Database={database}"; } static void LoadEnvironmentVariable(string filePath) { if (!File.Exists(filePath)) return; foreach (var line in File.ReadAllLines(filePath)) { var parts = line.Split( '=', StringSplitOptions.RemoveEmptyEntries); if (parts.Length != 2) continue; Environment.SetEnvironmentVariable(parts[0], parts[1]); } }