support config options file

This commit is contained in:
igeekfan 2021-12-16 21:58:32 +08:00
parent 233268c5e3
commit 03ee5dadb0

View File

@ -2,29 +2,36 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Hosting;
#if NETSTANDARD2_0
using IWebHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
#endif
namespace IGeekFan.AspNetCore.Knife4jUI
{
public static class Knife4UIBuilderExtensions
{
public static IApplicationBuilder UseKnife4UI(this IApplicationBuilder app, Knife4UIOptions options)
{
return app.UseMiddleware<Knife4jUIMiddleware>(options);
}
public static IApplicationBuilder UseKnife4UI(
this IApplicationBuilder app,
Action<Knife4UIOptions> setupAction = null)
{
var options = new Knife4UIOptions();
if (setupAction != null)
using (var scope = app.ApplicationServices.CreateScope())
{
setupAction(options);
options = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<Knife4UIOptions>>().Value;
setupAction?.Invoke(options);
}
else
{
options = app.ApplicationServices.GetRequiredService<IOptions<Knife4UIOptions>>().Value;
}
app.UseMiddleware<Knife4jUIMiddleware>(options);
return app;
if (options.ConfigObject.Urls == null)
{
var hostingEnv = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
options.ConfigObject.Urls = new[] { new UrlDescriptor { Name = $"{hostingEnv.ApplicationName} v1", Url = "v1/swagger.json" } };
}
return app.UseKnife4UI(options);
}
}
}