| | 1 | | using System.Reflection; |
| | 2 | | using Microsoft.OpenApi.Any; |
| | 3 | | using Microsoft.OpenApi.Models; |
| | 4 | | using Microsoft.OpenApi.Interfaces; |
| | 5 | |
|
| | 6 | | namespace Syki.Back.Configs; |
| | 7 | |
|
| | 8 | | public static class DocsConfigs |
| | 9 | | { |
| | 10 | | public static void AddDocsConfigs(this IServiceCollection services) |
| | 11 | | { |
| 1 | 12 | | services.AddSwaggerGen(options => |
| 1 | 13 | | { |
| 1 | 14 | | options.SwaggerDoc("v1", new OpenApiInfo |
| 1 | 15 | | { |
| 1 | 16 | | Description = ReadResource("api-intro.md"), |
| 1 | 17 | | Extensions = new Dictionary<string, IOpenApiExtension> |
| 1 | 18 | | { |
| 1 | 19 | | { "x-logo", new OpenApiObject |
| 1 | 20 | | { |
| 1 | 21 | | { "url", new OpenApiString("/syki-logo.png") }, |
| 1 | 22 | | }} |
| 1 | 23 | | }, |
| 1 | 24 | | }); |
| 1 | 25 | |
|
| 1 | 26 | | options.ExampleFilters(); |
| 1 | 27 | |
|
| 1 | 28 | | options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme |
| 1 | 29 | | { |
| 1 | 30 | | In = ParameterLocation.Header, |
| 1 | 31 | | Description = "Please enter a valid token", |
| 1 | 32 | | Name = "Authorization", |
| 1 | 33 | | Type = SecuritySchemeType.Http, |
| 1 | 34 | | BearerFormat = "JWT", |
| 1 | 35 | | Scheme = "Bearer" |
| 1 | 36 | | }); |
| 1 | 37 | | options.AddSecurityRequirement(new OpenApiSecurityRequirement |
| 1 | 38 | | { |
| 1 | 39 | | { |
| 1 | 40 | | new OpenApiSecurityScheme |
| 1 | 41 | | { |
| 1 | 42 | | Reference = new OpenApiReference |
| 1 | 43 | | { |
| 1 | 44 | | Type = ReferenceType.SecurityScheme, |
| 1 | 45 | | Id = "Bearer" |
| 1 | 46 | | } |
| 1 | 47 | | }, |
| 1 | 48 | | new string[] {} |
| 1 | 49 | | } |
| 1 | 50 | | }); |
| 1 | 51 | |
|
| 1 | 52 | | options.DescribeAllParametersInCamelCase(); |
| 1 | 53 | |
|
| 1 | 54 | | var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; |
| 1 | 55 | | var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); |
| 1 | 56 | | options.IncludeXmlComments(xmlPath, includeControllerXmlComments: true); |
| 2 | 57 | | }); |
| | 58 | |
|
| 1 | 59 | | services.AddSwaggerExamplesFromAssemblyOf(typeof(Program)); |
| 1 | 60 | | } |
| | 61 | |
|
| | 62 | | public static void UseDocs(this IApplicationBuilder app) |
| | 63 | | { |
| 1 | 64 | | app.UseStaticFiles(); |
| | 65 | |
|
| 1 | 66 | | app.UseSwagger(); |
| 1 | 67 | | app.UseSwaggerUI(options => |
| 1 | 68 | | { |
| 1 | 69 | | options.DocumentTitle = "Syki API"; |
| 1 | 70 | | options.SwaggerEndpoint("/swagger/v1/swagger.json", "Syki 1.0"); |
| 1 | 71 | | options.DefaultModelsExpandDepth(-1); |
| 2 | 72 | | }); |
| | 73 | |
|
| 1 | 74 | | app.UseReDoc(c => |
| 1 | 75 | | { |
| 1 | 76 | | c.RoutePrefix = "docs"; |
| 1 | 77 | | c.DocumentTitle = "Syki API"; |
| 1 | 78 | | c.SpecUrl = "/swagger/v1/swagger.json"; |
| 2 | 79 | | }); |
| 1 | 80 | | } |
| | 81 | |
|
| | 82 | | public static string ReadResource(string name) |
| | 83 | | { |
| 1 | 84 | | var assembly = Assembly.GetExecutingAssembly(); |
| 2 | 85 | | var resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(name)); |
| | 86 | |
|
| 1 | 87 | | using Stream stream = assembly.GetManifestResourceStream(resourcePath)!; |
| 1 | 88 | | using StreamReader reader = new(stream); |
| | 89 | |
|
| 1 | 90 | | return reader.ReadToEnd(); |
| 1 | 91 | | } |
| | 92 | | } |