Skip to content

Commit

Permalink
add ConfigurSwagger
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Jan 10, 2024
1 parent 1861135 commit 13bfd31
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 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.2</Version>
<Version>0.0.0.3</Version>
<Description>Authentications Microservice</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>module,database,services,asp,aspnet,auth,authentication,authentications</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.2</Version>
<Version>0.0.0.3</Version>
<Description>Authentications Microservice</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>module,domain,services,asp,aspnet,auth,authentication,authentications</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.2</Version>
<Version>0.0.0.3</Version>
<Description>Authentications Microservice</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>module,logics,asp,aspnet,auth,authentication,authentications</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using EasyMicroservices.AuthenticationsMicroservice.WebApi.Controllers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System.Linq;

namespace EasyMicroservices.AuthenticationsMicroservice.Module;
Expand All @@ -15,7 +17,7 @@ public static void ConfigureServices(IServiceCollection services, string prefix
{
mvcBuilder = services.AddControllersWithViews(options =>
{
options.Conventions.Add(new RoutePrefixConvention(new RouteAttribute("authentication")));
options.Conventions.Add(new RoutePrefixConvention(new RouteAttribute(prefix)));
});
}
else
Expand All @@ -24,6 +26,35 @@ public static void ConfigureServices(IServiceCollection services, string prefix
}
mvcBuilder.AddApplicationPart(typeof(RoleController).Assembly);
}

public static void ConfigurSwagger(IServiceCollection services, string name = "Authentication")
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(name, new OpenApiInfo { Title = $"{name} Module", Version = "v1" });
});
services.AddMvc(c =>
{
c.Conventions.Add(new ApiExplorerGroupPerVersionConvention()
{
Prefix = name
});
});
}

public static void ConfigureAll(IServiceCollection services, string prefix = "Authentication")
{
ConfigureServices(services, prefix);
ConfigurSwagger(services, prefix);
}

public static void ConfigurSwagger(WebApplication app, string name = "Authentication")
{
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"/swagger/{name}/swagger.json", $"API Group {name}");
});
}
}

public class RoutePrefixConvention : IApplicationModelConvention
Expand Down Expand Up @@ -57,4 +88,13 @@ public void Apply(ApplicationModel application)
}
}
}
}
public class ApiExplorerGroupPerVersionConvention : IControllerModelConvention
{
public string Prefix { get; set; }
public void Apply(ControllerModel controller)
{
if (controller.ControllerType.Assembly == typeof(RoleController).Assembly)
controller.ApiExplorer.GroupName = Prefix;
}
}
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.2</Version>
<Version>0.0.0.3</Version>
<Description>Authentications Microservice</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>module,services,asp,aspnet,auth,authentication,authentications</PackageTags>
Expand Down

0 comments on commit 13bfd31

Please sign in to comment.