77 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			77 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Asp.Versioning.ApiExplorer;
 | |
| using Microsoft.AspNetCore.Builder;
 | |
| using Microsoft.Extensions.DependencyInjection;
 | |
| using Microsoft.Extensions.Options;
 | |
| using Microsoft.OpenApi.Models;
 | |
| using Mirea.Api.Endpoint.Common.Services;
 | |
| using Mirea.Api.Endpoint.Configuration.SwaggerOptions;
 | |
| using Swashbuckle.AspNetCore.SwaggerGen;
 | |
| using System;
 | |
| using System.IO;
 | |
| 
 | |
| namespace Mirea.Api.Endpoint.Configuration.Core.Startup;
 | |
| 
 | |
| public static class SwaggerConfiguration
 | |
| {
 | |
|     public static IServiceCollection AddCustomSwagger(this IServiceCollection services)
 | |
|     {
 | |
|         services.AddSwaggerGen(options =>
 | |
|         {
 | |
|             options.OperationFilter<TagSchemeFilter>();
 | |
|             options.SchemaFilter<ExampleFilter>();
 | |
|             options.OperationFilter<DefaultValues>();
 | |
|             options.OperationFilter<ActionResultSchemaFilter>();
 | |
|             options.SchemaFilter<EnumSchemaFilter>();
 | |
|             var basePath = AppDomain.CurrentDomain.BaseDirectory;
 | |
| 
 | |
|             options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
 | |
|             {
 | |
|                 In = ParameterLocation.Header,
 | |
|                 Description = "Keep the JWT token in the field (Bearer token)",
 | |
|                 Name = "Authorization",
 | |
|                 Type = SecuritySchemeType.ApiKey
 | |
|             });
 | |
| 
 | |
|             options.AddSecurityRequirement(new OpenApiSecurityRequirement
 | |
|             {
 | |
|                 {
 | |
|                     new OpenApiSecurityScheme
 | |
|                     {
 | |
|                         Reference = new OpenApiReference
 | |
|                         {
 | |
|                             Type = ReferenceType.SecurityScheme,
 | |
|                             Id = "Bearer"
 | |
|                         }
 | |
|                     },
 | |
|                     []
 | |
|                 }
 | |
|             });
 | |
| 
 | |
|             if (File.Exists(Path.Combine(basePath, "docs.xml")))
 | |
|                 options.IncludeXmlComments(Path.Combine(basePath, "docs.xml"));
 | |
| 
 | |
|             if (File.Exists(Path.Combine(basePath, "ApiDtoDocs.xml")))
 | |
|                 options.IncludeXmlComments(Path.Combine(basePath, "ApiDtoDocs.xml"));
 | |
|         });
 | |
| 
 | |
|         return services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
 | |
|     }
 | |
| 
 | |
|     public static IApplicationBuilder UseCustomSwagger(this IApplicationBuilder app, IServiceProvider services)
 | |
|     {
 | |
|         app.UseSwagger();
 | |
|         return app.UseSwaggerUI(options =>
 | |
|         {
 | |
|             options.InjectStylesheet($"{UrlHelper.GetSubPath}css/swagger/SwaggerDark.css");
 | |
|             var provider = services.GetService<IApiVersionDescriptionProvider>();
 | |
| 
 | |
|             foreach (var description in provider!.ApiVersionDescriptions)
 | |
|             {
 | |
|                 var url = $"/swagger/{description.GroupName}/swagger.json";
 | |
|                 var name = description.GroupName.ToUpperInvariant();
 | |
|                 options.SwaggerEndpoint(url, name);
 | |
|                 options.RoutePrefix = UrlHelper.GetSubPathSwagger.Trim('/');
 | |
|             }
 | |
|         });
 | |
|     }
 | |
| } |