2024-05-28 07:04:07 +03:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Mirea.Api.Endpoint.Common.Attributes;
|
|
|
|
|
using Mirea.Api.Endpoint.Common.Interfaces;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2024-10-07 02:25:36 +03:00
|
|
|
|
namespace Mirea.Api.Endpoint.Configuration.Core.Middleware;
|
2024-05-28 07:04:07 +03:00
|
|
|
|
|
|
|
|
|
public class MaintenanceModeMiddleware(RequestDelegate next, IMaintenanceModeService maintenanceModeService, IMaintenanceModeNotConfigureService maintenanceModeNotConfigureService)
|
|
|
|
|
{
|
|
|
|
|
private static bool IsIgnoreMaintenanceMode(HttpContext context)
|
|
|
|
|
{
|
|
|
|
|
var endpoint = context.GetEndpoint();
|
|
|
|
|
return endpoint?.Metadata.GetMetadata<MaintenanceModeIgnoreAttribute>() != null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-12 21:36:07 +03:00
|
|
|
|
public async Task InvokeAsync(HttpContext context)
|
2024-05-28 07:04:07 +03:00
|
|
|
|
{
|
|
|
|
|
if (!maintenanceModeService.IsMaintenanceMode && !maintenanceModeNotConfigureService.IsMaintenanceMode || IsIgnoreMaintenanceMode(context))
|
|
|
|
|
await next(context);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
|
|
|
|
|
context.Response.ContentType = "plain/text";
|
|
|
|
|
|
|
|
|
|
string error;
|
|
|
|
|
if (maintenanceModeService.IsMaintenanceMode)
|
|
|
|
|
{
|
|
|
|
|
context.Response.Headers.RetryAfter = "600";
|
|
|
|
|
error = "The service is currently undergoing maintenance. Please try again later.";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
error =
|
|
|
|
|
"The service is currently not configured. Go to the setup page if you are an administrator or try again later.";
|
|
|
|
|
|
|
|
|
|
await context.Response.WriteAsync(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|