ServerStartupMessageMiddleware.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Net.Mime;
  3. using System.Threading.Tasks;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Model.Globalization;
  6. using Microsoft.AspNetCore.Http;
  7. namespace Jellyfin.Api.Middleware;
  8. /// <summary>
  9. /// Shows a custom message during server startup.
  10. /// </summary>
  11. public class ServerStartupMessageMiddleware
  12. {
  13. private readonly RequestDelegate _next;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="ServerStartupMessageMiddleware"/> class.
  16. /// </summary>
  17. /// <param name="next">The next delegate in the pipeline.</param>
  18. public ServerStartupMessageMiddleware(RequestDelegate next)
  19. {
  20. _next = next;
  21. }
  22. /// <summary>
  23. /// Executes the middleware action.
  24. /// </summary>
  25. /// <param name="httpContext">The current HTTP context.</param>
  26. /// <param name="serverApplicationHost">The server application host.</param>
  27. /// <param name="localizationManager">The localization manager.</param>
  28. /// <returns>The async task.</returns>
  29. public async Task Invoke(
  30. HttpContext httpContext,
  31. IServerApplicationHost serverApplicationHost,
  32. ILocalizationManager localizationManager)
  33. {
  34. if (serverApplicationHost.CoreStartupHasCompleted
  35. || httpContext.Request.Path.Equals("/system/ping", StringComparison.OrdinalIgnoreCase))
  36. {
  37. await _next(httpContext).ConfigureAwait(false);
  38. return;
  39. }
  40. var message = localizationManager.GetLocalizedString("StartupEmbyServerIsLoading");
  41. httpContext.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
  42. httpContext.Response.ContentType = MediaTypeNames.Text.Html;
  43. await httpContext.Response.WriteAsync(message, httpContext.RequestAborted).ConfigureAwait(false);
  44. }
  45. }