Skip to content

Commit

Permalink
#255 - Returning API version in every response as header.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Mar 18, 2020
1 parent 24e11f9 commit fb2088a
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/Money.Api.Shared/Models/Api/VersionHeader.cs
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Microsoft.AspNetCore.Builder;
using Money.Common.Diagnostics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Money.Common.Diagnostics
namespace Microsoft.AspNetCore.Builder
{
public static class ExceptionMiddlewareExceptions
{
Expand Down
32 changes: 32 additions & 0 deletions src/Money.Api/Common/Middlewares/VersionMiddleware.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
27 changes: 27 additions & 0 deletions src/Money.Api/Common/Middlewares/VersionMiddlewareExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<VersionMiddleware>();
}

public static IApplicationBuilder UseVersionHeader(this IApplicationBuilder builder) => builder.UseMiddleware<VersionMiddleware>();
}
}
15 changes: 15 additions & 0 deletions src/Money.Api/Common/Middlewares/VersionMiddlewareOptions.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
8 changes: 7 additions & 1 deletion src/Money.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,6 +106,9 @@ public void ConfigureServices(IServiceCollection services)
.AddControllers()
.AddNewtonsoftJson();

services
.AddVersionHeader();

services
.AddSignalR();

Expand Down Expand Up @@ -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();
Expand All @@ -161,6 +165,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseAuthentication();
app.UseAuthorization();

app.UseVersionHeader();

app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/health");
Expand Down

0 comments on commit fb2088a

Please sign in to comment.