-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide
X-Version
response header for API endpoints (#1355)
- Loading branch information
1 parent
7e6cebe
commit 457d354
Showing
14 changed files
with
126 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Promitor.Agents.Core | ||
{ | ||
public class HttpHeaders | ||
{ | ||
public const string AgentVersion = "X-Version"; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Promitor.Agents.Core/Middleware/AgentVersionMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Promitor.Core; | ||
|
||
namespace Promitor.Agents.Core.Middleware | ||
{ | ||
public class AgentVersionMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AgentVersionMiddleware" /> class which automatically adds the version of the agent to all HTTP responses | ||
/// </summary> | ||
/// <param name="next">The next <see cref="RequestDelegate" /> in the ASP.NET Core request pipeline.</param> | ||
/// <exception cref="System.ArgumentNullException">When the <paramref name="next" /> is <c>null</c>.</exception> | ||
public AgentVersionMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
/// <summary> | ||
/// Invoke the middleware to automatically add version to HTTP responses. | ||
/// </summary> | ||
/// <param name="httpContext">The context for the current HTTP request.</param> | ||
public async Task Invoke(HttpContext httpContext) | ||
{ | ||
var version = Version.Get(); | ||
|
||
httpContext.Response.OnStarting(state => | ||
{ | ||
var context = (HttpContext)state; | ||
context.Response.Headers.TryAdd(HttpHeaders.AgentVersion, version); | ||
|
||
return Task.CompletedTask; | ||
}, httpContext); | ||
|
||
await _next(httpContext); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters