using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DickerData.Website.Infrastructure
{
public class WebsiteMaintenanceMiddleware
{
public WebsiteMaintenanceMiddleware(RequestDelegate next)
{
_next = next;
}
readonly RequestDelegate _next;
public async Task Invoke(HttpContext context, IConfiguration configuration)
{
if(IsWebsiteMaintenance(configuration))
{
var url = "/Home/WebsiteMaintenance";
if (context.Request.Path.Value.IndexOf(url, StringComparison.CurrentCultureIgnoreCase) == -1)
{
context.Response.Redirect(url);
return;
}
}
await _next(context);
}
public static bool IsWebsiteMaintenance(IConfiguration configuration)
{
string IsMaintenance = configuration.GetSection("AppConfig")["WebsiteMaintenance"];
if (IsMaintenance == "1")
{
return true;
}
return false;
}
}
}