Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change action filters to run after requests have been processed #864

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions DragaliaAPI/DragaliaAPI/Middleware/MaintenanceActionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ public class MaintenanceActionFilter(
ILogger<MaintenanceActionFilter> logger
) : IActionFilter
{
private readonly IOptionsMonitor<MaintenanceOptions> options = options;
private readonly ILogger<MaintenanceActionFilter> logger = logger;
public void OnActionExecuting(ActionExecutingContext context) { }

public void OnActionExecuting(ActionExecutingContext context)
public void OnActionExecuted(ActionExecutedContext context)
{
if (!this.options.CurrentValue.Enabled)
if (!options.CurrentValue.Enabled)
{
return;
}

this.logger.LogInformation("Aborting request due to active maintenance.");
logger.LogInformation("Rewriting response due to active maintenance.");

context.Result = new OkObjectResult(
new DragaliaResponse<object>(
Expand All @@ -30,6 +31,4 @@ public void OnActionExecuting(ActionExecutingContext context)
)
);
}

public void OnActionExecuted(ActionExecutedContext context) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@ public class ResourceVersionActionFilter(
ILogger<ResourceVersionActionFilter> logger
) : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
public void OnActionExecuting(ActionExecutingContext context) { }

public void OnActionExecuted(ActionExecutedContext context)
{
string? clientResourceVer = context.HttpContext.Request.Headers["Res-Ver"].FirstOrDefault();
if (clientResourceVer is null)
{
return;
}

string? platformString = context.HttpContext.Request.Headers["Platform"].FirstOrDefault();
if (!Enum.TryParse(platformString, out Platform platform) && Enum.IsDefined(platform))
{
return;
}

string serverResourceVer = resourceVersionService.GetResourceVersion(platform);

if (clientResourceVer != serverResourceVer)
{
logger.LogInformation(
"Request blocked due to resource version mismatch: client: {clientVer}, server: {serverVer}",
"Response rewritten due to resource version mismatch: client: {clientVer}, server: {serverVer}",
clientResourceVer,
serverResourceVer
);
Expand All @@ -38,6 +44,4 @@ public void OnActionExecuting(ActionExecutingContext context)
);
}
}

public void OnActionExecuted(ActionExecutedContext context) { }
}
Loading