2024-08-17 16:38:40 +02:00
|
|
|
|
using Asp.Versioning;
|
|
|
|
|
|
using FluentValidation;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-08-17 16:38:40 +02:00
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using OpenTelemetry.Trace;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using Vegasco.WebApi.Authentication;
|
|
|
|
|
|
using Vegasco.WebApi.Endpoints;
|
|
|
|
|
|
using Vegasco.WebApi.Endpoints.OpenApi;
|
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>
|
|
|
|
|
|
/// <param name="services"></param>
|
2024-08-17 16:38:41 +02:00
|
|
|
|
/// <param name="configuration"></param>
|
|
|
|
|
|
/// <param name="environment"></param>
|
|
|
|
|
|
public static void AddWebApiServices(this IServiceCollection services, IConfiguration configuration, IHostEnvironment environment)
|
2024-08-17 16:38:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
services
|
|
|
|
|
|
.AddMiscellaneousServices()
|
|
|
|
|
|
.AddOpenApi()
|
|
|
|
|
|
.AddApiVersioning()
|
|
|
|
|
|
.AddOtel()
|
2024-08-17 16:38:41 +02:00
|
|
|
|
.AddAuthenticationAndAuthorization(environment)
|
2024-08-17 16:38:40 +02:00
|
|
|
|
.AddDbContext(configuration);
|
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();
|
|
|
|
|
|
services.AddEndpointsFromAssemblyContaining<IWebApiMarker>();
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IServiceCollection AddOpenApi(this IServiceCollection services)
|
|
|
|
|
|
{
|
|
|
|
|
|
services.ConfigureOptions<ConfigureSwaggerGenOptions>();
|
|
|
|
|
|
|
|
|
|
|
|
services.AddEndpointsApiExplorer();
|
2024-08-17 16:38:40 +02:00
|
|
|
|
services.AddSwaggerGen(o =>
|
|
|
|
|
|
{
|
|
|
|
|
|
o.CustomSchemaIds(type =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(type.FullName))
|
|
|
|
|
|
{
|
|
|
|
|
|
return type.Name;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var fullClassName = type.FullName;
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(type.Namespace))
|
|
|
|
|
|
{
|
|
|
|
|
|
fullClassName = fullClassName
|
|
|
|
|
|
.Replace(type.Namespace, "")
|
|
|
|
|
|
.TrimStart('.');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-25 13:14:32 +02:00
|
|
|
|
fullClassName = fullClassName.Replace('+', '_');
|
2024-08-17 16:38:40 +02:00
|
|
|
|
return fullClassName;
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IServiceCollection AddOtel(this IServiceCollection services)
|
|
|
|
|
|
{
|
|
|
|
|
|
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
|
|
|
|
|
|
|
|
|
|
|
|
ActivitySource activitySource = new(Constants.AppOtelName);
|
|
|
|
|
|
services.AddSingleton(activitySource);
|
|
|
|
|
|
|
|
|
|
|
|
services.AddOpenTelemetry()
|
|
|
|
|
|
.WithTracing(t =>
|
|
|
|
|
|
{
|
|
|
|
|
|
t.AddAspNetCoreInstrumentation()
|
|
|
|
|
|
.AddHttpClientInstrumentation()
|
|
|
|
|
|
.AddOtlpExporter()
|
|
|
|
|
|
.AddSource(activitySource.Name);
|
|
|
|
|
|
})
|
|
|
|
|
|
.WithMetrics();
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IServiceCollection AddDbContext(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
|
{
|
|
|
|
|
|
services.AddDbContext<ApplicationDbContext>(o =>
|
|
|
|
|
|
{
|
|
|
|
|
|
o.UseNpgsql(configuration.GetConnectionString("Database"), c =>
|
|
|
|
|
|
{
|
|
|
|
|
|
c.EnableRetryOnFailure();
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-08-17 16:38:40 +02:00
|
|
|
|
return services;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|