diff --git a/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs b/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs index 5ae97e4..885e950 100644 --- a/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs +++ b/src/Infrastructure/Extensions/ServiceCollectionExtensions.cs @@ -8,10 +8,12 @@ using Infrastructure.Repository; using Infrastructure.Repository.Mongo; using Infrastructure.Repository.Redis; using Infrastructure.Seed; +using Mapster; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.OpenApi.Models; using MongoDB.Driver; using Newtonsoft.Json; using Newtonsoft.Json.Converters; @@ -21,6 +23,7 @@ using Serilog.Events; using Serilog.Sinks.SystemConsole.Themes; using SqlSugar.Extensions; using StackExchange.Redis; +using Swashbuckle.AspNetCore.SwaggerGen; namespace Infrastructure.Extensions; @@ -45,7 +48,16 @@ public static class ServiceCollectionExtensions .AddDefaultSqlSugar(configuration, builder.Environment) .AddDefaultMongoDb(configuration) .AddDefaultTokenContext() - .AddDefaultDatabaseSeed(); + .AddDefaultDatabaseSeed() + .AddDefaultSwaggerGen() + .AddEndpointsApiExplorer() + .AddRouting(options => + { + options.LowercaseUrls = true; + options.LowercaseQueryStrings = true; + }) + .AddMapster(); + builder.Host.UseSerilog(Log.Logger, true); return builder; } @@ -556,4 +568,37 @@ public static class ServiceCollectionExtensions services.TryAddScoped(); return services; } + + public static IServiceCollection AddDefaultSwaggerGen(this IServiceCollection services, + Action? configureOptions = null) + { + ArgumentNullException.ThrowIfNull(services, nameof(services)); + + services.AddSwaggerGen(options => + { + var jwtSecurityScheme = new OpenApiSecurityScheme + { + BearerFormat = "JWT", + Name = "JWT Authentication", + In = ParameterLocation.Header, + Type = SecuritySchemeType.Http, + Scheme = JwtBearerDefaults.AuthenticationScheme, + Description = "Put **_ONLY_** your JWT Bearer token on textbox below!", + + Reference = new OpenApiReference + { + Id = JwtBearerDefaults.AuthenticationScheme, + Type = ReferenceType.SecurityScheme + } + }; + + options.AddSecurityDefinition(jwtSecurityScheme.Reference.Id, jwtSecurityScheme); + options.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { jwtSecurityScheme, Array.Empty() } + }); + configureOptions?.Invoke(options); + }); + return services; + } } \ No newline at end of file