Compare commits
4 Commits
7b779463bb
...
3f30b98cf9
Author | SHA1 | Date | |
---|---|---|---|
3f30b98cf9 | |||
af284e945f | |||
b62ddc9015 | |||
d1a806545d |
@ -0,0 +1,6 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Mirea.Api.Endpoint.Common.Attributes;
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||||
|
public class MaintenanceModeIgnoreAttribute : Attribute;
|
@ -0,0 +1,8 @@
|
|||||||
|
namespace Mirea.Api.Endpoint.Common.Interfaces;
|
||||||
|
|
||||||
|
public interface IMaintenanceModeNotConfigureService
|
||||||
|
{
|
||||||
|
bool IsMaintenanceMode { get; }
|
||||||
|
|
||||||
|
void DisableMaintenanceMode();
|
||||||
|
}
|
10
Endpoint/Common/Interfaces/IMaintenanceModeService.cs
Normal file
10
Endpoint/Common/Interfaces/IMaintenanceModeService.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
namespace Mirea.Api.Endpoint.Common.Interfaces;
|
||||||
|
|
||||||
|
public interface IMaintenanceModeService
|
||||||
|
{
|
||||||
|
bool IsMaintenanceMode { get; }
|
||||||
|
|
||||||
|
void EnableMaintenanceMode();
|
||||||
|
|
||||||
|
void DisableMaintenanceMode();
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
using Mirea.Api.Endpoint.Common.Interfaces;
|
||||||
|
|
||||||
|
namespace Mirea.Api.Endpoint.Common.Services;
|
||||||
|
|
||||||
|
public class MaintenanceModeNotConfigureService : IMaintenanceModeNotConfigureService
|
||||||
|
{
|
||||||
|
public bool IsMaintenanceMode { get; private set; } = true;
|
||||||
|
|
||||||
|
public void DisableMaintenanceMode() =>
|
||||||
|
IsMaintenanceMode = false;
|
||||||
|
}
|
14
Endpoint/Common/Services/MaintenanceModeService.cs
Normal file
14
Endpoint/Common/Services/MaintenanceModeService.cs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
using Mirea.Api.Endpoint.Common.Interfaces;
|
||||||
|
|
||||||
|
namespace Mirea.Api.Endpoint.Common.Services;
|
||||||
|
|
||||||
|
public class MaintenanceModeService : IMaintenanceModeService
|
||||||
|
{
|
||||||
|
public bool IsMaintenanceMode { get; private set; }
|
||||||
|
|
||||||
|
public void EnableMaintenanceMode() =>
|
||||||
|
IsMaintenanceMode = true;
|
||||||
|
|
||||||
|
public void DisableMaintenanceMode() =>
|
||||||
|
IsMaintenanceMode = false;
|
||||||
|
}
|
39
Endpoint/Middleware/MaintenanceModeMiddleware.cs
Normal file
39
Endpoint/Middleware/MaintenanceModeMiddleware.cs
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Mirea.Api.Endpoint.Common.Attributes;
|
||||||
|
using Mirea.Api.Endpoint.Common.Interfaces;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Mirea.Api.Endpoint.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 Invoke(HttpContext context)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -13,6 +13,7 @@ using Mirea.Api.Endpoint.Common.Services;
|
|||||||
using Mirea.Api.Endpoint.Configuration;
|
using Mirea.Api.Endpoint.Configuration;
|
||||||
using Mirea.Api.Endpoint.Configuration.General;
|
using Mirea.Api.Endpoint.Configuration.General;
|
||||||
using Mirea.Api.Endpoint.Configuration.Swagger;
|
using Mirea.Api.Endpoint.Configuration.Swagger;
|
||||||
|
using Mirea.Api.Endpoint.Middleware;
|
||||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
@ -51,6 +52,8 @@ public class Program
|
|||||||
builder.Services.AddPersistence(builder.Configuration.Get<GeneralConfig>()?.DbSettings?.ConnectionStringSql ?? string.Empty);
|
builder.Services.AddPersistence(builder.Configuration.Get<GeneralConfig>()?.DbSettings?.ConnectionStringSql ?? string.Empty);
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<IMaintenanceModeNotConfigureService, MaintenanceModeNotConfigureService>();
|
||||||
|
builder.Services.AddSingleton<IMaintenanceModeService, MaintenanceModeService>();
|
||||||
builder.Services.AddSingleton<ISetupToken, SetupTokenService>();
|
builder.Services.AddSingleton<ISetupToken, SetupTokenService>();
|
||||||
builder.Services.AddCors(options =>
|
builder.Services.AddCors(options =>
|
||||||
{
|
{
|
||||||
@ -116,6 +119,7 @@ public class Program
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
app.UseMiddleware<MaintenanceModeMiddleware>();
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user