using DS.Module.Core; using DS.Module.Core.Extensions; using DS.Module.Core.ServiceExtensions; using DS.WMS.Gateway; using Microsoft.OpenApi.Models; using Ocelot.DependencyInjection; using Ocelot.Middleware; var builder = WebApplication.CreateBuilder(args); var environment = builder.Environment.EnvironmentName; Console.WriteLine("当前开发环境:" + environment); //注册配置 builder.Configuration .AddJsonFile(path: "appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile(path: $"appsettings.{environment}.json", optional: true, reloadOnChange: true) .Build(); builder.Configuration.AddEnvironmentVariables(); IConfiguration configuration = new ConfigurationBuilder() .AddJsonFile("ocelot.json") .Build(); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); // builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen(options => { options.SwaggerDoc("ApiGateway", new OpenApiInfo { Title = "网关服务", Version = "v1" }); }); builder.Services.AddOcelot(configuration); //添加Ocelot服务 builder.Services.AddCorsInstall(); var app = builder.Build(); //跨域 var policyName = AppSetting.app(new string[] { "Cors", "PolicyName" }); if (!policyName.IsNullOrEmpty()) { app.UseCors(policyName); //添加跨域中间件 } // Configure the HTTP request pipeline. // if (app.Environment.IsDevelopment()) // { var apis = new List { "WmsMainAPI", "WmsAdminAPI", "WmsOpAPI", "WmsFeeAPI", "WmsCheckAPI", "WmsFinanceAPI", "TestApi" }; app .UseSwagger() .UseSwaggerUI(options => { apis.ForEach(m => { options.SwaggerEndpoint($"/{m}/swagger.json", m); }); }); // } // else // { // app.MapGet("/", () => "Hello World!"); // } app.UseMiddleware(); app.UseAuthentication(); //注意这一句的位置 app.UseAuthorization(); app.MapControllers(); app.UseOcelot().Wait(); //使用Ocelot中间件 app.Run();