MireaBackend/Endpoint/Configuration/Core/Middleware/MaintenanceModeMiddleware.cs
Polianin Nikita 85722f8552
Some checks failed
.NET Test Pipeline / build-and-test (push) Failing after 1m43s
Build and Deploy Docker Container / build-and-deploy (push) Failing after 2m6s
feat: add integration with seq
2024-12-22 07:13:59 +03:00

34 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Http;
using Mirea.Api.Endpoint.Common.Attributes;
using Mirea.Api.Endpoint.Common.Exceptions;
using Mirea.Api.Endpoint.Common.Interfaces;
using System.Threading.Tasks;
namespace Mirea.Api.Endpoint.Configuration.Core.Middleware;
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;
}
public async Task InvokeAsync(HttpContext context)
{
if (!maintenanceModeService.IsMaintenanceMode && !maintenanceModeNotConfigureService.IsMaintenanceMode || IsIgnoreMaintenanceMode(context))
await next(context);
else
{
context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
context.Response.ContentType = "plain/text";
if (maintenanceModeService.IsMaintenanceMode)
throw new ServerUnavailableException("The service is currently undergoing maintenance. Please try again later.", true);
throw new ServerUnavailableException(
"The service is currently not configured. Go to the setup page if you are an administrator or try again later.", false);
}
}
}