Initial endpoint configuration with authentication
This commit is contained in:
39
src/WebApi/Endpoints/EndpointExtensions.cs
Normal file
39
src/WebApi/Endpoints/EndpointExtensions.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using Asp.Versioning.Builder;
|
||||
using Asp.Versioning.Conventions;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Vegasco.WebApi.Cars;
|
||||
using Vegasco.WebApi.Common;
|
||||
|
||||
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()
|
||||
.HasApiVersion(1.0)
|
||||
.Build();
|
||||
|
||||
RouteGroupBuilder versionedApis = builder.MapGroup("/v{apiVersion:apiVersion}")
|
||||
.WithApiVersionSet(apiVersionSet)
|
||||
.RequireAuthorization(Constants.Authorization.RequireAuthenticatedUserPolicy);
|
||||
|
||||
CreateCar.MapEndpoint(versionedApis);
|
||||
}
|
||||
}
|
||||
6
src/WebApi/Endpoints/IEndpoint.cs
Normal file
6
src/WebApi/Endpoints/IEndpoint.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Vegasco.WebApi.Endpoints;
|
||||
|
||||
public interface IEndpoint
|
||||
{
|
||||
void MapEndpoint(IEndpointRouteBuilder builder);
|
||||
}
|
||||
56
src/WebApi/Endpoints/OpenApi/ConfigureSwaggerGenOptions.cs
Normal file
56
src/WebApi/Endpoints/OpenApi/ConfigureSwaggerGenOptions.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
6
src/WebApi/Endpoints/OpenApi/SwaggerDocConstants.cs
Normal file
6
src/WebApi/Endpoints/OpenApi/SwaggerDocConstants.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Vegasco.WebApi.Endpoints.OpenApi;
|
||||
|
||||
public static class SwaggerDocConstants
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user