//
namespace SafeCampus.Web.Core;
///
/// 网站开启状态 状态中间件
///
public class WebStatusMiddleware
{
private readonly RequestDelegate _next;
public WebStatusMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
var path = context.Request.Path.Value;//获取请求路径
// 检查请求路径是否以 "/biz" 开头
if (path.Length > 1 && !path.Contains('.') && !path.StartsWith("/sys", StringComparison.OrdinalIgnoreCase))
{
// 通过 context.RequestServices 解析
var configService = context.RequestServices.GetService();
// 获取网站状态
var webStatus = await configService.GetByConfigKey(CateGoryConst.CONFIG_SYS_BASE, SysConfigConst.SYS_WEB_STATUS);
// 如果网站状态为禁用,则返回 443 状态码
if (webStatus.ConfigValue == CommonStatusConst.DISABLED)
{
context.Response.StatusCode = 423;
return;
}
}
await _next(context);
}
}
///
/// 中间件拓展类
///
public static class WebStatusMiddlewareExtensions
{
public static IApplicationBuilder UseWebStatus(this IApplicationBuilder builder)
{
return builder.UseMiddleware();
}
}