From 75fc550a3fb44e528ca3c311e83075f79f987a36 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 3 Dec 2021 17:41:30 -0800 Subject: [PATCH] New: /ping endpoint for verifying that Sonarr is running and able to access it's DB Closes #4766 --- .../Authentication/AuthenticationService.cs | 5 +++ .../Extensions/RequestExtensions.cs | 5 +++ src/Sonarr.Http/Ping/PingModule.cs | 41 +++++++++++++++++++ src/Sonarr.Http/Ping/PingResource.cs | 8 ++++ 4 files changed, 59 insertions(+) create mode 100644 src/Sonarr.Http/Ping/PingModule.cs create mode 100644 src/Sonarr.Http/Ping/PingResource.cs diff --git a/src/Sonarr.Http/Authentication/AuthenticationService.cs b/src/Sonarr.Http/Authentication/AuthenticationService.cs index 2778e8828..56bdb04ab 100644 --- a/src/Sonarr.Http/Authentication/AuthenticationService.cs +++ b/src/Sonarr.Http/Authentication/AuthenticationService.cs @@ -166,6 +166,11 @@ namespace Sonarr.Http.Authentication { return true; } + + if (context.Request.IsPingRequest()) + { + return true; + } if (ValidUser(context)) { diff --git a/src/Sonarr.Http/Extensions/RequestExtensions.cs b/src/Sonarr.Http/Extensions/RequestExtensions.cs index 807401164..e07cfdaba 100644 --- a/src/Sonarr.Http/Extensions/RequestExtensions.cs +++ b/src/Sonarr.Http/Extensions/RequestExtensions.cs @@ -18,6 +18,11 @@ namespace Sonarr.Http.Extensions return request.Path.StartsWith("/feed/", StringComparison.InvariantCultureIgnoreCase); } + public static bool IsPingRequest(this Request request) + { + return request.Path.StartsWith("/ping", StringComparison.InvariantCultureIgnoreCase); + } + public static bool IsSignalRRequest(this Request request) { return request.Path.StartsWith("/signalr/", StringComparison.InvariantCultureIgnoreCase); diff --git a/src/Sonarr.Http/Ping/PingModule.cs b/src/Sonarr.Http/Ping/PingModule.cs new file mode 100644 index 000000000..2d248dd61 --- /dev/null +++ b/src/Sonarr.Http/Ping/PingModule.cs @@ -0,0 +1,41 @@ +using System; +using Nancy; +using NzbDrone.Core.Configuration; +using NzbDrone.Http.Ping; +using Sonarr.Http.Extensions; + +namespace NzbDrone.Http +{ + public class PingModule : NancyModule + { + private readonly IConfigRepository _configRepository; + + public PingModule(IConfigRepository configRepository) + { + _configRepository = configRepository; + + Get("/ping", x => GetStatus()); + } + + private Response GetStatus() + { + try + { + _configRepository.All(); + } + catch (Exception e) + { + return new PingResource + { + Status = "Error", + Message = e.Message + }.AsResponse(Context, HttpStatusCode.InternalServerError); + } + + return new PingResource + { + Status = "OK" + }.AsResponse(Context, HttpStatusCode.OK); + } + } +} diff --git a/src/Sonarr.Http/Ping/PingResource.cs b/src/Sonarr.Http/Ping/PingResource.cs new file mode 100644 index 000000000..65e6cc42d --- /dev/null +++ b/src/Sonarr.Http/Ping/PingResource.cs @@ -0,0 +1,8 @@ +namespace NzbDrone.Http.Ping +{ + public class PingResource + { + public string Status { get; set; } + public string Message { get; set; } + } +}