Replaced Swagger and MediatR implementations with inhouse nuget packages.

master
Tudor Stanciu 2020-12-20 23:07:49 +02:00
parent 76045fe755
commit 34228caf2c
16 changed files with 13 additions and 238 deletions

View File

@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="NDB.Application.DataContracts" Version="$(NDBApplicationPackageVersion)" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,4 +1,5 @@
using Chatbot.Api.Application.Events; using Chatbot.Api.Application.Events;
using NDB.Application.DataContracts;
using System; using System;
namespace Chatbot.Api.Application.Commands namespace Chatbot.Api.Application.Commands

View File

@ -1,18 +0,0 @@
using MediatR;
namespace Chatbot.Api.Application.Commands
{
public abstract class Command<TResponse> : ICommand, IRequest<TResponse>
{
public Metadata Metadata { get; }
protected Command(Metadata metadata)
{
Metadata = metadata;
}
}
public interface ICommand
{
}
}

View File

@ -1,25 +0,0 @@
using System;
using System.Collections.Generic;
namespace Chatbot.Api.Application.Commands
{
public class Metadata : Dictionary<string, string>
{
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());
}
}
}
}

View File

@ -1,4 +1,5 @@
using Chatbot.Api.Application.Events; using Chatbot.Api.Application.Events;
using NDB.Application.DataContracts;
using System; using System;
namespace Chatbot.Api.Application.Commands namespace Chatbot.Api.Application.Commands

View File

@ -1,4 +1,5 @@
using Chatbot.Api.Application.Events; using Chatbot.Api.Application.Events;
using NDB.Application.DataContracts;
using System; using System;
namespace Chatbot.Api.Application.Commands namespace Chatbot.Api.Application.Commands

View File

@ -1,6 +1,7 @@
using AutoMapper; using AutoMapper;
using Chatbot.Api.Domain.Repositories; using Chatbot.Api.Domain.Repositories;
using MediatR; using MediatR;
using NDB.Application.DataContracts;
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;

View File

@ -1,6 +1,7 @@
using AutoMapper; using AutoMapper;
using Chatbot.Api.Domain.Repositories; using Chatbot.Api.Domain.Repositories;
using MediatR; using MediatR;
using NDB.Application.DataContracts;
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;

View File

@ -1,6 +1,7 @@
using AutoMapper; using AutoMapper;
using Chatbot.Api.Domain.Repositories; using Chatbot.Api.Domain.Repositories;
using MediatR; using MediatR;
using NDB.Application.DataContracts;
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;

View File

@ -1,8 +0,0 @@
using MediatR;
namespace Chatbot.Api.Application.Queries
{
public abstract class Query<TResponse> : IRequest<TResponse>, IBaseRequest
{
}
}

View File

@ -12,6 +12,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.9.5" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.9.5" />
<PackageReference Include="NDB.Extensions.Swagger" Version="$(NDBExtensionsPackageVersion)" />
<PackageReference Include="Serilog.AspNetCore" Version="$(SerilogPackageVersion)" /> <PackageReference Include="Serilog.AspNetCore" Version="$(SerilogPackageVersion)" />
<PackageReference Include="Serilog.Extensions.Logging" Version="$(SerilogExtensionsPackageVersion)" /> <PackageReference Include="Serilog.Extensions.Logging" Version="$(SerilogExtensionsPackageVersion)" />
<PackageReference Include="Serilog.Sinks.Console" Version="$(SerilogSinksConsolePackageVersion)" /> <PackageReference Include="Serilog.Sinks.Console" Version="$(SerilogSinksConsolePackageVersion)" />

View File

@ -1,7 +1,6 @@
using AutoMapper; using AutoMapper;
using Chatbot.Api.Authentication; using Chatbot.Api.Authentication;
using Chatbot.Api.Domain.Data; using Chatbot.Api.Domain.Data;
using Chatbot.Api.Swagger;
using Chatbot.Application; using Chatbot.Application;
using MediatR; using MediatR;
using MediatR.Pipeline; using MediatR.Pipeline;
@ -11,6 +10,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using NDB.Extensions.Swagger;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Reflection; using System.Reflection;
@ -45,7 +45,7 @@ namespace Chatbot.Api
typeof(Application.Mappings.MappingProfile).Assembly); typeof(Application.Mappings.MappingProfile).Assembly);
// Swagger // Swagger
services.AddSwagger(); services.AddSwagger("Chatbot API");
// Application // Application
services.AddApplicationServices(); services.AddApplicationServices();
@ -75,7 +75,7 @@ namespace Chatbot.Api
{ {
endpoints.MapControllers(); endpoints.MapControllers();
}); });
app.ConfigureSwagger(); app.ConfigureSwagger("Chatbot API");
} }
private Assembly[] GetMediatRAssemblies() private Assembly[] GetMediatRAssemblies()

View File

@ -1,36 +0,0 @@
using Chatbot.Api.Application.Commands;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Linq;
namespace Chatbot.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;
}
}
}
}

View File

@ -1,34 +0,0 @@
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Chatbot.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;
}
}
}
}
}

View File

@ -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 Chatbot.Api.Swagger
{
public static class SwaggerExtensions
{
public static IServiceCollection AddSwagger(this IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1",
new OpenApiInfo
{
Title = "Chatbot 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<string>()
}
});
c.OperationFilter<PathParamsOperationFilter>();
c.SchemaFilter<DtoSchemaFilter>();
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<OpenApiServer>
{
new OpenApiServer {Url = $"{scheme}://{host}{basePath}"}
};
});
c.RouteTemplate = "swagger/{documentName}/swagger.json";
});
applicationBuilder.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("v1/swagger.json", "Chatbot 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;
}
}
}

View File

@ -10,5 +10,7 @@
<MediatRPackageVersion>6.0.0</MediatRPackageVersion> <MediatRPackageVersion>6.0.0</MediatRPackageVersion>
<SwashbucklePackageVersion>5.3.1</SwashbucklePackageVersion> <SwashbucklePackageVersion>5.3.1</SwashbucklePackageVersion>
<EntityFrameworkCorePackageVersion>3.1.3</EntityFrameworkCorePackageVersion> <EntityFrameworkCorePackageVersion>3.1.3</EntityFrameworkCorePackageVersion>
<NDBExtensionsPackageVersion>1.0.0</NDBExtensionsPackageVersion>
<NDBApplicationPackageVersion>1.0.0</NDBApplicationPackageVersion>
</PropertyGroup> </PropertyGroup>
</Project> </Project>