178 lines
6.4 KiB
C#
178 lines
6.4 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.OpenApi.Models;
|
|
using NoFurion.SqlSugar;
|
|
using SqlSugar;
|
|
using StackExchange.Redis;
|
|
using Volo.Abp;
|
|
using Volo.Abp.AspNetCore.Authentication.JwtBearer;
|
|
using Volo.Abp.AspNetCore.Serilog;
|
|
using Volo.Abp.Autofac;
|
|
using Volo.Abp.Caching;
|
|
using Volo.Abp.Caching.StackExchangeRedis;
|
|
using Volo.Abp.Modularity;
|
|
using Volo.Abp.Swashbuckle;
|
|
using Volo.Abp.VirtualFileSystem;
|
|
|
|
namespace Dolphin.ExamPictureCut;
|
|
|
|
[DependsOn(
|
|
typeof(DolphinExamPictureCutHttpApiModule),
|
|
typeof(AbpAutofacModule),
|
|
typeof(AbpAspNetCoreSerilogModule),
|
|
typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
|
|
typeof(AbpCachingStackExchangeRedisModule),
|
|
typeof(AbpSwashbuckleModule)
|
|
)]
|
|
public class DolphinExamPictureCutHttpApiHostModule : AbpModule
|
|
{
|
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
|
{
|
|
var configuration = context.Services.GetConfiguration();
|
|
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
|
|
|
ConfigureSqlSugar(context, configuration);
|
|
ConfigureAuthentication(context, configuration);
|
|
ConfigureCache(configuration);
|
|
ConfigureCors(context, configuration);
|
|
ConfigureSwaggerServices(context, configuration);
|
|
}
|
|
|
|
private void ConfigureSqlSugar(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
var config = new SqlSugarConfig();
|
|
context.Services.AddSingleton<ISqlSugarClient>(s =>
|
|
{
|
|
var scope = new SqlSugarScope(
|
|
new List<ConnectionConfig>() {
|
|
new ConnectionConfig()
|
|
{
|
|
ConfigId = DbConsts.basic,
|
|
DbType = DbType.PostgreSQL,
|
|
ConnectionString = configuration.GetConnectionString(DbConsts.basic),
|
|
IsAutoCloseConnection = true,
|
|
ConfigureExternalServices = config.ExtService,
|
|
},
|
|
},
|
|
db =>
|
|
{
|
|
db.QueryFilter.AddTableFilter<ISoftDelete>(s => s.IsDeleted == false);
|
|
|
|
db.Aop.DataExecuting = config.DataExecuting(context.Services);
|
|
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
{
|
|
Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
|
|
// Console.WriteLine(config.ParameterFormat(sql, pars)); // sql 参数格式化
|
|
};
|
|
}
|
|
);
|
|
return scope;
|
|
});
|
|
}
|
|
|
|
private void ConfigureCache(IConfiguration configuration)
|
|
{
|
|
//Configure<AbpDistributedCacheOptions>(options => { options.KeyPrefix = "Dolphin:"; });
|
|
}
|
|
|
|
private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.Authority = configuration["AuthServer:Authority"];
|
|
options.RequireHttpsMetadata = Convert.ToBoolean(configuration["AuthServer:RequireHttpsMetadata"]);
|
|
options.Audience = "Dolphin";
|
|
});
|
|
}
|
|
|
|
private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
context.Services.AddAbpSwaggerGenWithOAuth(
|
|
configuration["AuthServer:Authority"],
|
|
new Dictionary<string, string>
|
|
{
|
|
{"Dolphin", "Dolphin API"}
|
|
},
|
|
options =>
|
|
{
|
|
options.HideAbpEndpoints(); // 隐藏 Abp 相关的 endpoints
|
|
options.UseInlineDefinitionsForEnums();
|
|
|
|
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Dolphin API", Version = "v1" });
|
|
options.DocInclusionPredicate((docName, description) => true);
|
|
options.CustomSchemaIds(type => type.FullName);
|
|
|
|
// add summaries to swagger
|
|
var directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
|
|
var files = directory.GetFiles("Dolphin.*.xml");
|
|
foreach (var file in files)
|
|
{
|
|
options.IncludeXmlComments(file.FullName, true);
|
|
}
|
|
|
|
// 修改 Schema 默认的时间格式
|
|
options.MapType<DateTime>(() => new OpenApiSchema { Type = "string", Example = new Microsoft.OpenApi.Any.OpenApiString("2000-01-01 00:00:00") });
|
|
});
|
|
}
|
|
|
|
private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
context.Services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy(builder =>
|
|
{
|
|
builder
|
|
.WithOrigins(configuration["App:CorsOrigins"]?
|
|
.Split(",", StringSplitOptions.RemoveEmptyEntries)
|
|
.Select(o => o.RemovePostFix("/"))
|
|
.ToArray() ?? Array.Empty<string>())
|
|
.WithAbpExposedHeaders()
|
|
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
}
|
|
|
|
public override void OnApplicationInitialization(ApplicationInitializationContext context)
|
|
{
|
|
var app = context.GetApplicationBuilder();
|
|
var env = context.GetEnvironment();
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
|
|
app.UseAbpRequestLocalization();
|
|
app.UseCorrelationId();
|
|
app.UseStaticFiles();
|
|
app.UseRouting();
|
|
app.UseCors();
|
|
app.UseAuthentication();
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.UseSwagger();
|
|
app.UseAbpSwaggerUI(options =>
|
|
{
|
|
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Dolphin API");
|
|
options.DefaultModelsExpandDepth(0);
|
|
options.DisplayRequestDuration();
|
|
|
|
var configuration = context.GetConfiguration();
|
|
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
|
|
options.OAuthScopes("Dolphin");
|
|
});
|
|
|
|
app.UseAuditing();
|
|
app.UseUnitOfWork();
|
|
app.UseConfiguredEndpoints();
|
|
}
|
|
}
|
|
|