(cherry picked from commit 303fc5d786ccf2ad14c8523fc239696c5d37ea53)pull/2436/head
parent
c34418b984
commit
3c03413d5a
@ -0,0 +1,40 @@
|
|||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
|
using NzbDrone.Common.Serializer;
|
||||||
|
using Readarr.Http.Extensions;
|
||||||
|
|
||||||
|
namespace Readarr.Http.Middleware
|
||||||
|
{
|
||||||
|
public class StartingUpMiddleware
|
||||||
|
{
|
||||||
|
private readonly RequestDelegate _next;
|
||||||
|
private readonly IRuntimeInfo _runtimeInfo;
|
||||||
|
private static readonly string MESSAGE = "Readarr is starting up, please try again later";
|
||||||
|
|
||||||
|
public StartingUpMiddleware(RequestDelegate next, IRuntimeInfo runtimeInfo)
|
||||||
|
{
|
||||||
|
_next = next;
|
||||||
|
_runtimeInfo = runtimeInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task InvokeAsync(HttpContext context)
|
||||||
|
{
|
||||||
|
if (_runtimeInfo.IsStarting)
|
||||||
|
{
|
||||||
|
var isJson = context.Request.IsApiRequest();
|
||||||
|
var message = isJson ? STJson.ToJson(new { ErrorMessage = MESSAGE }) : MESSAGE;
|
||||||
|
var bytes = Encoding.UTF8.GetBytes(message);
|
||||||
|
|
||||||
|
context.Response.StatusCode = 503;
|
||||||
|
context.Response.ContentType = isJson ? "application/json" : "text/plain";
|
||||||
|
await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _next(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue