using Autofac.Core; using LINGYUN.Abp.AspNetCore.Mvc.Wrapper; using LINGYUN.Abp.Serilog.Enrichers.Application; using LINGYUN.Abp.Serilog.Enrichers.UniqueId; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.WebSockets; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.ApiExploring; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; using Volo.Abp.AspNetCore.Serilog; using Volo.Abp.Autofac; using Volo.Abp.Data; using Volo.Abp.Modularity; using Volo.Abp.Swashbuckle; namespace LINGYUN.Abp.MicroService.ApiGateway; [DependsOn( typeof(AbpSerilogEnrichersApplicationModule), typeof(AbpSerilogEnrichersUniqueIdModule), typeof(AbpAutofacModule), typeof(AbpDataModule), typeof(AbpSwashbuckleModule), typeof(AbpAspNetCoreSerilogModule), typeof(AbpAspNetCoreMvcWrapperModule) )] public class ApiGatewayModule : AbpModule { public override void PreConfigureServices(ServiceConfigurationContext context) { PreConfigure(options => { // 以开放端口区别 options.SnowflakeIdOptions.WorkerId = 0; options.SnowflakeIdOptions.WorkerIdBits = 5; options.SnowflakeIdOptions.DatacenterId = 1; }); } public override void ConfigureServices(ServiceConfigurationContext context) { var configuration = context.Services.GetConfiguration(); var hostingEnvironment = context.Services.GetHostingEnvironment(); Configure(options => { options.ControllersToRemove.Add(typeof(AbpApiDefinitionController)); options.ControllersToRemove.Add(typeof(AbpApplicationLocalizationController)); options.ControllersToRemove.Add(typeof(AbpApplicationConfigurationController)); options.ControllersToRemove.Add(typeof(AbpApplicationConfigurationScriptController)); }); context.Services.AddAbpSwaggerGenWithOAuth( authority: configuration["AuthServer:Authority"], scopes: new Dictionary { {"Account", "Account API"}, {"Identity", "Identity API"}, {"IdentityServer", "Identity Server API"}, {"BackendAdmin", "Backend Admin API"}, {"Localization", "Localization API"}, {"Platform", "Platform API"}, {"RealtimeMessage", "RealtimeMessage API"}, {"TaskManagement", "Task Management API"}, {"Webhooks", "Webhooks API"}, }, options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "ApiGateway", Version = "v1" }); options.DocInclusionPredicate((docName, description) => true); options.CustomSchemaIds(type => type.FullName); }); context.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { configuration.GetSection("AuthServer").Bind(options); }); context.Services .AddDataProtection() .SetApplicationName("LINGYUN.Abp.Application") .PersistKeysToStackExchangeRedis(() => { var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]!); return redis.GetDatabase(); }, "LINGYUN.Abp.Application:DataProtection:Protection-Keys"); context.Services.AddCors(options => { options.AddDefaultPolicy(builder => { var corsOrigins = configuration.GetSection("App:CorsOrigins").Get>(); if (corsOrigins == null || corsOrigins.Count == 0) { corsOrigins = configuration["App:CorsOrigins"]? .Split(",", StringSplitOptions.RemoveEmptyEntries) .Select(o => o.RemovePostFix("/")) .ToList() ?? new List(); } builder .WithOrigins(corsOrigins .Select(o => o.RemovePostFix("/")) .ToArray() ) .WithAbpExposedHeaders() .WithAbpWrapExposedHeaders() .SetIsOriginAllowedToAllowWildcardSubdomains() .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); context.Services.AddWebSockets(options => { }); context.Services.AddHttpForwarder(); context.Services.AddTelemetryListeners(); context.Services .AddReverseProxy() .ConfigureHttpClient((context, handler) => { handler.ActivityHeadersPropagator = null; }) .LoadFromConfig(configuration.GetSection("ReverseProxy")); } }