Update packages, use explicit type, use Microsoft OpenApi package
This commit is contained in:
@@ -11,6 +11,6 @@ public static class Constants
|
||||
{
|
||||
public const string ServiceName = "postgres";
|
||||
|
||||
public const string Name = "vegasco";
|
||||
public const string Name = "vegasco-database";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,4 +6,10 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Update="Nerdbank.GitVersioning">
|
||||
<Version>3.7.115</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using Vegasco.Server.AppHost.Shared;
|
||||
|
||||
var builder = DistributedApplication.CreateBuilder(args);
|
||||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(args);
|
||||
|
||||
var postgres = builder.AddPostgres(Constants.Database.ServiceName)
|
||||
IResourceBuilder<PostgresDatabaseResource> postgres = builder.AddPostgres(Constants.Database.ServiceName)
|
||||
.WithDataVolume()
|
||||
.AddDatabase(Constants.Database.Name);
|
||||
|
||||
|
||||
@@ -12,8 +12,11 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0" />
|
||||
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="9.0.0" />
|
||||
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.3.0" />
|
||||
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="9.3.0" />
|
||||
<PackageReference Update="Nerdbank.GitVersioning">
|
||||
<Version>3.7.115</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -72,7 +72,7 @@ public static class Extensions
|
||||
|
||||
private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
|
||||
{
|
||||
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
|
||||
bool useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
|
||||
|
||||
if (useOtlpExporter)
|
||||
{
|
||||
|
||||
@@ -10,13 +10,16 @@
|
||||
<ItemGroup>
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
|
||||
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
|
||||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
|
||||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.5.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="9.3.0" />
|
||||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.12.0" />
|
||||
<PackageReference Update="Nerdbank.GitVersioning">
|
||||
<Version>3.7.115</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -47,14 +47,14 @@ public sealed class UserAccessor
|
||||
|
||||
private string GetClaimValue(string claimType)
|
||||
{
|
||||
var httpContext = _httpContextAccessor.HttpContext;
|
||||
HttpContext? httpContext = _httpContextAccessor.HttpContext;
|
||||
|
||||
if (httpContext is null)
|
||||
{
|
||||
ThrowForMissingHttpContext();
|
||||
}
|
||||
|
||||
var claimValue = httpContext.User.FindFirstValue(claimType);
|
||||
string? claimValue = httpContext.User.FindFirstValue(claimType);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(claimValue))
|
||||
{
|
||||
|
||||
@@ -42,9 +42,9 @@ public static class CreateCar
|
||||
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
||||
}
|
||||
|
||||
var userId = userAccessor.GetUserId();
|
||||
string userId = userAccessor.GetUserId();
|
||||
|
||||
var user = await dbContext.Users.FindAsync([userId], cancellationToken: cancellationToken);
|
||||
User? user = await dbContext.Users.FindAsync([userId], cancellationToken: cancellationToken);
|
||||
if (user is null)
|
||||
{
|
||||
user = new User
|
||||
|
||||
@@ -16,7 +16,7 @@ public static class DeleteCar
|
||||
ApplicationDbContext dbContext,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var car = await dbContext.Cars.FindAsync([new CarId(id)], cancellationToken: cancellationToken);
|
||||
Car? car = await dbContext.Cars.FindAsync([new CarId(id)], cancellationToken: cancellationToken);
|
||||
|
||||
if (car is null)
|
||||
{
|
||||
|
||||
@@ -3,8 +3,6 @@ using FluentValidation;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Vegasco.WebApi.Authentication;
|
||||
using Vegasco.WebApi.Endpoints;
|
||||
using Vegasco.WebApi.Endpoints.OpenApi;
|
||||
using Vegasco.WebApi.Persistence;
|
||||
|
||||
namespace Vegasco.WebApi.Common;
|
||||
@@ -14,14 +12,12 @@ public static class DependencyInjectionExtensions
|
||||
/// <summary>
|
||||
/// Adds all the WebApi related services to the Dependency Injection container.
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="configuration"></param>
|
||||
/// <param name="environment"></param>
|
||||
/// <param name="builder"></param>
|
||||
public static void AddWebApiServices(this IHostApplicationBuilder builder)
|
||||
{
|
||||
builder.Services
|
||||
.AddMiscellaneousServices()
|
||||
.AddOpenApi()
|
||||
.AddCustomOpenApi()
|
||||
.AddApiVersioning()
|
||||
.AddAuthenticationAndAuthorization(builder.Environment);
|
||||
|
||||
@@ -38,7 +34,6 @@ public static class DependencyInjectionExtensions
|
||||
], ServiceLifetime.Singleton);
|
||||
|
||||
services.AddHealthChecks();
|
||||
services.AddEndpointsFromAssemblyContaining<IWebApiMarker>();
|
||||
|
||||
services.AddHttpContextAccessor();
|
||||
|
||||
@@ -47,32 +42,30 @@ public static class DependencyInjectionExtensions
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IServiceCollection AddOpenApi(this IServiceCollection services)
|
||||
private static IServiceCollection AddCustomOpenApi(this IServiceCollection services)
|
||||
{
|
||||
services.ConfigureOptions<ConfigureSwaggerGenOptions>();
|
||||
|
||||
services.AddEndpointsApiExplorer();
|
||||
services.AddSwaggerGen(o =>
|
||||
services.AddOpenApi(o =>
|
||||
{
|
||||
o.CustomSchemaIds(type =>
|
||||
o.CreateSchemaReferenceId = jsonTypeInfo =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(type.FullName))
|
||||
if (string.IsNullOrEmpty(jsonTypeInfo.Type.FullName))
|
||||
{
|
||||
return type.Name;
|
||||
return jsonTypeInfo.Type.Name;
|
||||
}
|
||||
|
||||
var fullClassName = type.FullName;
|
||||
string? fullClassName = jsonTypeInfo.Type.FullName;
|
||||
|
||||
if (!string.IsNullOrEmpty(type.Namespace))
|
||||
if (!string.IsNullOrEmpty(jsonTypeInfo.Type.Namespace))
|
||||
{
|
||||
fullClassName = fullClassName
|
||||
.Replace(type.Namespace, "")
|
||||
.Replace(jsonTypeInfo.Type.Namespace, "")
|
||||
.TrimStart('.');
|
||||
}
|
||||
|
||||
fullClassName = fullClassName.Replace('+', '_');
|
||||
return fullClassName;
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
return services;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Vegasco.WebApi.Common;
|
||||
@@ -25,7 +26,7 @@ public class FluentValidationOptions<TOptions> : IValidateOptions<TOptions>
|
||||
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
var failedValidations = _validators.ValidateAllAsync(options).Result;
|
||||
List<ValidationResult> failedValidations = _validators.ValidateAllAsync(options).Result;
|
||||
if (failedValidations.Count == 0)
|
||||
{
|
||||
return ValidateOptionsResult.Success;
|
||||
|
||||
@@ -45,18 +45,7 @@ internal static class StartupExtensions
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(o =>
|
||||
{
|
||||
// Create a Swagger endpoint for each API version
|
||||
IReadOnlyList<ApiVersionDescription> apiVersions = app.DescribeApiVersions();
|
||||
foreach (ApiVersionDescription apiVersionDescription in apiVersions)
|
||||
{
|
||||
string url = $"/swagger/{apiVersionDescription.GroupName}/swagger.json";
|
||||
string name = apiVersionDescription.GroupName.ToUpperInvariant();
|
||||
o.SwaggerEndpoint(url, name);
|
||||
}
|
||||
});
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
return app;
|
||||
|
||||
@@ -15,7 +15,7 @@ public static class ValidatorExtensions
|
||||
/// <returns>The failed validation results.</returns>
|
||||
public static async Task<List<ValidationResult>> ValidateAllAsync<T>(this IEnumerable<IValidator<T>> validators, T instance, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var validationTasks = validators
|
||||
List<Task<ValidationResult>> validationTasks = validators
|
||||
.Select(validator => validator.ValidateAsync(instance, cancellationToken))
|
||||
.ToList();
|
||||
|
||||
@@ -34,7 +34,7 @@ public static class ValidatorExtensions
|
||||
// Use a hash set to avoid duplicate error messages.
|
||||
Dictionary<string, HashSet<string>> combinedErrors = [];
|
||||
|
||||
foreach (var error in validationResults.SelectMany(x => x.Errors))
|
||||
foreach (ValidationFailure? error in validationResults.SelectMany(x => x.Errors))
|
||||
{
|
||||
if (!combinedErrors.TryGetValue(error.PropertyName, out HashSet<string>? value))
|
||||
{
|
||||
@@ -54,7 +54,7 @@ public static class ValidatorExtensions
|
||||
{
|
||||
builder.Services.AddTransient<IValidateOptions<T>>(serviceProvider =>
|
||||
{
|
||||
var validators = serviceProvider.GetServices<IValidator<T>>() ?? [];
|
||||
IEnumerable<IValidator<T>> validators = serviceProvider.GetServices<IValidator<T>>() ?? [];
|
||||
return new FluentValidationOptions<T>(builder.Name, validators);
|
||||
});
|
||||
return builder;
|
||||
|
||||
@@ -10,22 +10,6 @@ namespace Vegasco.WebApi.Endpoints;
|
||||
|
||||
public static class EndpointExtensions
|
||||
{
|
||||
public static IServiceCollection AddEndpointsFromAssemblyContaining<T>(this IServiceCollection services)
|
||||
{
|
||||
var assembly = typeof(T).Assembly;
|
||||
|
||||
ServiceDescriptor[] serviceDescriptors = assembly
|
||||
.DefinedTypes
|
||||
.Where(type => type is { IsAbstract: false, IsInterface: false } &&
|
||||
type.IsAssignableTo(typeof(IEndpoint)))
|
||||
.Select(type => ServiceDescriptor.Transient(typeof(IEndpoint), type))
|
||||
.ToArray();
|
||||
|
||||
services.TryAddEnumerable(serviceDescriptors);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static void MapEndpoints(this IEndpointRouteBuilder builder)
|
||||
{
|
||||
ApiVersionSet apiVersionSet = builder.NewApiVersionSet()
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Vegasco.WebApi.Endpoints;
|
||||
|
||||
public interface IEndpoint
|
||||
{
|
||||
void MapEndpoint(IEndpointRouteBuilder builder);
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
using Asp.Versioning.ApiExplorer;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace Vegasco.WebApi.Endpoints.OpenApi;
|
||||
|
||||
/// <summary>
|
||||
/// Registers each api version as its own swagger document.
|
||||
/// </summary>
|
||||
/// <param name="versionDescriptionProvider"></param>
|
||||
public class ConfigureSwaggerGenOptions(
|
||||
IApiVersionDescriptionProvider versionDescriptionProvider)
|
||||
: IConfigureNamedOptions<SwaggerGenOptions>
|
||||
{
|
||||
private readonly IApiVersionDescriptionProvider _versionDescriptionProvider = versionDescriptionProvider;
|
||||
|
||||
public void Configure(SwaggerGenOptions options)
|
||||
{
|
||||
foreach (ApiVersionDescription description in _versionDescriptionProvider.ApiVersionDescriptions)
|
||||
{
|
||||
OpenApiSecurityScheme securityScheme = new()
|
||||
{
|
||||
Name = "Bearer",
|
||||
In = ParameterLocation.Header,
|
||||
Type = SecuritySchemeType.Http,
|
||||
Scheme = "bearer",
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Id = IdentityConstants.BearerScheme,
|
||||
Type = ReferenceType.SecurityScheme
|
||||
}
|
||||
};
|
||||
options.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme);
|
||||
|
||||
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||
{
|
||||
{ securityScheme, Array.Empty<string>() }
|
||||
});
|
||||
|
||||
OpenApiInfo openApiInfo = new()
|
||||
{
|
||||
Title = "Vegasco API",
|
||||
Version = description.ApiVersion.ToString()
|
||||
};
|
||||
|
||||
options.SwaggerDoc(description.GroupName, openApiInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public void Configure(string? name, SwaggerGenOptions options)
|
||||
{
|
||||
Configure(options);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Vegasco.WebApi.Endpoints.OpenApi;
|
||||
|
||||
public static class SwaggerDocConstants
|
||||
{
|
||||
|
||||
}
|
||||
@@ -2,18 +2,14 @@
|
||||
|
||||
namespace Vegasco.WebApi.Persistence;
|
||||
|
||||
public class ApplyMigrationsService : IHostedService
|
||||
public class ApplyMigrationsService(ILogger<ApplyMigrationsService> logger, IServiceScopeFactory scopeFactory)
|
||||
: IHostedService
|
||||
{
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
|
||||
public ApplyMigrationsService(IServiceScopeFactory scopeFactory)
|
||||
{
|
||||
_scopeFactory = scopeFactory;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
using IServiceScope scope = _scopeFactory.CreateScope();
|
||||
logger.LogInformation("Starting migrations");
|
||||
|
||||
using IServiceScope scope = scopeFactory.CreateScope();
|
||||
await using var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
await dbContext.Database.MigrateAsync(cancellationToken);
|
||||
}
|
||||
|
||||
@@ -13,25 +13,24 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
|
||||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
|
||||
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.0" />
|
||||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
|
||||
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.3.0" />
|
||||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="12.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.2" />
|
||||
<PackageReference Include="OpenTelemetry" Version="1.10.0" />
|
||||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />
|
||||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.10.1" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.10.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||
<PackageReference Include="OpenTelemetry" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
|
||||
<PackageReference Include="StronglyTypedId" Version="1.0.0-beta08" PrivateAssets="all" ExcludeAssets="runtime" />
|
||||
<PackageReference Include="StronglyTypedId.Templates" Version="1.0.0-beta08" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -40,7 +39,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Update="Nerdbank.GitVersioning" Version="3.7.112" />
|
||||
<PackageReference Update="Nerdbank.GitVersioning" Version="3.7.115" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user