From 2ec9433d0d9547383a7431e97c0577ffcc98ea97 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 7 Jan 2021 20:20:44 +0800 Subject: chore: Move open api setup to its own namespace. --- BackEnd/Timeline/Startup.cs | 34 +++++----------------- .../Swagger/OpenApiServiceCollectionExtensions.cs | 32 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 BackEnd/Timeline/Swagger/OpenApiServiceCollectionExtensions.cs (limited to 'BackEnd') diff --git a/BackEnd/Timeline/Startup.cs b/BackEnd/Timeline/Startup.cs index 70461909..cb99c138 100644 --- a/BackEnd/Timeline/Startup.cs +++ b/BackEnd/Timeline/Startup.cs @@ -8,8 +8,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; -using NSwag; -using NSwag.Generation.Processors.Security; using System; using System.ComponentModel; using System.Net.Mime; @@ -99,6 +97,12 @@ namespace Timeline services.AddScoped(); services.AddScoped(); + services.AddDbContext((services, options) => + { + var pathProvider = services.GetRequiredService(); + options.UseSqlite($"Data Source={pathProvider.GetDatabaseFilePath()}"); + }); + services.AddTransient(); services.AddScoped(); services.AddScoped(); @@ -116,31 +120,7 @@ namespace Timeline services.AddScoped(); services.AddScoped(); - services.AddDbContext((services, options) => - { - var pathProvider = services.GetRequiredService(); - options.UseSqlite($"Data Source={pathProvider.GetDatabaseFilePath()}"); - }); - - services.AddSwaggerDocument(document => - { - document.DocumentName = "Timeline"; - document.Title = "Timeline REST API Reference"; - document.Version = typeof(Startup).Assembly.GetName().Version?.ToString() ?? "unknown version"; - document.DocumentProcessors.Add(new DocumentDescriptionDocumentProcessor()); - document.DocumentProcessors.Add( - new SecurityDefinitionAppender("JWT", - new OpenApiSecurityScheme - { - Type = OpenApiSecuritySchemeType.ApiKey, - Name = "Authorization", - In = OpenApiSecurityApiKeyLocation.Header, - Description = "Create token via `/api/token/create` ." - })); - document.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT")); - document.OperationProcessors.Add(new DefaultDescriptionOperationProcessor()); - document.OperationProcessors.Add(new ByteDataRequestOperationProcessor()); - }); + services.AddOpenApiDocs(); if (_frontEndMode == FrontEndMode.Mock) { diff --git a/BackEnd/Timeline/Swagger/OpenApiServiceCollectionExtensions.cs b/BackEnd/Timeline/Swagger/OpenApiServiceCollectionExtensions.cs new file mode 100644 index 00000000..e1266d7a --- /dev/null +++ b/BackEnd/Timeline/Swagger/OpenApiServiceCollectionExtensions.cs @@ -0,0 +1,32 @@ +using Microsoft.Extensions.DependencyInjection; +using NSwag; +using NSwag.Generation.Processors.Security; + +namespace Timeline.Swagger +{ + public static class OpenApiServiceCollectionExtensions + { + public static void AddOpenApiDocs(this IServiceCollection services) + { + services.AddSwaggerDocument(document => + { + document.DocumentName = "Timeline"; + document.Title = "Timeline REST API Reference"; + document.Version = typeof(Startup).Assembly.GetName().Version?.ToString() ?? "unknown version"; + document.DocumentProcessors.Add(new DocumentDescriptionDocumentProcessor()); + document.DocumentProcessors.Add( + new SecurityDefinitionAppender("JWT", + new OpenApiSecurityScheme + { + Type = OpenApiSecuritySchemeType.ApiKey, + Name = "Authorization", + In = OpenApiSecurityApiKeyLocation.Header, + Description = "Create token via `/api/token/create` ." + })); + document.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT")); + document.OperationProcessors.Add(new DefaultDescriptionOperationProcessor()); + document.OperationProcessors.Add(new ByteDataRequestOperationProcessor()); + }); + } + } +} -- cgit v1.2.3