From fb2088a9d742c01ede5318a6eee6b50b28eb77be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Wed, 18 Mar 2020 15:36:14 +0100 Subject: [PATCH] #255 - Returning API version in every response as header. --- .../Models/Api/VersionHeader.cs | 14 ++++++++ .../ExceptionMiddlewareExceptions.cs | 3 +- .../Common/Middlewares/VersionMiddleware.cs | 32 +++++++++++++++++++ .../VersionMiddlewareExtensions.cs | 27 ++++++++++++++++ .../Middlewares/VersionMiddlewareOptions.cs | 15 +++++++++ src/Money.Api/Startup.cs | 8 ++++- 6 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/Money.Api.Shared/Models/Api/VersionHeader.cs create mode 100644 src/Money.Api/Common/Middlewares/VersionMiddleware.cs create mode 100644 src/Money.Api/Common/Middlewares/VersionMiddlewareExtensions.cs create mode 100644 src/Money.Api/Common/Middlewares/VersionMiddlewareOptions.cs diff --git a/src/Money.Api.Shared/Models/Api/VersionHeader.cs b/src/Money.Api.Shared/Models/Api/VersionHeader.cs new file mode 100644 index 00000000..f1d4c1ff --- /dev/null +++ b/src/Money.Api.Shared/Models/Api/VersionHeader.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Money.Models.Api +{ + public class VersionHeader + { + public const string Name = "X-Api-Version"; + } +} diff --git a/src/Money.Api/Common/Diagnostics/ExceptionMiddlewareExceptions.cs b/src/Money.Api/Common/Diagnostics/ExceptionMiddlewareExceptions.cs index 54f1274f..6a1b63bc 100644 --- a/src/Money.Api/Common/Diagnostics/ExceptionMiddlewareExceptions.cs +++ b/src/Money.Api/Common/Diagnostics/ExceptionMiddlewareExceptions.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Builder; +using Money.Common.Diagnostics; using System; using System.Collections.Generic; using System.IO; @@ -6,7 +7,7 @@ using System.Text; using System.Threading.Tasks; -namespace Money.Common.Diagnostics +namespace Microsoft.AspNetCore.Builder { public static class ExceptionMiddlewareExceptions { diff --git a/src/Money.Api/Common/Middlewares/VersionMiddleware.cs b/src/Money.Api/Common/Middlewares/VersionMiddleware.cs new file mode 100644 index 00000000..e1d987de --- /dev/null +++ b/src/Money.Api/Common/Middlewares/VersionMiddleware.cs @@ -0,0 +1,32 @@ +using Microsoft.AspNetCore.Http; +using Money.Models.Api; +using Neptuo; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Money.Common.Middlewares +{ + public class VersionMiddleware : IMiddleware + { + private readonly Version version; + + public VersionMiddleware(VersionMiddlewareOptions options) + { + Ensure.NotNull(options, "options"); + version = options.AppAssembly.GetName().Version; + } + + public Task InvokeAsync(HttpContext context, RequestDelegate next) + { + if (version != null) + context.Response.Headers[VersionHeader.Name] = version.ToString(4); + + return next(context); + } + } +} diff --git a/src/Money.Api/Common/Middlewares/VersionMiddlewareExtensions.cs b/src/Money.Api/Common/Middlewares/VersionMiddlewareExtensions.cs new file mode 100644 index 00000000..815557b2 --- /dev/null +++ b/src/Money.Api/Common/Middlewares/VersionMiddlewareExtensions.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.DependencyInjection; +using Money.Common.Middlewares; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Microsoft.AspNetCore.Builder +{ + public static class VersionMiddlewareExtensions + { + public static IServiceCollection AddVersionHeader(this IServiceCollection services, Assembly appAssembly = null) + { + var options = new VersionMiddlewareOptions(); + options.AppAssembly = appAssembly ?? Assembly.GetCallingAssembly(); + + return services + .AddSingleton(options) + .AddTransient(); + } + + public static IApplicationBuilder UseVersionHeader(this IApplicationBuilder builder) => builder.UseMiddleware(); + } +} diff --git a/src/Money.Api/Common/Middlewares/VersionMiddlewareOptions.cs b/src/Money.Api/Common/Middlewares/VersionMiddlewareOptions.cs new file mode 100644 index 00000000..850014e1 --- /dev/null +++ b/src/Money.Api/Common/Middlewares/VersionMiddlewareOptions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Money.Common.Middlewares +{ + public class VersionMiddlewareOptions + { + public Assembly AppAssembly { get; set; } + } +} diff --git a/src/Money.Api/Startup.cs b/src/Money.Api/Startup.cs index 3c9f5649..cafeb3e6 100644 --- a/src/Money.Api/Startup.cs +++ b/src/Money.Api/Startup.cs @@ -16,6 +16,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Tokens; using Money.Common.Diagnostics; +using Money.Common.Middlewares; using Money.Data; using Money.Hubs; using Money.Models; @@ -105,6 +106,9 @@ public void ConfigureServices(IServiceCollection services) .AddControllers() .AddNewtonsoftJson(); + services + .AddVersionHeader(); + services .AddSignalR(); @@ -150,7 +154,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) #if DEBUG p.WithOrigins("http://localhost:48613"); #else - p.WithOrigins("https://app.money.neptuo.com"); + p.WithOrigins("https://app.money.neptuo.com", "https://beta.app.money.neptuo.com"); #endif p.AllowAnyMethod(); p.AllowCredentials(); @@ -161,6 +165,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseAuthentication(); app.UseAuthorization(); + app.UseVersionHeader(); + app.UseEndpoints(endpoints => { endpoints.MapHealthChecks("/health");