ATV requested endpoint mock

This commit is contained in:
JPVenson 2024-10-26 16:59:12 +00:00
parent dc029d549c
commit 41c27d4e7e
2 changed files with 52 additions and 9 deletions

View file

@ -46,7 +46,7 @@ namespace Jellyfin.Server
private static readonly SerilogLoggerFactory _loggerFactory = new SerilogLoggerFactory();
private static SetupServer? _setupServer = new();
private static CoreAppHost? _appHost;
private static IHost? _jfHost = null;
private static long _startTimestamp;
private static ILogger _logger = NullLogger.Instance;
@ -74,7 +74,7 @@ namespace Jellyfin.Server
{
_startTimestamp = Stopwatch.GetTimestamp();
ServerApplicationPaths appPaths = StartupHelpers.CreateApplicationPaths(options);
await _setupServer!.RunAsync(static () => _jfHost?.Services?.GetService<INetworkManager>(), appPaths).ConfigureAwait(false);
await _setupServer!.RunAsync(static () => _jfHost?.Services?.GetService<INetworkManager>(), appPaths, static () => _appHost).ConfigureAwait(false);
// $JELLYFIN_LOG_DIR needs to be set for the logger configuration manager
Environment.SetEnvironmentVariable("JELLYFIN_LOG_DIR", appPaths.LogDirectoryPath);
@ -130,18 +130,19 @@ namespace Jellyfin.Server
{
_startTimestamp = Stopwatch.GetTimestamp();
_setupServer = new SetupServer();
await _setupServer.RunAsync(static () => _jfHost?.Services?.GetService<INetworkManager>(), appPaths).ConfigureAwait(false);
await _setupServer.RunAsync(static () => _jfHost?.Services?.GetService<INetworkManager>(), appPaths, static () => _appHost).ConfigureAwait(false);
}
} while (_restartOnShutdown);
}
private static async Task StartServer(IServerApplicationPaths appPaths, StartupOptions options, IConfiguration startupConfig)
{
using var appHost = new CoreAppHost(
appPaths,
_loggerFactory,
options,
startupConfig);
using CoreAppHost appHost = new CoreAppHost(
appPaths,
_loggerFactory,
options,
startupConfig);
_appHost = appHost;
try
{
_jfHost = Host.CreateDefaultBuilder()
@ -215,6 +216,7 @@ namespace Jellyfin.Server
}
}
_appHost = null;
_jfHost?.Dispose();
}
}

View file

@ -7,6 +7,8 @@ using System.Threading.Tasks;
using Jellyfin.Networking.Manager;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Model.System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
@ -31,8 +33,12 @@ public sealed class SetupServer : IDisposable
/// </summary>
/// <param name="networkManagerFactory">The networkmanager.</param>
/// <param name="applicationPaths">The application paths.</param>
/// <param name="serverApplicationHost">The servers application host.</param>
/// <returns>A Task.</returns>
public async Task RunAsync(Func<INetworkManager?> networkManagerFactory, IApplicationPaths applicationPaths)
public async Task RunAsync(
Func<INetworkManager?> networkManagerFactory,
IApplicationPaths applicationPaths,
Func<IServerApplicationHost?> serverApplicationHost)
{
ThrowIfDisposed();
_startupServer = Host.CreateDefaultBuilder()
@ -69,6 +75,41 @@ public sealed class SetupServer : IDisposable
});
});
app.Map("/System/Info/Public", systemRoute =>
{
systemRoute.Run(async context =>
{
var jfApplicationHost = serverApplicationHost();
var retryCounter = 0;
while (jfApplicationHost is null && retryCounter < 5)
{
await Task.Delay(500).ConfigureAwait(false);
jfApplicationHost = serverApplicationHost();
retryCounter++;
}
if (jfApplicationHost is null)
{
context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
context.Response.Headers.RetryAfter = new Microsoft.Extensions.Primitives.StringValues("60");
return;
}
var sysInfo = new PublicSystemInfo
{
Version = jfApplicationHost.ApplicationVersionString,
ProductName = jfApplicationHost.Name,
Id = jfApplicationHost.SystemId,
ServerName = jfApplicationHost.FriendlyName,
LocalAddress = jfApplicationHost.GetSmartApiUrl(context.Request),
StartupWizardCompleted = false
};
await context.Response.WriteAsJsonAsync(sysInfo).ConfigureAwait(false);
});
});
app.Run((context) =>
{
context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;