Skip to content

Commit

Permalink
Merge pull request #132 from Ali-YousefiTelori/develop
Browse files Browse the repository at this point in the history
configure UseSwaggerUI
  • Loading branch information
Ali-YousefiTelori authored Jan 10, 2024
2 parents a4710bd + 9ac4a8b commit 25adb93
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.95</Version>
<Version>0.0.0.96</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.95</Version>
<Version>0.0.0.96</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet,aspcore,efcore</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -140,6 +141,21 @@ public static IServiceCollection UseActivityChangeLog(this IServiceCollection se
return services;
}


static void UseSwaggerUI(IConfiguration config,Func<Action<SwaggerUIOptions>,IApplicationBuilder> swagger)
{
var ui = config.GetSection("Swagger:SwaggerUI").Get<SwaggerUIConfigInfo>();
swagger(so =>
{
if (ui.Endpoints?.Length > 0)
{
foreach (var item in ui.Endpoints)
{
so.SwaggerEndpoint(item.Url, item.Name);
}
}
});
}
/// <summary>
///
/// </summary>
Expand All @@ -149,16 +165,22 @@ public static IServiceCollection UseActivityChangeLog(this IServiceCollection se
public static async Task Build<TContext>(this IApplicationBuilder app)
where TContext : RelationalCoreContext
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
app.UseDeveloperExceptionPage();
// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();

var useSwagger = config["Authorization:IsUse"];
var doUseSwagger = useSwagger.HasValue() && useSwagger.Equals("true", StringComparison.OrdinalIgnoreCase);
if (doUseSwagger)
{
app.UseSwagger();
UseSwaggerUI(config, app.UseSwaggerUI);
}

app.UseHttpsRedirection();
app.UseAuthorization();

IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
//app.MapControllers();
//app.Run(build);
using (var scope = app.ApplicationServices.CreateAsyncScope())
Expand Down Expand Up @@ -191,7 +213,7 @@ public static WebApplication Build(this WebApplicationBuilder app)

static bool PreBuild(WebApplicationBuilder app)
{
var useAuth = app.Configuration["Authorization:Use"];
var useAuth = app.Configuration["Authorization:IsUse"];
var useAuthorization = useAuth.HasValue() && useAuth.Equals("true", StringComparison.OrdinalIgnoreCase);
if (useAuthorization)
{
Expand Down Expand Up @@ -293,16 +315,21 @@ public static async Task<WebApplication> Use<TContext>(this WebApplication webAp
{
webApplication.UseMiddleware<AppAuthorizationMiddleware>();

// Configure the HTTP request pipeline.
webApplication.UseSwagger();
webApplication.UseSwaggerUI();

webApplication.UseHttpsRedirection();
webApplication.UseAuthorization();
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();

var useSwagger = config["Authorization:IsUse"];
var doUseSwagger = useSwagger.HasValue() && useSwagger.Equals("true", StringComparison.OrdinalIgnoreCase);
if (doUseSwagger)
{
webApplication.UseSwagger();
UseSwaggerUI(config, webApplication.UseSwaggerUI);
}

webApplication.UseHttpsRedirection();
webApplication.UseAuthorization();

using (var scope = webApplication.Services.CreateAsyncScope())
{
var dbbuilder = new DatabaseCreator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.95</Version>
<Version>0.0.0.96</Version>
<Description>core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,client,clients</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net45;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.95</Version>
<Version>0.0.0.96</Version>
<Description>core contracts.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,contract,contracts,dto,dtos</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace EasyMicroservices.Cores.Models;

/// <summary>
///
/// </summary>
public class SwaggerUIConfigInfo
{
/// <summary>
///
/// </summary>
public SwaggerUIEndpointInfo[] Endpoints { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace EasyMicroservices.Cores.Models;

/// <summary>
///
/// </summary>
public class SwaggerUIEndpointInfo
{
/// <summary>
///
/// </summary>
public string Name { get; set; }
/// <summary>
///
/// </summary>
public string Url { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net45;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.95</Version>
<Version>0.0.0.96</Version>
<Description>core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.95</Version>
<Version>0.0.0.96</Version>
<Description>ef core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.95</Version>
<Version>0.0.0.96</Version>
<Description>ef core of Relational database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore,Relational</PackageTags>
Expand Down

0 comments on commit 25adb93

Please sign in to comment.