|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
using Ocelot.DependencyInjection;
|
|
|
|
using Ocelot.Middleware;
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
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服务
|
|
|
|
//将身份验证服务添加到DI并配置Bearer为默认方案。
|
|
|
|
builder.Services.AddAuthentication("Bearer")
|
|
|
|
.AddJwtBearer("Bearer", options =>
|
|
|
|
{
|
|
|
|
options.Authority = "http://localhost:3005";
|
|
|
|
options.RequireHttpsMetadata = false;
|
|
|
|
options.Audience = "openapi";
|
|
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
// var apis = new List<string> { "swagger" };
|
|
|
|
// app.UseSwagger()
|
|
|
|
// .UseSwaggerUI(options =>
|
|
|
|
// {
|
|
|
|
// apis.ForEach(m =>
|
|
|
|
// {
|
|
|
|
// options.SwaggerEndpoint($"/{m}/v1/swagger.json", m);
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
app.UseAuthentication();//注意这一句的位置
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
app.UseOcelot().Wait();//使用Ocelot中间件
|
|
|
|
|
|
|
|
app.Run();
|