using System.Reflection; using AutoMapper; using Events.Auth; using Events.WebAPI; using Events.WebAPI.Contract.Validation.Sport; using Events.WebAPI.Contract.Validation; using Events.WebAPI.Handlers.EF.Mappings; using Events.WebAPI.Handlers.EF.QueryHandlers; using Events.WebAPI.Handlers.EF.Data.Postgres; using Events.WebAPI.Util.Extensions; using Events.WebAPI.Util.Startup; using Events.WebAPI.Util.Swagger; using Events.WebAPI.Util.Validation; using FluentValidation; using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi; using Sieve.Models; using Sieve.Services; var builder = WebApplication.CreateBuilder(args); builder.Services .AddControllers(options => options.AddJsonPatchSupport()) .AddJsonOptions(configure => configure.JsonSerializerOptions.PropertyNamingPolicy = null); builder.Services.AddDbContext(options => options.UseNpgsql(builder.Configuration.GetConnectionString("EventsPostgres"))); builder.Services.Configure(builder.Configuration.GetSection("Sieve")); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>)); builder.Services.AddValidatorsFromAssemblyContaining(typeof(AddSportValidator)); builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(CountriesLookupQueryHandler).Assembly)); builder.Services.SetupMassTransit(builder.Configuration); builder.Services.SetupAuthenticationAndAuthorization( builder.Configuration["Auth:Authority"] ?? throw new InvalidOperationException("Missing configuration value Auth:Authority."), builder.Configuration["Auth:Audience"] ?? throw new InvalidOperationException("Missing configuration value Auth:Audience.")); #region AutoMapper settings Action mapperConfigAction = (serviceProvider, cfg) => { cfg.ConstructServicesUsing(serviceProvider.GetService); }; builder.Services.AddAutoMapper(mapperConfigAction, typeof(EFMappingProfile)); //assemblies containing mapping profiles #endregion builder.Services.AddSwaggerGen(c => { c.SwaggerDoc(Constants.ApiVersion, new OpenApiInfo { Title = "Events API", Version = Constants.ApiVersion }); var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); if (File.Exists(xmlPath)) c.IncludeXmlComments(xmlPath); c.AddBearerTokenScheme(); }); var app = builder.Build(); app.UseSwagger(); app.UseSwaggerUI(c => { c.RoutePrefix = "docs"; c.DocumentTitle = "Events WebApi"; c.SwaggerEndpoint($"../swagger/{Constants.ApiVersion}/swagger.json", "Events WebAPI"); }); app.UseRouting(); app.UseCors(builder => { builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .WithExposedHeaders("Token-Expired", "Content-Disposition"); }); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();