(),
builderContext,
options);
})
.Configure(app =>
{
app.UseHealthChecks("/health");
app.Map("/startup/logger", loggerRoute =>
{
loggerRoute.Run(async context =>
{
var networkManager = _networkManagerFactory();
if (context.Connection.RemoteIpAddress is null || networkManager is null || !networkManager.IsInLocalNetwork(context.Connection.RemoteIpAddress))
{
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return;
}
var logFilePath = new DirectoryInfo(_applicationPaths.LogDirectoryPath)
.EnumerateFiles()
.OrderByDescending(f => f.CreationTimeUtc)
.FirstOrDefault()
?.FullName;
if (logFilePath is not null)
{
await context.Response.SendFileAsync(logFilePath, CancellationToken.None).ConfigureAwait(false);
}
});
});
app.Map("/System/Info/Public", systemRoute =>
{
systemRoute.Run(async context =>
{
var jfApplicationHost = _serverFactory();
var retryCounter = 0;
while (jfApplicationHost is null && retryCounter < 5)
{
await Task.Delay(500).ConfigureAwait(false);
jfApplicationHost = _serverFactory();
retryCounter++;
}
if (jfApplicationHost is null)
{
context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
context.Response.Headers.RetryAfter = new StringValues("5");
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;
context.Response.Headers.RetryAfter = new StringValues("5");
context.Response.Headers.ContentType = new StringValues("text/html");
context.Response.WriteAsync("Jellyfin Server still starting. Please wait.
");
var networkManager = _networkManagerFactory();
if (networkManager is not null && context.Connection.RemoteIpAddress is not null && networkManager.IsInLocalNetwork(context.Connection.RemoteIpAddress))
{
context.Response.WriteAsync("You can download the current logfiles here.
");
}
return Task.CompletedTask;
});
});
})
.Build();
await _startupServer.StartAsync().ConfigureAwait(false);
}
///
/// Stops the Setup server.
///
/// A task. Duh.
public async Task StopAsync()
{
ThrowIfDisposed();
if (_startupServer is null)
{
throw new InvalidOperationException("Tried to stop a non existing startup server");
}
await _startupServer.StopAsync().ConfigureAwait(false);
}
///
public void Dispose()
{
if (_disposed)
{
return;
}
_disposed = true;
_startupServer?.Dispose();
}
private void ThrowIfDisposed()
{
ObjectDisposedException.ThrowIf(_disposed, this);
}
private class SetupHealthcheck : IHealthCheck
{
public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
return Task.FromResult(HealthCheckResult.Degraded("Server is still starting up."));
}
}
}