using GameIdeas.Resources; using GameIdeas.Shared.Model; using GameIdeas.WebAPI.Context; using GameIdeas.WebAPI.Services.Categories; using GameIdeas.WebAPI.Services.Games; using GameIdeas.WebAPI.Services.Users; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using System.Text; 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.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); var jwtKey = Environment.GetEnvironmentVariable("JWT_KEY") ?? throw new ArgumentNullException(message: "Invalid key for JWT token", null); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = Environment.GetEnvironmentVariable("JWT_ISSUER"), ValidAudience = Environment.GetEnvironmentVariable("JWT_AUDIENCE"), IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)) }; }); services.Configure(options => { // Default Password settings. options.Password.RequireDigit = false; options.Password.RequireLowercase = false; options.Password.RequireNonAlphanumeric = false; options.Password.RequireUppercase = false; options.Password.RequiredLength = 6; options.Password.RequiredUniqueChars = 1; }); services.AddAuthorization(); services.AddSingleton(); services.AddSingleton(); services.AddScoped(); services.AddScoped(); 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.AllowAnyOrigin() .AllowAnyHeader() .WithMethods("GET", "POST", "PUT", "DELETE"))); var app = builder.Build(); using (var scope = app.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); await db.Database.MigrateAsync(); } 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]); } }