From 5cc5a5b4a77339e6a084f47b4a09a3aef76901b6 Mon Sep 17 00:00:00 2001 From: Ske Date: Thu, 7 May 2020 05:14:31 +0200 Subject: [PATCH] Add basic Swagger integration into ASP.NET Only enabled in Development mode for now. --- PluralKit.API/PluralKit.API.csproj | 7 ++++++ PluralKit.API/Startup.cs | 39 ++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/PluralKit.API/PluralKit.API.csproj b/PluralKit.API/PluralKit.API.csproj index 62149d74..3f9e60f5 100644 --- a/PluralKit.API/PluralKit.API.csproj +++ b/PluralKit.API/PluralKit.API.csproj @@ -15,6 +15,13 @@ + + + + + + + diff --git a/PluralKit.API/Startup.cs b/PluralKit.API/Startup.cs index cc5fa770..05a2b0d5 100644 --- a/PluralKit.API/Startup.cs +++ b/PluralKit.API/Startup.cs @@ -1,13 +1,15 @@ -using System; +using System; using Autofac; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.OpenApi.Models; using PluralKit.Core; @@ -35,11 +37,37 @@ namespace PluralKit.API c.AssumeDefaultVersionWhenUnspecified = true; c.DefaultApiVersion = ApiVersion.Parse("1.0"); }); + + services.AddVersionedApiExplorer(c => + { + c.GroupNameFormat = "'v'VV"; + c.DefaultApiVersion = ApiVersion.Parse("1.0"); + c.ApiVersionParameterSource = new UrlSegmentApiVersionReader(); + c.SubstituteApiVersionInUrl = true; + }); + + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1.0", new OpenApiInfo {Title = "PluralKit", Version = "1.0"}); + + c.EnableAnnotations(); + c.AddSecurityDefinition("TokenAuth", + new OpenApiSecurityScheme {Name = "Authorization", Type = SecuritySchemeType.ApiKey}); + + // Exclude routes without a version, then fall back to group name matching (default behavior) + c.DocInclusionPredicate((docName, apiDesc) => + { + if (!apiDesc.RelativePath.StartsWith("v1/")) return false; + return apiDesc.GroupName == docName; + }); + }); + services.AddSwaggerGenNewtonsoftSupport(); } public void ConfigureContainer(ContainerBuilder builder) { - builder.RegisterInstance(InitUtils.BuildConfiguration(Environment.GetCommandLineArgs()).Build()).As(); + builder.RegisterInstance(InitUtils.BuildConfiguration(Environment.GetCommandLineArgs()).Build()) + .As(); builder.RegisterModule(new ConfigModule()); builder.RegisterModule(new LoggingModule("api")); builder.RegisterModule(new MetricsModule("API")); @@ -55,6 +83,13 @@ namespace PluralKit.API if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); + + // Only enable Swagger stuff when ASPNETCORE_ENVIRONMENT=Development (for now) + app.UseSwagger(); + app.UseSwaggerUI(c => + { + c.SwaggerEndpoint("/swagger/v1.0/swagger.json", "PluralKit (v1)"); + }); } else {