ServerStartupMessageMiddleware.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Net.Mime;
  2. using System.Threading.Tasks;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Model.Globalization;
  5. using Microsoft.AspNetCore.Http;
  6. namespace Jellyfin.Server.Middleware
  7. {
  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. {
  36. await _next(httpContext).ConfigureAwait(false);
  37. return;
  38. }
  39. var message = localizationManager.GetLocalizedString("StartupEmbyServerIsLoading");
  40. httpContext.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
  41. httpContext.Response.ContentType = MediaTypeNames.Text.Html;
  42. await httpContext.Response.WriteAsync(message, httpContext.RequestAborted).ConfigureAwait(false);
  43. }
  44. }
  45. }