committed by
GitHub
2 changed files with 255 additions and 243 deletions
@ -1,29 +1,34 @@ |
|||||
using LINGYUN.Abp.ExceptionHandling.Emailing.Localization; |
using LINGYUN.Abp.ExceptionHandling.Emailing.Localization; |
||||
using Volo.Abp.Emailing; |
using Microsoft.Extensions.DependencyInjection; |
||||
using Volo.Abp.Localization; |
using Volo.Abp.Emailing; |
||||
using Volo.Abp.Modularity; |
using Volo.Abp.Localization; |
||||
using Volo.Abp.VirtualFileSystem; |
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.VirtualFileSystem; |
||||
namespace LINGYUN.Abp.ExceptionHandling.Emailing |
|
||||
{ |
namespace LINGYUN.Abp.ExceptionHandling.Emailing |
||||
[DependsOn( |
{ |
||||
typeof(AbpExceptionHandlingModule), |
[DependsOn( |
||||
typeof(AbpEmailingModule))] |
typeof(AbpExceptionHandlingModule), |
||||
public class AbpEmailingExceptionHandlingModule : AbpModule |
typeof(AbpEmailingModule))] |
||||
{ |
public class AbpEmailingExceptionHandlingModule : AbpModule |
||||
public override void ConfigureServices(ServiceConfigurationContext context) |
{ |
||||
{ |
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
Configure<AbpVirtualFileSystemOptions>(options => |
{ |
||||
{ |
var configuration = context.Services.GetConfiguration(); |
||||
options.FileSets.AddEmbedded<AbpEmailingExceptionHandlingModule>(); |
Configure<AbpEmailExceptionHandlingOptions>( |
||||
}); |
configuration.GetSection("ExceptionHandling:Emailing")); |
||||
|
|
||||
Configure<AbpLocalizationOptions>(options => |
Configure<AbpVirtualFileSystemOptions>(options => |
||||
{ |
{ |
||||
options.Resources |
options.FileSets.AddEmbedded<AbpEmailingExceptionHandlingModule>(); |
||||
.Add<ExceptionHandlingResource>("en") |
}); |
||||
.AddVirtualJson("/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/Resources"); |
|
||||
}); |
Configure<AbpLocalizationOptions>(options => |
||||
} |
{ |
||||
} |
options.Resources |
||||
} |
.Add<ExceptionHandlingResource>("en") |
||||
|
.AddVirtualJson("/LINGYUN/Abp/ExceptionHandling/Emailing/Localization/Resources"); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|||||
@ -1,214 +1,221 @@ |
|||||
using DotNetCore.CAP; |
using DotNetCore.CAP; |
||||
using LINGYUN.Abp.AspNetCore.HttpOverrides; |
using LINGYUN.Abp.AspNetCore.HttpOverrides; |
||||
using LINGYUN.Abp.EventBus.CAP; |
using LINGYUN.Abp.EventBus.CAP; |
||||
using LINGYUN.ApiGateway.Localization; |
using LINGYUN.ApiGateway.Localization; |
||||
using Microsoft.AspNetCore.Authentication.JwtBearer; |
using Microsoft.AspNetCore.Authentication.JwtBearer; |
||||
using Microsoft.AspNetCore.Builder; |
using Microsoft.AspNetCore.Builder; |
||||
using Microsoft.AspNetCore.DataProtection; |
using Microsoft.AspNetCore.DataProtection; |
||||
using Microsoft.AspNetCore.Hosting; |
using Microsoft.AspNetCore.Hosting; |
||||
using Microsoft.AspNetCore.Routing; |
using Microsoft.AspNetCore.Routing; |
||||
using Microsoft.AspNetCore.Server.Kestrel.Core; |
using Microsoft.AspNetCore.Server.Kestrel.Core; |
||||
using Microsoft.Extensions.Caching.StackExchangeRedis; |
using Microsoft.Extensions.Caching.StackExchangeRedis; |
||||
using Microsoft.Extensions.Configuration; |
using Microsoft.Extensions.Configuration; |
||||
using Microsoft.Extensions.DependencyInjection; |
using Microsoft.Extensions.DependencyInjection; |
||||
using Microsoft.Extensions.Hosting; |
using Microsoft.Extensions.Hosting; |
||||
using Ocelot.Configuration.Repository; |
using Ocelot.Configuration.Repository; |
||||
using Ocelot.DependencyInjection; |
using Ocelot.DependencyInjection; |
||||
using Ocelot.Extenssions; |
using Ocelot.Extenssions; |
||||
using Ocelot.Middleware.Multiplexer; |
using Ocelot.Middleware.Multiplexer; |
||||
using Ocelot.Provider.Polly; |
using Ocelot.Provider.Polly; |
||||
using StackExchange.Redis; |
using StackExchange.Redis; |
||||
using System; |
using System; |
||||
using System.Text; |
using System.Text; |
||||
using System.Text.Encodings.Web; |
using System.Text.Encodings.Web; |
||||
using System.Text.Unicode; |
using System.Text.Unicode; |
||||
using Volo.Abp; |
using Volo.Abp; |
||||
using Volo.Abp.AspNetCore; |
using Volo.Abp.AspNetCore; |
||||
using Volo.Abp.Autofac; |
using Volo.Abp.Autofac; |
||||
using Volo.Abp.AutoMapper; |
using Volo.Abp.AutoMapper; |
||||
using Volo.Abp.Caching; |
using Volo.Abp.Caching; |
||||
using Volo.Abp.Caching.StackExchangeRedis; |
using Volo.Abp.Caching.StackExchangeRedis; |
||||
using Volo.Abp.Http.Client.IdentityModel; |
using Volo.Abp.Http.Client.IdentityModel; |
||||
using Volo.Abp.Json; |
using Volo.Abp.Json; |
||||
using Volo.Abp.Json.SystemTextJson; |
using Volo.Abp.Json.SystemTextJson; |
||||
using Volo.Abp.Localization; |
using Volo.Abp.Localization; |
||||
using Volo.Abp.Modularity; |
using Volo.Abp.Modularity; |
||||
using Volo.Abp.Security.Encryption; |
using Volo.Abp.Security.Encryption; |
||||
using Volo.Abp.VirtualFileSystem; |
using Volo.Abp.VirtualFileSystem; |
||||
|
|
||||
namespace LINGYUN.ApiGateway |
namespace LINGYUN.ApiGateway |
||||
{ |
{ |
||||
[DependsOn( |
[DependsOn( |
||||
typeof(AbpAutofacModule), |
typeof(AbpAutofacModule), |
||||
typeof(AbpHttpClientIdentityModelModule), |
typeof(AbpHttpClientIdentityModelModule), |
||||
typeof(AbpCachingStackExchangeRedisModule), |
typeof(AbpCachingStackExchangeRedisModule), |
||||
typeof(AbpAutoMapperModule), |
typeof(AbpAutoMapperModule), |
||||
typeof(ApiGatewayHttpApiClientModule), |
typeof(ApiGatewayHttpApiClientModule), |
||||
typeof(AbpCAPEventBusModule), |
typeof(AbpCAPEventBusModule), |
||||
typeof(AbpAspNetCoreModule), |
typeof(AbpAspNetCoreModule), |
||||
typeof(AbpAspNetCoreHttpOverridesModule) |
typeof(AbpAspNetCoreHttpOverridesModule) |
||||
)] |
)] |
||||
public class ApiGatewayHostModule : AbpModule |
public class ApiGatewayHostModule : AbpModule |
||||
{ |
{ |
||||
public override void PreConfigureServices(ServiceConfigurationContext context) |
public override void PreConfigureServices(ServiceConfigurationContext context) |
||||
{ |
{ |
||||
var configuration = context.Services.GetConfiguration(); |
var configuration = context.Services.GetConfiguration(); |
||||
|
|
||||
// 不启用则使用本地配置文件的方式启动Ocelot
|
// 不启用则使用本地配置文件的方式启动Ocelot
|
||||
if (configuration.GetValue<bool>("EnabledDynamicOcelot")) |
if (configuration.GetValue<bool>("EnabledDynamicOcelot")) |
||||
{ |
{ |
||||
context.Services.AddSingleton<IFileConfigurationRepository, ApiHttpClientFileConfigurationRepository>(); |
context.Services.AddSingleton<IFileConfigurationRepository, ApiHttpClientFileConfigurationRepository>(); |
||||
} |
} |
||||
|
|
||||
PreConfigure<CapOptions>(options => |
PreConfigure<CapOptions>(options => |
||||
{ |
{ |
||||
options |
options |
||||
.UseSqlite("Data Source=./event-bus-cap.db") |
.UseSqlite("Data Source=./event-bus-cap.db") |
||||
.UseDashboard() |
.UseDashboard() |
||||
.UseRabbitMQ(rabbitMQOptions => |
.UseRabbitMQ(rabbitMQOptions => |
||||
{ |
{ |
||||
configuration.GetSection("CAP:RabbitMQ").Bind(rabbitMQOptions); |
configuration.GetSection("CAP:RabbitMQ").Bind(rabbitMQOptions); |
||||
}); |
}); |
||||
}); |
}); |
||||
} |
} |
||||
|
|
||||
public override void ConfigureServices(ServiceConfigurationContext context) |
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
{ |
{ |
||||
var hostingEnvironment = context.Services.GetHostingEnvironment(); |
var hostingEnvironment = context.Services.GetHostingEnvironment(); |
||||
var configuration = context.Services.GetConfiguration(); |
var configuration = context.Services.GetConfiguration(); |
||||
|
|
||||
Configure<AbpAutoMapperOptions>(options => |
// fix: 不限制请求体大小,解决上传文件问题
|
||||
{ |
Configure<KestrelServerOptions>(options => |
||||
options.AddProfile<ApiGatewayMapperProfile>(validate: true); |
{ |
||||
}); |
options.Limits.MaxRequestBodySize = null; |
||||
|
options.Limits.MaxRequestBufferSize = null; |
||||
Configure<ApiGatewayOptions>(configuration.GetSection("ApiGateway")); |
}); |
||||
|
|
||||
// 中文序列化的编码问题
|
Configure<AbpAutoMapperOptions>(options => |
||||
Configure<AbpSystemTextJsonSerializerOptions>(options => |
{ |
||||
{ |
options.AddProfile<ApiGatewayMapperProfile>(validate: true); |
||||
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); |
}); |
||||
}); |
|
||||
|
Configure<ApiGatewayOptions>(configuration.GetSection("ApiGateway")); |
||||
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) |
|
||||
.AddJwtBearer(options => |
// 中文序列化的编码问题
|
||||
{ |
Configure<AbpSystemTextJsonSerializerOptions>(options => |
||||
options.Authority = configuration["AuthServer:Authority"]; |
{ |
||||
options.RequireHttpsMetadata = false; |
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); |
||||
options.Audience = configuration["AuthServer:ApiName"]; |
}); |
||||
}); |
|
||||
|
context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) |
||||
Configure<AbpDistributedCacheOptions>(options => |
.AddJwtBearer(options => |
||||
{ |
{ |
||||
// 最好统一命名,不然某个缓存变动其他应用服务有例外发生
|
options.Authority = configuration["AuthServer:Authority"]; |
||||
options.KeyPrefix = "LINGYUN.Abp.Application"; |
options.RequireHttpsMetadata = false; |
||||
// 滑动过期30天
|
options.Audience = configuration["AuthServer:ApiName"]; |
||||
options.GlobalCacheEntryOptions.SlidingExpiration = TimeSpan.FromDays(30); |
}); |
||||
// 绝对过期60天
|
|
||||
options.GlobalCacheEntryOptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(60); |
Configure<AbpDistributedCacheOptions>(options => |
||||
}); |
{ |
||||
|
// 最好统一命名,不然某个缓存变动其他应用服务有例外发生
|
||||
Configure<RedisCacheOptions>(options => |
options.KeyPrefix = "LINGYUN.Abp.Application"; |
||||
{ |
// 滑动过期30天
|
||||
var redisConfig = ConfigurationOptions.Parse(options.Configuration); |
options.GlobalCacheEntryOptions.SlidingExpiration = TimeSpan.FromDays(30); |
||||
options.ConfigurationOptions = redisConfig; |
// 绝对过期60天
|
||||
options.InstanceName = configuration["Redis:InstanceName"]; |
options.GlobalCacheEntryOptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(60); |
||||
}); |
}); |
||||
|
|
||||
// 加解密
|
Configure<RedisCacheOptions>(options => |
||||
Configure<AbpStringEncryptionOptions>(options => |
{ |
||||
{ |
var redisConfig = ConfigurationOptions.Parse(options.Configuration); |
||||
var encryptionConfiguration = configuration.GetSection("Encryption"); |
options.ConfigurationOptions = redisConfig; |
||||
if (encryptionConfiguration.Exists()) |
options.InstanceName = configuration["Redis:InstanceName"]; |
||||
{ |
}); |
||||
options.DefaultPassPhrase = encryptionConfiguration["PassPhrase"] ?? options.DefaultPassPhrase; |
|
||||
options.DefaultSalt = encryptionConfiguration.GetSection("Salt").Exists() |
// 加解密
|
||||
? Encoding.ASCII.GetBytes(encryptionConfiguration["Salt"]) |
Configure<AbpStringEncryptionOptions>(options => |
||||
: options.DefaultSalt; |
{ |
||||
options.InitVectorBytes = encryptionConfiguration.GetSection("InitVector").Exists() |
var encryptionConfiguration = configuration.GetSection("Encryption"); |
||||
? Encoding.ASCII.GetBytes(encryptionConfiguration["InitVector"]) |
if (encryptionConfiguration.Exists()) |
||||
: options.InitVectorBytes; |
{ |
||||
} |
options.DefaultPassPhrase = encryptionConfiguration["PassPhrase"] ?? options.DefaultPassPhrase; |
||||
}); |
options.DefaultSalt = encryptionConfiguration.GetSection("Salt").Exists() |
||||
|
? Encoding.ASCII.GetBytes(encryptionConfiguration["Salt"]) |
||||
Configure<AbpVirtualFileSystemOptions>(options => |
: options.DefaultSalt; |
||||
{ |
options.InitVectorBytes = encryptionConfiguration.GetSection("InitVector").Exists() |
||||
options.FileSets.AddEmbedded<ApiGatewayHostModule>(); |
? Encoding.ASCII.GetBytes(encryptionConfiguration["InitVector"]) |
||||
}); |
: options.InitVectorBytes; |
||||
|
} |
||||
Configure<AbpLocalizationOptions>(options => |
}); |
||||
{ |
|
||||
options.Languages.Add(new LanguageInfo("en", "en", "English")); |
Configure<AbpVirtualFileSystemOptions>(options => |
||||
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文")); |
{ |
||||
|
options.FileSets.AddEmbedded<ApiGatewayHostModule>(); |
||||
options.Resources |
}); |
||||
.Get<ApiGatewayResource>() |
|
||||
.AddVirtualJson("/Localization/Host"); |
Configure<AbpLocalizationOptions>(options => |
||||
}); |
{ |
||||
|
options.Languages.Add(new LanguageInfo("en", "en", "English")); |
||||
var mvcBuilder = context.Services.AddMvc(); |
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "简体中文")); |
||||
mvcBuilder.AddApplicationPart(typeof(ApiGatewayHostModule).Assembly); |
|
||||
|
options.Resources |
||||
Configure<AbpEndpointRouterOptions>(options => |
.Get<ApiGatewayResource>() |
||||
{ |
.AddVirtualJson("/Localization/Host"); |
||||
options.EndpointConfigureActions.Add(endpointContext => |
}); |
||||
{ |
|
||||
endpointContext.Endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}"); |
var mvcBuilder = context.Services.AddMvc(); |
||||
endpointContext.Endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); |
mvcBuilder.AddApplicationPart(typeof(ApiGatewayHostModule).Assembly); |
||||
}); |
|
||||
}); |
Configure<AbpEndpointRouterOptions>(options => |
||||
|
{ |
||||
if (!hostingEnvironment.IsDevelopment()) |
options.EndpointConfigureActions.Add(endpointContext => |
||||
{ |
{ |
||||
// Ssl证书
|
endpointContext.Endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}"); |
||||
var sslOptions = configuration.GetSection("App:SslOptions"); |
endpointContext.Endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); |
||||
if (sslOptions.Exists()) |
}); |
||||
{ |
}); |
||||
var fileName = sslOptions["FileName"]; |
|
||||
var password = sslOptions["Password"]; |
if (!hostingEnvironment.IsDevelopment()) |
||||
Configure<KestrelServerOptions>(options => |
{ |
||||
{ |
// Ssl证书
|
||||
options.ConfigureEndpointDefaults(cfg => |
var sslOptions = configuration.GetSection("App:SslOptions"); |
||||
{ |
if (sslOptions.Exists()) |
||||
cfg.UseHttps(fileName, password); |
{ |
||||
}); |
var fileName = sslOptions["FileName"]; |
||||
}); |
var password = sslOptions["Password"]; |
||||
} |
Configure<KestrelServerOptions>(options => |
||||
|
{ |
||||
var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); |
options.ConfigureEndpointDefaults(cfg => |
||||
context.Services |
{ |
||||
.AddDataProtection() |
cfg.UseHttps(fileName, password); |
||||
.PersistKeysToStackExchangeRedis(redis, "ApiGatewayHost-Protection-Keys"); |
}); |
||||
} |
}); |
||||
|
} |
||||
context.Services |
|
||||
.AddOcelot() |
var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); |
||||
.AddPolly() |
context.Services |
||||
.AddSingletonDefinedAggregator<AbpApiDefinitionAggregator>(); |
.AddDataProtection() |
||||
} |
.PersistKeysToStackExchangeRedis(redis, "ApiGatewayHost-Protection-Keys"); |
||||
|
} |
||||
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
|
||||
{ |
context.Services |
||||
var app = context.GetApplicationBuilder(); |
.AddOcelot() |
||||
|
.AddPolly() |
||||
app.UseForwardedHeaders(); |
.AddSingletonDefinedAggregator<AbpApiDefinitionAggregator>(); |
||||
app.UseAuditing(); |
} |
||||
app.UseStaticFiles(); |
|
||||
app.UseRouting(); |
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
||||
app.UseAuthentication(); |
{ |
||||
app.UseAbpClaimsMap(); |
var app = context.GetApplicationBuilder(); |
||||
app.MapWhen( |
|
||||
ctx => ctx.Request.Path.ToString().StartsWith("/api/ApiGateway/Basic/"), |
app.UseForwardedHeaders(); |
||||
appNext => |
app.UseAuditing(); |
||||
{ |
app.UseStaticFiles(); |
||||
// 仅针对属于网关自己的控制器进入MVC管道
|
app.UseRouting(); |
||||
appNext.UseRouting(); |
app.UseAuthentication(); |
||||
appNext.UseConfiguredEndpoints(); |
app.UseAbpClaimsMap(); |
||||
}); |
app.MapWhen( |
||||
|
ctx => ctx.Request.Path.ToString().StartsWith("/api/ApiGateway/Basic/"), |
||||
// 启用ws协议
|
appNext => |
||||
app.UseWebSockets(); |
{ |
||||
app.UseOcelot().Wait(); |
// 仅针对属于网关自己的控制器进入MVC管道
|
||||
} |
appNext.UseRouting(); |
||||
} |
appNext.UseConfiguredEndpoints(); |
||||
} |
}); |
||||
|
|
||||
|
// 启用ws协议
|
||||
|
app.UseWebSockets(); |
||||
|
app.UseOcelot().Wait(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|||||
Loading…
Reference in new issue