shoc-backend/Api.SeaHavenIndustries/Middleware/LegacyDeprecationMiddleware.cs

50 lines
1.7 KiB
C#

using Api.SeaHavenIndustries.Options;
using Microsoft.Extensions.Options;
namespace Api.SeaHavenIndustries.Middleware
{
/// <summary>Adds Sunset/Deprecation headers to legacy WorkOrder REST routes.</summary>
public class LegacyDeprecationMiddleware
{
private static readonly HashSet<string> LegacyPathSegments = new(StringComparer.OrdinalIgnoreCase)
{
"GetWorkOrderList",
"AddWorkorder",
"EditWorkorder",
"ChangeStatus",
"ChangeAssignment",
"DeleteWorkorder",
"GetWorkorderById"
};
private readonly RequestDelegate _next;
private readonly LegacyEndpointOptions _options;
public LegacyDeprecationMiddleware(RequestDelegate next, IOptions<LegacyEndpointOptions> options)
{
_next = next;
_options = options.Value;
}
public async Task InvokeAsync(HttpContext context)
{
await _next(context);
if (!_options.DeprecationEnabled)
return;
var path = context.Request.Path.Value ?? "";
if (!path.Contains("/WorkOrder", StringComparison.OrdinalIgnoreCase)
&& !path.Contains("/workorders", StringComparison.OrdinalIgnoreCase))
return;
if (!LegacyPathSegments.Any(seg => path.Contains(seg, StringComparison.OrdinalIgnoreCase)))
return;
context.Response.Headers["Deprecation"] = "true";
if (!string.IsNullOrWhiteSpace(_options.SunsetDate))
context.Response.Headers["Sunset"] = _options.SunsetDate;
context.Response.Headers["Link"] = "</api/workorders/board>; rel=\"successor-version\"";
}
}
}