You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
485 lines
17 KiB
485 lines
17 KiB
using DotNetCore.CAP;
|
|
using LINGYUN.Abp.ExceptionHandling;
|
|
using LINGYUN.Abp.ExceptionHandling.Emailing;
|
|
using LINGYUN.Abp.Localization.CultureMap;
|
|
using LINGYUN.Abp.Serilog.Enrichers.Application;
|
|
using LINGYUN.Abp.Serilog.Enrichers.UniqueId;
|
|
using LINGYUN.Abp.Wrapper;
|
|
using LINGYUN.Platform.Localization;
|
|
using Medallion.Threading;
|
|
using Medallion.Threading.Redis;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
|
using Microsoft.Extensions.Caching.StackExchangeRedis;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.IdentityModel.Logging;
|
|
using Microsoft.OpenApi.Models;
|
|
using OpenTelemetry.Metrics;
|
|
using OpenTelemetry.Resources;
|
|
using OpenTelemetry.Trace;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.Encodings.Web;
|
|
using System.Text.Unicode;
|
|
using System.Threading.Tasks;
|
|
using Volo.Abp;
|
|
using Volo.Abp.AspNetCore.Mvc;
|
|
using Volo.Abp.Auditing;
|
|
using Volo.Abp.BlobStoring;
|
|
using Volo.Abp.BlobStoring.FileSystem;
|
|
using Volo.Abp.Caching;
|
|
using Volo.Abp.EntityFrameworkCore;
|
|
using Volo.Abp.FeatureManagement;
|
|
using Volo.Abp.GlobalFeatures;
|
|
using Volo.Abp.Http.Client;
|
|
using Volo.Abp.Json;
|
|
using Volo.Abp.Json.SystemTextJson;
|
|
using Volo.Abp.Localization;
|
|
using Volo.Abp.MultiTenancy;
|
|
using Volo.Abp.Security.Claims;
|
|
using Volo.Abp.Threading;
|
|
using Volo.Abp.Timing;
|
|
using Volo.Abp.VirtualFileSystem;
|
|
|
|
namespace LY.MicroService.PlatformManagement;
|
|
|
|
public partial class PlatformManagementHttpApiHostModule
|
|
{
|
|
public static string ApplicationName { get; set; } = "PlatformService";
|
|
protected const string DefaultCorsPolicyName = "Default";
|
|
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();
|
|
|
|
private void PreConfigureFeature()
|
|
{
|
|
OneTimeRunner.Run(() =>
|
|
{
|
|
GlobalFeatureManager.Instance.Modules.Editions().EnableAll();
|
|
});
|
|
}
|
|
|
|
private void PreForwardedHeaders()
|
|
{
|
|
}
|
|
|
|
private void PreConfigureApp(IConfiguration configuration)
|
|
{
|
|
AbpSerilogEnrichersConsts.ApplicationName = ApplicationName;
|
|
|
|
PreConfigure<AbpSerilogEnrichersUniqueIdOptions>(options =>
|
|
{
|
|
// 以开放端口区别,应在0-31之间
|
|
options.SnowflakeIdOptions.WorkerId = 25;
|
|
options.SnowflakeIdOptions.WorkerIdBits = 5;
|
|
options.SnowflakeIdOptions.DatacenterId = 1;
|
|
});
|
|
|
|
if (configuration.GetValue<bool>("App:ShowPii"))
|
|
{
|
|
IdentityModelEventSource.ShowPII = true;
|
|
}
|
|
}
|
|
|
|
private void PreConfigureCAP(IConfiguration configuration)
|
|
{
|
|
PreConfigure<CapOptions>(options =>
|
|
{
|
|
options
|
|
.UseMySql(mySqlOptions =>
|
|
{
|
|
configuration.GetSection("CAP:MySql").Bind(mySqlOptions);
|
|
})
|
|
.UseRabbitMQ(rabbitMQOptions =>
|
|
{
|
|
configuration.GetSection("CAP:RabbitMQ").Bind(rabbitMQOptions);
|
|
})
|
|
.UseDashboard();
|
|
});
|
|
}
|
|
|
|
private void ConfigureDbContext()
|
|
{
|
|
// 配置Ef
|
|
Configure<AbpDbContextOptions>(options =>
|
|
{
|
|
options.UseMySQL();
|
|
});
|
|
}
|
|
|
|
private void ConfigureFeatureManagement()
|
|
{
|
|
Configure<FeatureManagementOptions>(options =>
|
|
{
|
|
options.IsDynamicFeatureStoreEnabled = true;
|
|
});
|
|
}
|
|
|
|
private void ConfigureJsonSerializer(IConfiguration configuration)
|
|
{
|
|
// 统一时间日期格式
|
|
Configure<AbpJsonOptions>(options =>
|
|
{
|
|
var jsonConfiguration = configuration.GetSection("Json");
|
|
if (jsonConfiguration.Exists())
|
|
{
|
|
jsonConfiguration.Bind(options);
|
|
}
|
|
});
|
|
// 中文序列化的编码问题
|
|
Configure<AbpSystemTextJsonSerializerOptions>(options =>
|
|
{
|
|
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
|
|
});
|
|
}
|
|
|
|
private void ConfigureKestrelServer()
|
|
{
|
|
Configure<KestrelServerOptions>(options =>
|
|
{
|
|
options.Limits.MaxRequestBodySize = null;
|
|
options.Limits.MaxRequestBufferSize = null;
|
|
});
|
|
}
|
|
|
|
private void ConfigureBlobStoring()
|
|
{
|
|
Configure<AbpBlobStoringOptions>(options =>
|
|
{
|
|
options.Containers.ConfigureAll((containerName, containerConfiguration) =>
|
|
{
|
|
containerConfiguration.UseFileSystem(fileSystem =>
|
|
{
|
|
fileSystem.BasePath = Path.Combine(Directory.GetCurrentDirectory(), "file-blob-storing");
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
private void ConfigureExceptionHandling()
|
|
{
|
|
// 自定义需要处理的异常
|
|
Configure<AbpExceptionHandlingOptions>(options =>
|
|
{
|
|
// 加入需要处理的异常类型
|
|
options.Handlers.Add<Volo.Abp.Data.AbpDbConcurrencyException>();
|
|
options.Handlers.Add<AbpInitializationException>();
|
|
options.Handlers.Add<ObjectDisposedException>();
|
|
options.Handlers.Add<StackOverflowException>();
|
|
options.Handlers.Add<OutOfMemoryException>();
|
|
options.Handlers.Add<System.Data.Common.DbException>();
|
|
options.Handlers.Add<Microsoft.EntityFrameworkCore.DbUpdateException>();
|
|
options.Handlers.Add<System.Data.DBConcurrencyException>();
|
|
});
|
|
// 自定义需要发送邮件通知的异常类型
|
|
Configure<AbpEmailExceptionHandlingOptions>(options =>
|
|
{
|
|
// 是否发送堆栈信息
|
|
options.SendStackTrace = true;
|
|
});
|
|
|
|
Configure<Volo.Abp.AspNetCore.ExceptionHandling.AbpExceptionHandlingOptions>(options =>
|
|
{
|
|
// 是否发送错误详情
|
|
options.SendExceptionsDetailsToClients = false;
|
|
options.SendStackTraceToClients = false;
|
|
});
|
|
}
|
|
|
|
private void ConfigureAuditing(IConfiguration configuration)
|
|
{
|
|
Configure<AbpAuditingOptions>(options =>
|
|
{
|
|
options.ApplicationName = ApplicationName;
|
|
// 是否启用实体变更记录
|
|
var allEntitiesSelectorIsEnabled = configuration["Auditing:AllEntitiesSelector"];
|
|
if (allEntitiesSelectorIsEnabled.IsNullOrWhiteSpace() ||
|
|
(bool.TryParse(allEntitiesSelectorIsEnabled, out var enabled) && enabled))
|
|
{
|
|
options.EntityHistorySelectors.AddAllEntities();
|
|
}
|
|
});
|
|
}
|
|
|
|
private void ConfigureDistributedLocking(IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
var distributedLockEnabled = configuration["DistributedLock:IsEnabled"];
|
|
if (distributedLockEnabled.IsNullOrEmpty() || bool.Parse(distributedLockEnabled))
|
|
{
|
|
var redis = ConnectionMultiplexer.Connect(configuration["DistributedLock:Redis:Configuration"]);
|
|
services.AddSingleton<IDistributedLockProvider>(_ => new RedisDistributedSynchronizationProvider(redis.GetDatabase()));
|
|
}
|
|
}
|
|
|
|
private void ConfigureOpenTelemetry(IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
var openTelemetryEnabled = configuration["OpenTelemetry:IsEnabled"];
|
|
if (openTelemetryEnabled.IsNullOrEmpty() || bool.Parse(openTelemetryEnabled))
|
|
{
|
|
services.AddOpenTelemetry()
|
|
.ConfigureResource(resource =>
|
|
{
|
|
resource.AddService(ApplicationName);
|
|
})
|
|
.WithTracing(tracing =>
|
|
{
|
|
tracing.AddHttpClientInstrumentation();
|
|
tracing.AddAspNetCoreInstrumentation();
|
|
tracing.AddCapInstrumentation();
|
|
tracing.AddEntityFrameworkCoreInstrumentation();
|
|
tracing.AddSource(ApplicationName);
|
|
|
|
var tracingOtlpEndpoint = configuration["OpenTelemetry:Otlp:Endpoint"];
|
|
if (!tracingOtlpEndpoint.IsNullOrWhiteSpace())
|
|
{
|
|
tracing.AddOtlpExporter(otlpOptions =>
|
|
{
|
|
otlpOptions.Endpoint = new Uri(tracingOtlpEndpoint);
|
|
});
|
|
return;
|
|
}
|
|
|
|
var zipkinEndpoint = configuration["OpenTelemetry:ZipKin:Endpoint"];
|
|
if (!zipkinEndpoint.IsNullOrWhiteSpace())
|
|
{
|
|
tracing.AddZipkinExporter(zipKinOptions =>
|
|
{
|
|
zipKinOptions.Endpoint = new Uri(zipkinEndpoint);
|
|
});
|
|
return;
|
|
}
|
|
})
|
|
.WithMetrics(metrics =>
|
|
{
|
|
metrics.AddRuntimeInstrumentation();
|
|
metrics.AddHttpClientInstrumentation();
|
|
metrics.AddAspNetCoreInstrumentation();
|
|
});
|
|
}
|
|
}
|
|
|
|
private void ConfigureTiming(IConfiguration configuration)
|
|
{
|
|
Configure<AbpClockOptions>(options =>
|
|
{
|
|
configuration.GetSection("Clock").Bind(options);
|
|
});
|
|
}
|
|
|
|
private void ConfigureCaching(IConfiguration configuration)
|
|
{
|
|
Configure<AbpDistributedCacheOptions>(options =>
|
|
{
|
|
configuration.GetSection("DistributedCache").Bind(options);
|
|
});
|
|
|
|
Configure<RedisCacheOptions>(options =>
|
|
{
|
|
var redisConfig = ConfigurationOptions.Parse(options.Configuration);
|
|
options.ConfigurationOptions = redisConfig;
|
|
options.InstanceName = configuration["Redis:InstanceName"];
|
|
});
|
|
}
|
|
|
|
private void ConfigureMvc(IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
Configure<AbpAspNetCoreMvcOptions>(options =>
|
|
{
|
|
options.ExposeIntegrationServices = true;
|
|
});
|
|
|
|
Configure<AbpEndpointRouterOptions>(options =>
|
|
{
|
|
options.EndpointConfigureActions.Add((builder) =>
|
|
{
|
|
builder.Endpoints.MapHealthChecks(configuration["App:HealthChecks"] ?? "/healthz");
|
|
});
|
|
});
|
|
|
|
services.AddHealthChecks();
|
|
}
|
|
|
|
private void ConfigureVirtualFileSystem()
|
|
{
|
|
Configure<AbpVirtualFileSystemOptions>(options =>
|
|
{
|
|
options.FileSets.AddEmbedded<PlatformManagementHttpApiHostModule>("LY.MicroService.PlatformManagement");
|
|
});
|
|
}
|
|
|
|
private void ConfigureMultiTenancy(IConfiguration configuration)
|
|
{
|
|
// 多租户
|
|
Configure<AbpMultiTenancyOptions>(options =>
|
|
{
|
|
options.IsEnabled = true;
|
|
});
|
|
|
|
var tenantResolveCfg = configuration.GetSection("App:Domains");
|
|
if (tenantResolveCfg.Exists())
|
|
{
|
|
Configure<AbpTenantResolveOptions>(options =>
|
|
{
|
|
var domains = tenantResolveCfg.Get<string[]>();
|
|
foreach (var domain in domains)
|
|
{
|
|
options.AddDomainTenantResolver(domain);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
private void ConfigureIdentity(IConfiguration configuration)
|
|
{
|
|
Configure<AbpClaimsPrincipalFactoryOptions>(options =>
|
|
{
|
|
options.IsDynamicClaimsEnabled = true;
|
|
options.RemoteRefreshUrl = configuration["App:RefreshClaimsUrl"] + options.RemoteRefreshUrl;
|
|
});
|
|
}
|
|
|
|
private void ConfigureSwagger(IServiceCollection services)
|
|
{
|
|
// Swagger
|
|
services.AddSwaggerGen(
|
|
options =>
|
|
{
|
|
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Platform API", Version = "v1" });
|
|
options.DocInclusionPredicate((docName, description) => true);
|
|
options.CustomSchemaIds(type => type.FullName);
|
|
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
|
|
Name = "Authorization",
|
|
In = ParameterLocation.Header,
|
|
Scheme = "bearer",
|
|
Type = SecuritySchemeType.Http,
|
|
BearerFormat = "JWT"
|
|
});
|
|
options.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }
|
|
},
|
|
new string[] { }
|
|
}
|
|
});
|
|
options.OperationFilter<TenantHeaderParamter>();
|
|
});
|
|
}
|
|
|
|
private void ConfigureLocalization()
|
|
{
|
|
// 支持本地化语言类型
|
|
Configure<AbpLocalizationOptions>(options =>
|
|
{
|
|
options.Languages.Add(new LanguageInfo("en", "en", "English"));
|
|
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文"));
|
|
|
|
options.UsePersistence<PlatformResource>();
|
|
});
|
|
|
|
Configure<AbpLocalizationCultureMapOptions>(options =>
|
|
{
|
|
var zhHansCultureMapInfo = new CultureMapInfo
|
|
{
|
|
TargetCulture = "zh-Hans",
|
|
SourceCultures = new string[] { "zh", "zh_CN", "zh-CN" }
|
|
};
|
|
|
|
options.CulturesMaps.Add(zhHansCultureMapInfo);
|
|
options.UiCulturesMaps.Add(zhHansCultureMapInfo);
|
|
});
|
|
}
|
|
|
|
private void ConfigureCors(IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
services.AddCors(options =>
|
|
{
|
|
options.AddPolicy(DefaultCorsPolicyName, builder =>
|
|
{
|
|
builder
|
|
.WithOrigins(
|
|
configuration["App:CorsOrigins"]
|
|
.Split(",", StringSplitOptions.RemoveEmptyEntries)
|
|
.Select(o => o.RemovePostFix("/"))
|
|
.ToArray()
|
|
)
|
|
.WithAbpExposedHeaders()
|
|
.WithAbpWrapExposedHeaders()
|
|
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
}
|
|
|
|
private void ConfigureSecurity(IServiceCollection services, IConfiguration configuration, bool isDevelopment = false)
|
|
{
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
configuration.GetSection("AuthServer").Bind(options);
|
|
options.Events = new JwtBearerEvents
|
|
{
|
|
OnMessageReceived = context =>
|
|
{
|
|
var accessToken = context.Request.Query["access_token"];
|
|
var path = context.HttpContext.Request.Path;
|
|
if (!string.IsNullOrEmpty(accessToken) &&
|
|
(path.StartsWithSegments("/api/files")))
|
|
{
|
|
context.Token = accessToken;
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
};
|
|
});
|
|
|
|
if (!isDevelopment)
|
|
{
|
|
var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
|
|
services
|
|
.AddDataProtection()
|
|
.SetApplicationName("LINGYUN.Abp.Application")
|
|
.PersistKeysToStackExchangeRedis(redis, "LINGYUN.Abp.Application:DataProtection:Protection-Keys");
|
|
}
|
|
}
|
|
|
|
private void ConfigureWrapper()
|
|
{
|
|
Configure<AbpWrapperOptions>(options =>
|
|
{
|
|
options.IsEnabled = true;
|
|
});
|
|
}
|
|
|
|
private void PreConfigureWrapper()
|
|
{
|
|
//PreConfigure<AbpDaprClientProxyOptions>(options =>
|
|
//{
|
|
// options.ProxyRequestActions.Add(
|
|
// (appid, httprequestmessage) =>
|
|
// {
|
|
// httprequestmessage.Headers.TryAddWithoutValidation(AbpHttpWrapConsts.AbpDontWrapResult, "true");
|
|
// });
|
|
//});
|
|
// 服务间调用不包装
|
|
PreConfigure<AbpHttpClientBuilderOptions>(options =>
|
|
{
|
|
options.ProxyClientActions.Add(
|
|
(_, _, client) =>
|
|
{
|
|
client.DefaultRequestHeaders.TryAddWithoutValidation(AbpHttpWrapConsts.AbpDontWrapResult, "true");
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|