using System.Text; using Hospitality.Backend.Configuration; using Hospitality.Backend.Services; using Hospitality.Domain.Entities; using Hospitality.Infrastructure.Data; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddDbContext(options => options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); // Configure JWT settings builder.Services.Configure(builder.Configuration.GetSection("JwtSettings")); var jwtSettings = builder.Configuration.GetSection("JwtSettings").Get(); // Add Identity builder.Services.AddIdentity(options => { options.Password.RequireDigit = true; options.Password.RequireLowercase = true; options.Password.RequireUppercase = true; options.Password.RequireNonAlphanumeric = true; options.Password.RequiredLength = 8; }) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); // Add JWT Authentication builder.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 = jwtSettings!.Issuer, ValidAudience = jwtSettings.Audience, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.SecretKey)) }; }); // Register services builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); // Add CORS builder.Services.AddCors(options => options.AddPolicy("AllowFrontend", policy => { policy.SetIsOriginAllowed(origin => true) // Allow any origin .AllowAnyHeader() .AllowAnyMethod(); }); var app = builder.Build(); // Seed database with sample data using (var scope = app.Services.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService(); var userManager = scope.ServiceProvider.GetRequiredService>(); var roleManager = scope.ServiceProvider.GetRequiredService>(); // Apply migrations await context.Database.MigrateAsync(); await Hospitality.Backend.Data.DbSeeder.SeedAsync(context, userManager, roleManager); } // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } // app.UseHttpsRedirection(); // Disabled because Traefik handles SSL app.UseCors("AllowFrontend"); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();