c#
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<VoithosDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("Database")));
builder.Services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<VoithosDbContext>()
.AddApiEndpoints();
builder.Services.AddAuthentication().AddBearerToken(IdentityConstants.BearerScheme);
builder.Services.AddAuthorizationBuilder();
builder.Services.AddHostedService<ProductFetchBackgroundService>();
builder.Services.AddScoped<ProductFetchService>();
builder.Services
.AddAutoMapperProfiles()
.AddServices()
.AddMediatR(typeof(HandlerBase))
.AddCors(options =>
{
options.AddPolicy("CorsPolicy", corsBuilder =>
{
corsBuilder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
})
.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapIdentityApi<User>();
app
.UseExceptionHandling()
.UseSwagger()
.UseSwaggerUI()
.UseRouting()
.UseCors("CorsPolicy")
.UseAuthentication()
.UseAuthorization()
.UseEndpoints(endpoints => endpoints.MapControllers());
app.Run();