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.
544 lines
20 KiB
544 lines
20 KiB
using DotNetCore.CAP;
|
|
using Elsa;
|
|
using Elsa.Options;
|
|
using Elsa.Rebus.RabbitMq;
|
|
using LINGYUN.Abp.BackgroundTasks;
|
|
using LINGYUN.Abp.BlobStoring.OssManagement;
|
|
using LINGYUN.Abp.Elsa.Localization;
|
|
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 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.EntityFrameworkCore;
|
|
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 Quartz;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Encodings.Web;
|
|
using System.Text.Unicode;
|
|
using Volo.Abp;
|
|
using Volo.Abp.AspNetCore.Mvc;
|
|
using Volo.Abp.AspNetCore.Mvc.AntiForgery;
|
|
using Volo.Abp.Auditing;
|
|
using Volo.Abp.BlobStoring;
|
|
using Volo.Abp.Caching;
|
|
using Volo.Abp.EntityFrameworkCore;
|
|
using Volo.Abp.GlobalFeatures;
|
|
using Volo.Abp.Json;
|
|
using Volo.Abp.Json.SystemTextJson;
|
|
using Volo.Abp.Localization;
|
|
using Volo.Abp.MultiTenancy;
|
|
using Volo.Abp.Quartz;
|
|
using Volo.Abp.Security.Claims;
|
|
using Volo.Abp.Threading;
|
|
using Volo.Abp.Timing;
|
|
using Volo.Abp.VirtualFileSystem;
|
|
|
|
namespace LY.MicroService.WorkflowManagement;
|
|
|
|
public partial class WorkflowManagementHttpApiHostModule
|
|
{
|
|
public static string ApplicationName { get; set; } = "WorkflowService";
|
|
private const string DefaultCorsPolicyName = "Default";
|
|
private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();
|
|
|
|
private void PreConfigureFeature()
|
|
{
|
|
OneTimeRunner.Run(() =>
|
|
{
|
|
GlobalFeatureManager.Instance.Modules.Editions().EnableAll();
|
|
});
|
|
}
|
|
|
|
private void PreConfigureForwardedHeaders()
|
|
{
|
|
}
|
|
|
|
private void PreConfigureApp(IConfiguration configuration)
|
|
{
|
|
AbpSerilogEnrichersConsts.ApplicationName = ApplicationName;
|
|
|
|
PreConfigure<AbpSerilogEnrichersUniqueIdOptions>(options =>
|
|
{
|
|
// 以开放端口区别
|
|
options.SnowflakeIdOptions.WorkerId = 29;
|
|
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 PreConfigureQuartz(IConfiguration configuration)
|
|
{
|
|
PreConfigure<AbpQuartzOptions>(options =>
|
|
{
|
|
// 如果使用持久化存储, 则配置quartz持久层
|
|
if (configuration.GetSection("Quartz:UsePersistentStore").Get<bool>())
|
|
{
|
|
var settings = configuration.GetSection("Quartz:Properties").Get<Dictionary<string, string>>();
|
|
if (settings != null)
|
|
{
|
|
foreach (var setting in settings)
|
|
{
|
|
options.Properties[setting.Key] = setting.Value;
|
|
}
|
|
}
|
|
|
|
options.Configurator += (config) =>
|
|
{
|
|
config.UsePersistentStore(store =>
|
|
{
|
|
store.UseProperties = false;
|
|
store.UseNewtonsoftJsonSerializer();
|
|
});
|
|
};
|
|
}
|
|
});
|
|
}
|
|
|
|
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 ConfigureBackgroundTasks(IConfiguration configuration)
|
|
{
|
|
Configure<AbpBackgroundTasksOptions>(options =>
|
|
{
|
|
options.NodeName = ApplicationName;
|
|
});
|
|
}
|
|
|
|
private void PreConfigureElsa(IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
var elsaSection = configuration.GetSection("Elsa");
|
|
var startups = new[]
|
|
{
|
|
typeof(Elsa.Activities.Console.Startup),
|
|
typeof(Elsa.Activities.Http.Startup),
|
|
typeof(Elsa.Activities.UserTask.Startup),
|
|
typeof(Elsa.Activities.Temporal.Quartz.Startup),
|
|
typeof(Elsa.Activities.Email.Startup),
|
|
typeof(Elsa.Scripting.JavaScript.Startup),
|
|
typeof(Elsa.Activities.Webhooks.Startup),
|
|
};
|
|
|
|
PreConfigure<ElsaOptionsBuilder>(elsa =>
|
|
{
|
|
elsa
|
|
.AddActivitiesFrom<WorkflowManagementHttpApiHostModule>()
|
|
.AddWorkflowsFrom<WorkflowManagementHttpApiHostModule>()
|
|
.AddFeatures(startups, configuration)
|
|
.ConfigureWorkflowChannels(options => elsaSection.GetSection("WorkflowChannels").Bind(options))
|
|
.UseRabbitMq(elsaSection["Rebus:RabbitMQ:Connection"]);
|
|
|
|
elsa.DistributedLockingOptionsBuilder
|
|
.UseProviderFactory(sp => name =>
|
|
{
|
|
var provider = sp.GetRequiredService<IDistributedLockProvider>();
|
|
|
|
return provider.CreateLock(name);
|
|
});
|
|
});
|
|
|
|
services.AddNotificationHandlersFrom<WorkflowManagementHttpApiHostModule>();
|
|
|
|
PreConfigure<IMvcBuilder>(mvcBuilder =>
|
|
{
|
|
mvcBuilder.AddApplicationPartIfNotExists(typeof(Elsa.Webhooks.Api.Endpoints.List).Assembly);
|
|
});
|
|
}
|
|
|
|
private void ConfigureEndpoints(IServiceCollection services)
|
|
{
|
|
// 不需要
|
|
//Configure<AbpEndpointRouterOptions>(options =>
|
|
//{
|
|
// options.EndpointConfigureActions.Add(
|
|
// (context) =>
|
|
// {
|
|
// context.Endpoints.MapFallbackToPage("/_Host");
|
|
// });
|
|
//});
|
|
var preActions = services.GetPreConfigureActions<AbpAspNetCoreMvcOptions>();
|
|
|
|
services.AddAbpApiVersioning(options =>
|
|
{
|
|
options.ReportApiVersions = true;
|
|
options.AssumeDefaultVersionWhenUnspecified = true;
|
|
|
|
//options.ApiVersionReader = new HeaderApiVersionReader("api-version"); //Supports header too
|
|
//options.ApiVersionReader = new MediaTypeApiVersionReader(); //Supports accept header too
|
|
}, mvcOptions =>
|
|
{
|
|
mvcOptions.ConfigureAbp(preActions.Configure());
|
|
});
|
|
}
|
|
|
|
private void ConfigureDistributedLock(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 ConfigureBlobStoring(IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
var preActions = services.GetPreConfigureActions<AbpBlobStoringOptions>();
|
|
Configure<AbpBlobStoringOptions>(options =>
|
|
{
|
|
preActions.Configure(options);
|
|
//options.Containers.Configure<WorkflowContainer>((containerConfiguration) =>
|
|
//{
|
|
// containerConfiguration.UseOssManagement(config =>
|
|
// {
|
|
// config.Bucket = configuration[OssManagementBlobProviderConfigurationNames.Bucket] ?? "workflow";
|
|
// });
|
|
//});
|
|
options.Containers.ConfigureAll((_, containerConfiguration) =>
|
|
{
|
|
containerConfiguration.UseOssManagement(config =>
|
|
{
|
|
config.Bucket = configuration[OssManagementBlobProviderConfigurationNames.Bucket] ?? "workflow";
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
private void ConfigureDbContext()
|
|
{
|
|
// 配置Ef
|
|
Configure<AbpDbContextOptions>(options =>
|
|
{
|
|
options.UseMySQL();
|
|
});
|
|
}
|
|
|
|
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 ConfigureExceptionHandling()
|
|
{
|
|
// 自定义需要处理的异常
|
|
Configure<AbpExceptionHandlingOptions>(options =>
|
|
{
|
|
// 加入需要处理的异常类型
|
|
options.Handlers.Add<Volo.Abp.Data.AbpDbConcurrencyException>();
|
|
options.Handlers.Add<AbpInitializationException>();
|
|
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;
|
|
// 未指定异常接收者的默认接收邮件
|
|
// 指定自己的邮件地址
|
|
});
|
|
}
|
|
|
|
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 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<WorkflowManagementHttpApiHostModule>("LINGYUN.Abp.WorkflowManagement");
|
|
});
|
|
}
|
|
|
|
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 = "Workflow 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<ElsaResource>();
|
|
});
|
|
|
|
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 ConfigureSecurity(IServiceCollection services, IConfiguration configuration, bool isDevelopment = false)
|
|
{
|
|
Configure<AbpAntiForgeryOptions>(options =>
|
|
{
|
|
options.AutoValidate = false;
|
|
//options.AutoValidateFilter = (type) => !type.Namespace.Contains("elsa", StringComparison.CurrentCultureIgnoreCase);
|
|
});
|
|
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
configuration.GetSection("AuthServer").Bind(options);
|
|
});
|
|
|
|
//services.AddElsaJwtBearerAuthentication(options =>
|
|
//{
|
|
//});
|
|
|
|
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 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();
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|