updated service extensions

master
Young 6 months ago
parent 1c969999ba
commit 083f7759f2

@ -8,10 +8,12 @@ using Infrastructure.Repository;
using Infrastructure.Repository.Mongo; using Infrastructure.Repository.Mongo;
using Infrastructure.Repository.Redis; using Infrastructure.Repository.Redis;
using Infrastructure.Seed; using Infrastructure.Seed;
using Mapster;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.OpenApi.Models;
using MongoDB.Driver; using MongoDB.Driver;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Converters; using Newtonsoft.Json.Converters;
@ -21,6 +23,7 @@ using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes; using Serilog.Sinks.SystemConsole.Themes;
using SqlSugar.Extensions; using SqlSugar.Extensions;
using StackExchange.Redis; using StackExchange.Redis;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace Infrastructure.Extensions; namespace Infrastructure.Extensions;
@ -45,7 +48,16 @@ public static class ServiceCollectionExtensions
.AddDefaultSqlSugar(configuration, builder.Environment) .AddDefaultSqlSugar(configuration, builder.Environment)
.AddDefaultMongoDb(configuration) .AddDefaultMongoDb(configuration)
.AddDefaultTokenContext() .AddDefaultTokenContext()
.AddDefaultDatabaseSeed(); .AddDefaultDatabaseSeed()
.AddDefaultSwaggerGen()
.AddEndpointsApiExplorer()
.AddRouting(options =>
{
options.LowercaseUrls = true;
options.LowercaseQueryStrings = true;
})
.AddMapster();
builder.Host.UseSerilog(Log.Logger, true);
return builder; return builder;
} }
@ -556,4 +568,37 @@ public static class ServiceCollectionExtensions
services.TryAddScoped<DatabaseSeed>(); services.TryAddScoped<DatabaseSeed>();
return services; return services;
} }
public static IServiceCollection AddDefaultSwaggerGen(this IServiceCollection services,
Action<SwaggerGenOptions>? 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<string>() }
});
configureOptions?.Invoke(options);
});
return services;
}
} }
Loading…
Cancel
Save