aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-01-07 20:20:44 +0800
committercrupest <crupest@outlook.com>2021-01-07 20:20:44 +0800
commitd5c2e1bd1573325e5585f5cf7279bd7592dbfeee (patch)
treeccf114de32f524fe3553968e0aef5fb9e28f8c0f
parent8efb9dc9bec7e52aa76a741d820bdeaf9eb45fec (diff)
downloadtimeline-d5c2e1bd1573325e5585f5cf7279bd7592dbfeee.tar.gz
timeline-d5c2e1bd1573325e5585f5cf7279bd7592dbfeee.tar.bz2
timeline-d5c2e1bd1573325e5585f5cf7279bd7592dbfeee.zip
chore: Move open api setup to its own namespace.
-rw-r--r--BackEnd/Timeline/Startup.cs34
-rw-r--r--BackEnd/Timeline/Swagger/OpenApiServiceCollectionExtensions.cs32
2 files changed, 39 insertions, 27 deletions
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<IDataManager, DataManager>();
services.AddScoped<IImageValidator, ImageValidator>();
+ services.AddDbContext<DatabaseContext>((services, options) =>
+ {
+ var pathProvider = services.GetRequiredService<IPathProvider>();
+ options.UseSqlite($"Data Source={pathProvider.GetDatabaseFilePath()}");
+ });
+
services.AddTransient<IPasswordService, PasswordService>();
services.AddScoped<IBasicUserService, BasicUserService>();
services.AddScoped<IUserService, UserService>();
@@ -116,31 +120,7 @@ namespace Timeline
services.AddScoped<IHighlightTimelineService, HighlightTimelineService>();
services.AddScoped<IBookmarkTimelineService, BookmarkTimelineService>();
- services.AddDbContext<DatabaseContext>((services, options) =>
- {
- var pathProvider = services.GetRequiredService<IPathProvider>();
- 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());
+ });
+ }
+ }
+}