mirror of
https://github.com/Readarr/Readarr
synced 2026-01-02 21:53:18 +01:00
New: Return static response to requests while app is starting
(cherry picked from commit 303fc5d786ccf2ad14c8523fc239696c5d37ea53)
This commit is contained in:
parent
c34418b984
commit
3c03413d5a
5 changed files with 45 additions and 0 deletions
|
|
@ -9,6 +9,7 @@ public interface IRuntimeInfo
|
|||
bool IsAdmin { get; }
|
||||
bool IsWindowsService { get; }
|
||||
bool IsWindowsTray { get; }
|
||||
bool IsStarting { get; set; }
|
||||
bool IsExiting { get; set; }
|
||||
bool IsTray { get; }
|
||||
RuntimeMode Mode { get; }
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ public RuntimeInfo(Logger logger, IHostLifetime hostLifetime = null)
|
|||
_logger = logger;
|
||||
|
||||
IsWindowsService = hostLifetime is WindowsServiceLifetime;
|
||||
IsStarting = true;
|
||||
|
||||
//Guarded to avoid issues when running in a non-managed process
|
||||
var entry = Process.GetCurrentProcess().MainModule;
|
||||
|
|
@ -80,6 +81,7 @@ public bool IsAdmin
|
|||
|
||||
public bool IsWindowsService { get; private set; }
|
||||
|
||||
public bool IsStarting { get; set; }
|
||||
public bool IsExiting { get; set; }
|
||||
|
||||
public bool IsTray
|
||||
|
|
|
|||
|
|
@ -57,6 +57,7 @@ public Task StopAsync(CancellationToken cancellationToken)
|
|||
|
||||
private void OnAppStarted()
|
||||
{
|
||||
_runtimeInfo.IsStarting = false;
|
||||
_runtimeInfo.IsExiting = false;
|
||||
|
||||
if (!_startupContext.Flags.Contains(StartupContext.NO_BROWSER)
|
||||
|
|
|
|||
|
|
@ -252,6 +252,7 @@ public void Configure(IApplicationBuilder app,
|
|||
|
||||
app.UseMiddleware<VersionMiddleware>();
|
||||
app.UseMiddleware<UrlBaseMiddleware>(configFileProvider.UrlBase);
|
||||
app.UseMiddleware<StartingUpMiddleware>();
|
||||
app.UseMiddleware<CacheHeaderMiddleware>();
|
||||
app.UseMiddleware<IfModifiedMiddleware>();
|
||||
app.UseMiddleware<BufferingMiddleware>(new List<string> { "/api/v1/command" });
|
||||
|
|
|
|||
40
src/Readarr.Http/Middleware/StartingUpMiddleware.cs
Normal file
40
src/Readarr.Http/Middleware/StartingUpMiddleware.cs
Normal file
|
|
@ -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 a new issue