mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-14 17:54:42 +00:00
refactor(api-core): move service registration (#275)
This commit is contained in:
121
web/backend/API/API.Core/ServiceCollectionExtensions.cs
Normal file
121
web/backend/API/API.Core/ServiceCollectionExtensions.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using API.Core.Authentication;
|
||||
using Database.Connection.DependencyInjection;
|
||||
using Features.Auth.Controllers;
|
||||
using Features.Breweries.Controllers;
|
||||
using Features.Breweries.DependencyInjection;
|
||||
using Features.Emails.DependencyInjection;
|
||||
using Features.Emails.Services;
|
||||
using Features.ImageUploads.Commands.UploadPhoto;
|
||||
using Features.ImageUploads.DependencyInjection;
|
||||
using Features.Users.DependencyInjection;
|
||||
using FluentValidation;
|
||||
using FluentValidation.AspNetCore;
|
||||
using Infrastructure.FileUpload;
|
||||
using Infrastructure.Jwt;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Shared.Application.Behaviors;
|
||||
|
||||
namespace API.Core;
|
||||
|
||||
internal static class ServiceCollectionExtensions
|
||||
{
|
||||
extension(IServiceCollection services)
|
||||
{
|
||||
public IServiceCollection AddApiControllers() =>
|
||||
services
|
||||
.AddControllers(options => options.Filters.Add<GlobalExceptionFilter>())
|
||||
.AddApplicationPart(typeof(BreweryController).Assembly)
|
||||
.AddApplicationPart(typeof(AuthController).Assembly)
|
||||
.Services;
|
||||
|
||||
public IServiceCollection AddValidationAndMediatR()
|
||||
{
|
||||
services
|
||||
.AddOpenApi()
|
||||
.AddValidatorsFromAssemblyContaining<Program>()
|
||||
.AddValidatorsFromAssembly(typeof(BreweryController).Assembly)
|
||||
.AddValidatorsFromAssembly(typeof(AuthController).Assembly)
|
||||
.AddValidatorsFromAssembly(typeof(UploadPhotoCommand).Assembly)
|
||||
.AddFluentValidationAutoValidation()
|
||||
.AddMediatR(cfg =>
|
||||
{
|
||||
cfg.RegisterServicesFromAssemblyContaining<Program>();
|
||||
cfg.RegisterServicesFromAssembly(typeof(BreweryController).Assembly);
|
||||
cfg.RegisterServicesFromAssembly(typeof(AuthController).Assembly);
|
||||
cfg.RegisterServicesFromAssembly(typeof(UploadPhotoCommand).Assembly);
|
||||
cfg.RegisterServicesFromAssembly(typeof(IEmailDispatcher).Assembly);
|
||||
cfg.AddOpenBehavior(typeof(ValidationBehavior<,>));
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public IServiceCollection AddApplicationServices()
|
||||
{
|
||||
services
|
||||
.AddEndpointsApiExplorer()
|
||||
.AddSwaggerGen(options =>
|
||||
{
|
||||
options.AddSecurityDefinition(
|
||||
"Bearer",
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Name = "Authorization",
|
||||
Type = SecuritySchemeType.Http,
|
||||
Scheme = "Bearer",
|
||||
BearerFormat = "JWT",
|
||||
In = ParameterLocation.Header,
|
||||
Description = "Enter a JWT access token.",
|
||||
}
|
||||
);
|
||||
options.AddSecurityRequirement(
|
||||
new OpenApiSecurityRequirement
|
||||
{
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = "Bearer",
|
||||
},
|
||||
},
|
||||
[]
|
||||
},
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
services.AddHealthChecks();
|
||||
return services;
|
||||
}
|
||||
|
||||
public IServiceCollection AddFeatureModules() =>
|
||||
services
|
||||
.AddDatabaseConnection()
|
||||
.AddFeaturesBreweries()
|
||||
.AddFeaturesUsers()
|
||||
.AddFeaturesEmails()
|
||||
.AddFeaturesPhotoUpload();
|
||||
|
||||
public IServiceCollection AddCoreInfrastructure() =>
|
||||
services
|
||||
// ITokenInfrastructure is registered here because JwtAuthenticationHandler depends on it directly.
|
||||
.AddScoped<ITokenInfrastructure, JwtInfrastructure>()
|
||||
.AddSingleton<IFileStorageProvider, S3FileStorageProvider>()
|
||||
.AddScoped<GlobalExceptionFilter>();
|
||||
|
||||
public IServiceCollection AddJwtAuthentication()
|
||||
{
|
||||
services
|
||||
.AddAuthentication("JWT")
|
||||
.AddScheme<JwtAuthenticationOptions, JwtAuthenticationHandler>(
|
||||
"JWT",
|
||||
options => { }
|
||||
);
|
||||
|
||||
services.AddAuthorization();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user