是否可以为SwaggerUI动态添加SwaggerEndpoint?

问题描述 投票:4回答:1

我们正在.NET Core中构建面向服务的体系结构。我们已经决定使用Ocelot作为我们的API网关。我已经将Ocelot与Consul集成在一起以进行服务发现。现在,我尝试为所有下游服务创建一个统一的Swagger UI。

在发现服务之前,我们需要像这样设置Swagger:

// Enable middleware to serve generated Swagger as a JSON endpoint
app.UseSwagger(c => { c.RouteTemplate = "{documentName}/swagger.json"; });

// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
app.UseSwaggerUI(c =>
{
  c.SwaggerEndpoint("/docs/customer/swagger.json", "Customers Api Doc");
  c.SwaggerEndpoint("/docs/employee/swagger.json", "Employee Api Doc");
  c.SwaggerEndpoint("/docs/report/swagger.json", "Reports Api Doc");
});

在Swagger用户界面上,这提供了“选择规格”下拉菜单。开发人员喜欢此功能,我们希望保留它。但是,由于我们已经删除了手动配置以支持服务发现,因此我们还希望动态更新这些端点。

使用当前可用的Swagger解决方案,这可能吗?我还没有看到任何有关服务发现或能够动态配置UI的信息。有意见和建议吗?

更新

我想出了一种方法。这有点hacking,我希望有一种方法可以使这种方法不那么费力。

public class Startup 
{
    static object LOCK = new object();

    SwaggerUIOptions options;

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<SwaggerUIOptions>((provider) =>
        {
            return this.options;
        });
        services.AddSingleton<IHostedService, SwaggerUIDocsAggregator>();
        services.AddSingleton<IConsulDiscoveryService, MyCounsulDiscoveryServiceImplementation>();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // Enable middleware to serve generated Swagger as a JSON endpoint
        app.UseSwagger(c => { c.RouteTemplate = "{documentName}/swagger.json"; });
        // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
        app.UseSwaggerUI(c =>
        {
            this.options = c;
        });
    }
}

public class SwaggerUIDocsAggregator : IHostedService
{
    static object LOCK = new object();

    IConsulDiscoveryService discoveryService;
    SwaggerUIOptions options;
    Timer timer;
    bool polling = false;
    int pollingInterval = 600;

    public ConsulHostedService(IConsulDiscoveryService discoveryService, SwaggerUIOptions options)
    {
        this.discoveryService = discoveryService;
        this.options = options;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        this.timer = new Timer(async x =>
        {
            if (this.polling)
            {
                return;
            }

            lock (LOCK)
            {
                this.polling = true;
            }

            await this.UpdateDocs();

            lock (LOCK)
            {
                this.polling = false;
            }

        }, null, 0, pollingInterval);
    }

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        this.timer.Dispose();
        this.timer = null;
    }

    private async Task UpdateDocs()
    {
        var discoveredServices = await this.discoveryService.LookupServices();

        var urls = new JArray();

        foreach (var kvp in discoveredServices)
        {
            var serviceName = kvp.Key;

            if (!urls.Any(u => (u as JObject).GetValue("url").Value<string>().Equals($"/{serviceName}/docs/swagger.json")))
            {

                urls.Add(JObject.FromObject(new { url = $"/{serviceName}/docs/swagger.json", name = serviceName }));
            }
        }

        this.options.ConfigObject["urls"] = urls;
    }
}
asp.net-core swagger swagger-2.0 service-discovery ocelot
1个回答
0
投票

将Ocelot api网关集成为所有下游服务的统一Swagger UI的简便方法是项目MMLib.SwaggerForOcelot。>>

© www.soinside.com 2019 - 2024. All rights reserved.