2024-08-17 16:38:40 +02:00
|
|
|
|
using Asp.Versioning;
|
|
|
|
|
|
using FluentValidation;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using Vegasco.WebApi.Authentication;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
using Vegasco.WebApi.Persistence;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Vegasco.WebApi.Common;
|
|
|
|
|
|
|
|
|
|
|
|
public static class DependencyInjectionExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Adds all the WebApi related services to the Dependency Injection container.
|
|
|
|
|
|
/// </summary>
|
2025-06-12 17:43:22 +02:00
|
|
|
|
/// <param name="builder"></param>
|
2024-12-28 17:01:18 +01:00
|
|
|
|
public static void AddWebApiServices(this IHostApplicationBuilder builder)
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
2024-12-28 17:01:18 +01:00
|
|
|
|
builder.Services
|
2024-08-17 16:38:40 +02:00
|
|
|
|
.AddMiscellaneousServices()
|
2025-06-12 17:43:22 +02:00
|
|
|
|
.AddCustomOpenApi()
|
2024-08-17 16:38:40 +02:00
|
|
|
|
.AddApiVersioning()
|
2024-12-28 17:01:18 +01:00
|
|
|
|
.AddAuthenticationAndAuthorization(builder.Environment);
|
|
|
|
|
|
|
|
|
|
|
|
builder.AddDbContext();
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IServiceCollection AddMiscellaneousServices(this IServiceCollection services)
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddResponseCompression();
|
|
|
|
|
|
|
|
|
|
|
|
services.AddValidatorsFromAssemblies(
|
|
|
|
|
|
[
|
|
|
|
|
|
typeof(IWebApiMarker).Assembly
|
|
|
|
|
|
], ServiceLifetime.Singleton);
|
|
|
|
|
|
|
|
|
|
|
|
services.AddHealthChecks();
|
|
|
|
|
|
|
2024-08-17 16:38:40 +02:00
|
|
|
|
services.AddHttpContextAccessor();
|
|
|
|
|
|
|
2024-08-24 13:43:43 +02:00
|
|
|
|
services.AddHostedService<ApplyMigrationsService>();
|
|
|
|
|
|
|
2024-08-17 16:38:40 +02:00
|
|
|
|
return services;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 17:43:22 +02:00
|
|
|
|
private static IServiceCollection AddCustomOpenApi(this IServiceCollection services)
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
services.AddEndpointsApiExplorer();
|
2025-06-12 17:43:22 +02:00
|
|
|
|
services.AddOpenApi(o =>
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
2025-06-12 17:43:22 +02:00
|
|
|
|
o.CreateSchemaReferenceId = jsonTypeInfo =>
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
2025-06-12 17:43:22 +02:00
|
|
|
|
if (string.IsNullOrEmpty(jsonTypeInfo.Type.FullName))
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
2025-06-12 17:43:22 +02:00
|
|
|
|
return jsonTypeInfo.Type.Name;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 17:43:22 +02:00
|
|
|
|
string? fullClassName = jsonTypeInfo.Type.FullName;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
2025-06-12 17:43:22 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(jsonTypeInfo.Type.Namespace))
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
fullClassName = fullClassName
|
2025-06-12 17:43:22 +02:00
|
|
|
|
.Replace(jsonTypeInfo.Type.Namespace, "")
|
2024-08-17 16:38:40 +02:00
|
|
|
|
.TrimStart('.');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-25 13:14:32 +02:00
|
|
|
|
fullClassName = fullClassName.Replace('+', '_');
|
2024-08-17 16:38:40 +02:00
|
|
|
|
return fullClassName;
|
2025-06-12 17:43:22 +02:00
|
|
|
|
};
|
2024-08-17 16:38:40 +02:00
|
|
|
|
});
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IServiceCollection AddApiVersioning(this IServiceCollection services)
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddApiVersioning(o =>
|
|
|
|
|
|
{
|
|
|
|
|
|
o.DefaultApiVersion = new ApiVersion(1);
|
|
|
|
|
|
o.ApiVersionReader = new UrlSegmentApiVersionReader();
|
|
|
|
|
|
o.ReportApiVersions = true;
|
|
|
|
|
|
})
|
|
|
|
|
|
.AddApiExplorer(o =>
|
|
|
|
|
|
{
|
|
|
|
|
|
o.GroupNameFormat = "'v'V";
|
|
|
|
|
|
o.SubstituteApiVersionInUrl = true;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-17 16:38:41 +02:00
|
|
|
|
private static IServiceCollection AddAuthenticationAndAuthorization(this IServiceCollection services, IHostEnvironment environment)
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
services.AddOptions<JwtOptions>()
|
|
|
|
|
|
.BindConfiguration(JwtOptions.SectionName)
|
|
|
|
|
|
.ValidateFluently()
|
|
|
|
|
|
.ValidateOnStart();
|
|
|
|
|
|
|
|
|
|
|
|
var jwtOptions = services.BuildServiceProvider().GetRequiredService<IOptions<JwtOptions>>();
|
|
|
|
|
|
|
|
|
|
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
|
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, o =>
|
|
|
|
|
|
{
|
2024-08-17 16:38:40 +02:00
|
|
|
|
o.MetadataAddress = jwtOptions.Value.MetadataUrl;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
|
2024-08-17 16:38:40 +02:00
|
|
|
|
o.TokenValidationParameters.ValidAudience = jwtOptions.Value.ValidAudience;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
o.TokenValidationParameters.ValidateAudience = true;
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(jwtOptions.Value.NameClaimType))
|
|
|
|
|
|
{
|
|
|
|
|
|
o.TokenValidationParameters.NameClaimType = jwtOptions.Value.NameClaimType;
|
|
|
|
|
|
}
|
2024-08-17 16:38:41 +02:00
|
|
|
|
|
|
|
|
|
|
o.RequireHttpsMetadata = !jwtOptions.Value.AllowHttpMetadataUrl && !environment.IsDevelopment();
|
2024-08-17 16:38:40 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
services.AddAuthorizationBuilder()
|
|
|
|
|
|
.AddPolicy(Constants.Authorization.RequireAuthenticatedUserPolicy, p => p
|
|
|
|
|
|
.RequireAuthenticatedUser()
|
|
|
|
|
|
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme));
|
|
|
|
|
|
|
2024-08-17 16:38:40 +02:00
|
|
|
|
services.AddScoped<UserAccessor>();
|
|
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-28 17:01:18 +01:00
|
|
|
|
private static IHostApplicationBuilder AddDbContext(this IHostApplicationBuilder builder)
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
2024-12-28 17:01:18 +01:00
|
|
|
|
builder.AddNpgsqlDbContext<ApplicationDbContext>(Server.AppHost.Shared.Constants.Database.Name);
|
|
|
|
|
|
return builder;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|