Files
TP-Fluent/BookHive/Program.cs
2026-03-10 08:35:40 +01:00

69 lines
2.1 KiB
C#

using AutoMapper;
using AutoMapper.EquivalencyExpression;
using BookHive;
using BookHive.MappingProfiles;
using BookHive.Repositories;
using FastEndpoints;
using FastEndpoints.Security;
using FastEndpoints.Swagger;
using Microsoft.Net.Http.Headers;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// On ajoute ici FastEndpoints, un framework REPR et Swagger aux services disponibles dans le projet
builder.Services
.AddAuthenticationJwtBearer(s => s.SigningKey = "ThisIsASuperSecretJwtKeyThatIsAtLeast32CharsLong")
.AddAuthorization()
.AddFastEndpoints()
.AddCors(options =>
{
options.AddDefaultPolicy(policyBuilder =>
{
policyBuilder
.WithOrigins("http://localhost:4200")
.WithMethods("GET", "POST", "PUT", "DELETE", "PATCH")
.AllowAnyHeader()
.WithExposedHeaders(HeaderNames.ContentDisposition);
});
})
.SwaggerDocument(options => { options.ShortSchemaNames = true; });
builder.Services.AddDbContext<BookHiveDbContext>();
builder.Services.AddScoped<AuthorRepository>();
builder.Services.AddScoped<BookRepository>();
builder.Services.AddScoped<MemberRepository>();
builder.Services.AddScoped<ReviewRepository>();
builder.Services.AddScoped<LoanRepository>();
builder.Services.AddHttpContextAccessor();
MapperConfiguration mappingConfig = new(mc =>
{
mc.AddCollectionMappers();
mc.AddProfile(new DtoToEntityMappings());
mc.AddProfile(new EntityToDtoMappings());
}, new LoggerFactory());
AutoMapper.IMapper mapper = mappingConfig.CreateMapper();
builder.Services.AddSingleton(mapper);
WebApplication app = builder.Build();
app.UseAuthentication()
.UseAuthorization()
.UseFastEndpoints(options =>
{
options.Endpoints.ShortNames = true;
options.Endpoints.RoutePrefix = "API";
})
.UseSwaggerGen();
// app.UseHttpsRedirection();
// app.UseCors();
app.Services.GetRequiredService<BookHiveDbContext>().Database.EnsureDeleted();
app.Services.GetRequiredService<BookHiveDbContext>().Database.EnsureCreated();
app.Run();