From 088b8c17fe76f312e6cadcaa57d4c1d0b14a2af3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 14 Jan 2021 07:09:09 -0600 Subject: add options for swagger --- src/slskd/Startup.cs | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index 97b306ef..83be2e91 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -49,6 +49,8 @@ internal static int RoomMessageLimit { get; set; } internal static int ReadBufferSize { get; set; } internal static int WriteBufferSize { get; set; } + internal static bool EnableSwagger { get; set; } + internal static string XmlDocFile { get; set; } internal static SymmetricSecurityKey JwtSigningKey { get; set; } @@ -78,6 +80,8 @@ RoomMessageLimit = Configuration.GetValue("ROOM_MESSAGE_LIMIT", 250); ReadBufferSize = Configuration.GetValue("READ_BUFFER_SIZE", 16384); WriteBufferSize = Configuration.GetValue("WRITE_BUFFER_SIZE", 16384); + EnableSwagger = Configuration.GetValue("ENABLE_SWAGGER", true); + XmlDocFile = Configuration.GetValue("XML_DOC_FILE", Path.Combine(AppContext.BaseDirectory, typeof(Startup).GetTypeInfo().Assembly.GetName().Name + ".xml")); JwtSigningKey = new SymmetricSecurityKey(PBKDF2.GetKey(Password)); @@ -139,19 +143,25 @@ options.SubstituteApiVersionInUrl = true; }); - services.AddSwaggerGen(options => + if (EnableSwagger) { - options.DescribeAllParametersInCamelCase(); - options.SwaggerDoc("v0", - new OpenApiInfo + services.AddSwaggerGen(options => + { + options.DescribeAllParametersInCamelCase(); + options.SwaggerDoc("v0", + new OpenApiInfo + { + Title = "slskd", + Version = "v0" + } + ); + + if (System.IO.File.Exists(XmlDocFile)) { - Title = "slskd", - Version = "v0" + options.IncludeXmlComments(XmlDocFile); } - ); - - options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, typeof(Startup).GetTypeInfo().Assembly.GetName().Name + ".xml")); - }); + }); + } services.AddSingleton(serviceProvider => Client); services.AddSingleton(); @@ -212,9 +222,12 @@ app.UseAuthentication(); app.UseMvc(); - app.UseSwagger(); - app.UseSwaggerUI(options => provider.ApiVersionDescriptions.ToList() - .ForEach(description => options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName))); + if (EnableSwagger) + { + app.UseSwagger(); + app.UseSwaggerUI(options => provider.ApiVersionDescriptions.ToList() + .ForEach(description => options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName))); + } // if we made it this far and the route still wasn't matched, return the index // this is required so that SPA routing (React Router, etc) can work properly -- cgit v1.2.3