ServerStartupMessageMiddleware.cs 1.9 KB

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