using System.Net.Mime; using API.Validation; using DbServices; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; using Services; using StubContext; using System.Text; var builder = WebApplication.CreateBuilder(args); var config = builder.Configuration; // Add services to the container. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddControllers() .ConfigureApiBehaviorOptions(options => options.InvalidModelStateResponseFactory = context => new BadRequestObjectResult(context.ModelState) { ContentTypes = { MediaTypeNames.Application.Json } }); builder.Services.AddCors(options => options.AddPolicy("cors", policy => policy .AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod() )); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer( x => x.TokenValidationParameters = new TokenValidationParameters { IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["JWT:Key"]!)), ValidateLifetime = true, ValidateIssuer = false, ValidateAudience = false, ValidateIssuerSigningKey = true } ); builder.Services.AddAuthorization(options => options.AddPolicy(IdentityData.AdminUserPolicyName, p => p.RequireClaim(IdentityData.AdminUserClaimName))); var appContext = new StubAppContext(); appContext.Database.EnsureCreated(); builder.Services.AddScoped(_ => new DbUserService(appContext)); builder.Services.AddScoped(_ => new DbTeamService(appContext)); builder.Services.AddScoped(_ => new DbTacticService(appContext)); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseCors("cors"); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();