From e3ce6fc6947d7c3712c2edd0abc108c6312bd744 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sun, 20 Dec 2020 23:00:04 +0200 Subject: [PATCH] Replaced Swagger and MediatR implementations with inhouse nuget packages --- Directory.Build.props | 2 +- .../NetworkResurrector.Api.csproj | 1 + NetworkResurrector.Api/Startup.cs | 6 +- .../Swagger/DtoSchemaFilter.cs | 36 ------ .../Swagger/PathParamsOperationFilter.cs | 34 ------ .../Swagger/SwaggerExtensions.cs | 114 ------------------ .../Commands/Command.cs | 18 --- .../Commands/Metadata.cs | 25 ---- .../Commands/PingMachine.cs | 3 +- .../Commands/ShutdownMachine.cs | 3 +- .../Commands/WakeMachine.cs | 3 +- .../NetworkResurrector.Application.csproj | 1 + .../Queries/GetToken.cs | 1 + .../Queries/Query.cs | 6 - NetworkResurrector.sln | 1 + NuGet.config | 8 ++ ReleaseNotes.xml | 6 + dependencies.props | 2 + 18 files changed, 30 insertions(+), 240 deletions(-) delete mode 100644 NetworkResurrector.Api/Swagger/DtoSchemaFilter.cs delete mode 100644 NetworkResurrector.Api/Swagger/PathParamsOperationFilter.cs delete mode 100644 NetworkResurrector.Api/Swagger/SwaggerExtensions.cs delete mode 100644 NetworkResurrector.Application/Commands/Command.cs delete mode 100644 NetworkResurrector.Application/Commands/Metadata.cs delete mode 100644 NetworkResurrector.Application/Queries/Query.cs create mode 100644 NuGet.config diff --git a/Directory.Build.props b/Directory.Build.props index 55a8528..82a33f8 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 1.0.0.1 + 1.0.2.0 Tudor Stanciu STA NetworkResurrector diff --git a/NetworkResurrector.Api/NetworkResurrector.Api.csproj b/NetworkResurrector.Api/NetworkResurrector.Api.csproj index 6a216f4..ef3c70e 100644 --- a/NetworkResurrector.Api/NetworkResurrector.Api.csproj +++ b/NetworkResurrector.Api/NetworkResurrector.Api.csproj @@ -11,6 +11,7 @@ + diff --git a/NetworkResurrector.Api/Startup.cs b/NetworkResurrector.Api/Startup.cs index 61bc16b..4698504 100644 --- a/NetworkResurrector.Api/Startup.cs +++ b/NetworkResurrector.Api/Startup.cs @@ -7,9 +7,9 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using NDB.Extensions.Swagger; using NetworkResurrector.Api.Authentication; using NetworkResurrector.Api.Extensions; -using NetworkResurrector.Api.Swagger; using NetworkResurrector.Application; using Newtonsoft.Json; using System.Reflection; @@ -45,7 +45,7 @@ namespace NetworkResurrector.Api typeof(Application.Mappings.MappingProfile).Assembly); // Swagger - services.AddSwagger(); + services.AddSwagger("NetworkResurrector API"); // Application services.AddApplicationServices(); @@ -75,7 +75,7 @@ namespace NetworkResurrector.Api { endpoints.MapControllers(); }); - app.ConfigureSwagger(); + app.ConfigureSwagger("NetworkResurrector API"); } private Assembly[] GetMediatRAssemblies() diff --git a/NetworkResurrector.Api/Swagger/DtoSchemaFilter.cs b/NetworkResurrector.Api/Swagger/DtoSchemaFilter.cs deleted file mode 100644 index e833c90..0000000 --- a/NetworkResurrector.Api/Swagger/DtoSchemaFilter.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Microsoft.OpenApi.Models; -using NetworkResurrector.Application.Commands; -using Swashbuckle.AspNetCore.SwaggerGen; -using System.Linq; - -namespace NetworkResurrector.Api.Swagger -{ - public class DtoSchemaFilter : ISchemaFilter - { - public void Apply(OpenApiSchema schema, SchemaFilterContext context) - { - var targetType = context.Type; - while (targetType != null) - { - if (typeof(ICommand).IsAssignableFrom(targetType)) - { - foreach (var property in schema.Properties.ToList()) - { - property.Value.ReadOnly = false; - - switch (property.Key) - { - case "metadata": - schema.Properties.Remove(property.Key); - break; - default: - break; - } - } - } - - targetType = targetType.DeclaringType; - } - } - } -} diff --git a/NetworkResurrector.Api/Swagger/PathParamsOperationFilter.cs b/NetworkResurrector.Api/Swagger/PathParamsOperationFilter.cs deleted file mode 100644 index ad82995..0000000 --- a/NetworkResurrector.Api/Swagger/PathParamsOperationFilter.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Microsoft.OpenApi.Models; -using Swashbuckle.AspNetCore.SwaggerGen; -using System; -using System.Linq; -using System.Text.RegularExpressions; - -namespace NetworkResurrector.Api.Swagger -{ - public class PathParamsOperationFilter : IOperationFilter - { - public void Apply(OpenApiOperation operation, OperationFilterContext context) - { - const string paramCaptureGroup = "param"; - - var openApiPathParameters = operation.Parameters.Where(param => param.In == ParameterLocation.Path).ToList(); - var pathParamRegEx = $@"\{{(?<{paramCaptureGroup}>[^\}}]+)\}}"; - - if (openApiPathParameters.Any()) - { - var pathParameterMatches = Regex.Matches(context.ApiDescription.RelativePath, pathParamRegEx, RegexOptions.Compiled); - var pathParameters = pathParameterMatches.Select(x => x.Groups[paramCaptureGroup].Value); - - foreach (var openApiPathParameter in openApiPathParameters) - { - var correspondingPathParameter = pathParameters.FirstOrDefault(x => - string.Equals(x, openApiPathParameter.Name, StringComparison.InvariantCultureIgnoreCase)); - - if (correspondingPathParameter != null) - openApiPathParameter.Name = correspondingPathParameter; - } - } - } - } -} diff --git a/NetworkResurrector.Api/Swagger/SwaggerExtensions.cs b/NetworkResurrector.Api/Swagger/SwaggerExtensions.cs deleted file mode 100644 index 8603575..0000000 --- a/NetworkResurrector.Api/Swagger/SwaggerExtensions.cs +++ /dev/null @@ -1,114 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.OpenApi.Models; -using System.Collections.Generic; -using System.Linq; - -namespace NetworkResurrector.Api.Swagger -{ - public static class SwaggerExtensions - { - public static IServiceCollection AddSwagger(this IServiceCollection services) - { - services.AddSwaggerGen(c => - { - c.SwaggerDoc("v1", - new OpenApiInfo - { - Title = "NetworkResurrector API", - Version = "v1" - }); - - c.AddSecurityDefinition("Basic", - new OpenApiSecurityScheme - { - In = ParameterLocation.Header, - Description = @"JWT Authorization header using the Basic scheme. Enter 'Basic' [space] and then your token in the text input below. Example: 'Basic 12345abcdef'", - Name = "Authorization", - Scheme = "Basic", - Type = SecuritySchemeType.ApiKey - }); - - c.AddSecurityRequirement(new OpenApiSecurityRequirement() - { - { - new OpenApiSecurityScheme - { - Reference = new OpenApiReference - { - Type = ReferenceType.SecurityScheme, - Id = "Basic" - }, - Scheme = "Basic", - Name = "Authorization", - In = ParameterLocation.Header - }, - new List() - } - }); - - c.OperationFilter(); - c.SchemaFilter(); - c.CustomSchemaIds(type => type.ToString()); - }); - - return services; - } - - public static IApplicationBuilder ConfigureSwagger(this IApplicationBuilder applicationBuilder) - { - applicationBuilder.UseSwagger(c => - { - c.PreSerializeFilters.Add((swagger, httpRequest) => - { - var (host, basePath, scheme) = GetUrlComponents(httpRequest); - - swagger.Servers = new List - { - new OpenApiServer {Url = $"{scheme}://{host}{basePath}"} - }; - }); - c.RouteTemplate = "swagger/{documentName}/swagger.json"; - }); - - applicationBuilder.UseSwaggerUI(c => - { - c.SwaggerEndpoint("v1/swagger.json", "NetworkResurrector API"); - c.RoutePrefix = $"swagger"; - }); - - return applicationBuilder; - } - - private static (string host, string basePath, string scheme) GetUrlComponents(HttpRequest request) - { - var host = ExtractHost(request); - var basePath = ExtractBasePath(request); - var scheme = ExtractScheme(request); - - return (host, basePath, scheme); - } - - private static string ExtractHost(HttpRequest request) - { - if (request.Headers.ContainsKey("X-Forwarded-Host")) - return request.Headers["X-Forwarded-Host"].First(); - - return request.Host.Value; - } - - private static string ExtractBasePath(HttpRequest request) - { - if (request.Headers.ContainsKey("X-Forwarded-PathBase")) - return request.Headers["X-Forwarded-PathBase"].First(); - - return string.Empty; - } - - private static string ExtractScheme(HttpRequest request) - { - return request.Headers["X-Forwarded-Proto"].FirstOrDefault() ?? request.Scheme; - } - } -} diff --git a/NetworkResurrector.Application/Commands/Command.cs b/NetworkResurrector.Application/Commands/Command.cs deleted file mode 100644 index f22f4b2..0000000 --- a/NetworkResurrector.Application/Commands/Command.cs +++ /dev/null @@ -1,18 +0,0 @@ -using MediatR; - -namespace NetworkResurrector.Application.Commands -{ - public abstract class Command : ICommand, IRequest - { - public Metadata Metadata { get; } - - protected Command(Metadata metadata) - { - Metadata = metadata; - } - } - - public interface ICommand - { - } -} diff --git a/NetworkResurrector.Application/Commands/Metadata.cs b/NetworkResurrector.Application/Commands/Metadata.cs deleted file mode 100644 index 421a5d6..0000000 --- a/NetworkResurrector.Application/Commands/Metadata.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace NetworkResurrector.Application.Commands -{ - public class Metadata : Dictionary - { - public const string CorrelationIdKey = "CorrelationId"; - - public Guid CorrelationId - { - get - { - return Guid.Parse(this[CorrelationIdKey]); - } - set - { - if (ContainsKey(CorrelationIdKey)) - this[CorrelationIdKey] = value.ToString(); - else - Add(CorrelationIdKey, value.ToString()); - } - } - } -} diff --git a/NetworkResurrector.Application/Commands/PingMachine.cs b/NetworkResurrector.Application/Commands/PingMachine.cs index bde8a81..b5c9a08 100644 --- a/NetworkResurrector.Application/Commands/PingMachine.cs +++ b/NetworkResurrector.Application/Commands/PingMachine.cs @@ -1,4 +1,5 @@ -using NetworkResurrector.Application.Events; +using NDB.Application.DataContracts; +using NetworkResurrector.Application.Events; using System; namespace NetworkResurrector.Application.Commands diff --git a/NetworkResurrector.Application/Commands/ShutdownMachine.cs b/NetworkResurrector.Application/Commands/ShutdownMachine.cs index cc3d32d..078ec2c 100644 --- a/NetworkResurrector.Application/Commands/ShutdownMachine.cs +++ b/NetworkResurrector.Application/Commands/ShutdownMachine.cs @@ -1,4 +1,5 @@ -using NetworkResurrector.Application.Events; +using NDB.Application.DataContracts; +using NetworkResurrector.Application.Events; using System; namespace NetworkResurrector.Application.Commands diff --git a/NetworkResurrector.Application/Commands/WakeMachine.cs b/NetworkResurrector.Application/Commands/WakeMachine.cs index 2ace6a3..375a6f8 100644 --- a/NetworkResurrector.Application/Commands/WakeMachine.cs +++ b/NetworkResurrector.Application/Commands/WakeMachine.cs @@ -1,4 +1,5 @@ -using NetworkResurrector.Application.Events; +using NDB.Application.DataContracts; +using NetworkResurrector.Application.Events; using System; namespace NetworkResurrector.Application.Commands diff --git a/NetworkResurrector.Application/NetworkResurrector.Application.csproj b/NetworkResurrector.Application/NetworkResurrector.Application.csproj index a7efcd2..784cfbe 100644 --- a/NetworkResurrector.Application/NetworkResurrector.Application.csproj +++ b/NetworkResurrector.Application/NetworkResurrector.Application.csproj @@ -11,6 +11,7 @@ + diff --git a/NetworkResurrector.Application/Queries/GetToken.cs b/NetworkResurrector.Application/Queries/GetToken.cs index 38c59f7..7d535f8 100644 --- a/NetworkResurrector.Application/Queries/GetToken.cs +++ b/NetworkResurrector.Application/Queries/GetToken.cs @@ -1,5 +1,6 @@ using AutoMapper; using MediatR; +using NDB.Application.DataContracts; using NetworkResurrector.Application.Services; using System; using System.Threading; diff --git a/NetworkResurrector.Application/Queries/Query.cs b/NetworkResurrector.Application/Queries/Query.cs deleted file mode 100644 index 2341bb4..0000000 --- a/NetworkResurrector.Application/Queries/Query.cs +++ /dev/null @@ -1,6 +0,0 @@ -using MediatR; - -namespace NetworkResurrector.Application.Queries -{ - public abstract class Query : IRequest, IBaseRequest { } -} diff --git a/NetworkResurrector.sln b/NetworkResurrector.sln index d5b3135..a6df37e 100644 --- a/NetworkResurrector.sln +++ b/NetworkResurrector.sln @@ -12,6 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution dependencies.props = dependencies.props Directory.Build.props = Directory.Build.props Notes.txt = Notes.txt + NuGet.config = NuGet.config ReleaseNotes.xml = ReleaseNotes.xml EndProjectSection EndProject diff --git a/NuGet.config b/NuGet.config new file mode 100644 index 0000000..fdd86c4 --- /dev/null +++ b/NuGet.config @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/ReleaseNotes.xml b/ReleaseNotes.xml index 8099706..8ecddd5 100644 --- a/ReleaseNotes.xml +++ b/ReleaseNotes.xml @@ -13,4 +13,10 @@ Has been added "ping" and "shutdown" support. Routes with same names have also been added to the controller. + + 1.0.2.0 + + Replaced Swagger and MediatR implementations with inhouse nuget packages. + + \ No newline at end of file diff --git a/dependencies.props b/dependencies.props index 40ace42..d2e9092 100644 --- a/dependencies.props +++ b/dependencies.props @@ -9,5 +9,7 @@ 7.0.0 6.0.0 5.3.1 + 1.0.0 + 1.0.0 \ No newline at end of file