CoreAppHost.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Emby.Server.Implementations;
  5. using Emby.Server.Implementations.Session;
  6. using Jellyfin.Api.WebSocketListeners;
  7. using Jellyfin.Drawing;
  8. using Jellyfin.Drawing.Skia;
  9. using Jellyfin.Server.Implementations;
  10. using Jellyfin.Server.Implementations.Activity;
  11. using Jellyfin.Server.Implementations.Devices;
  12. using Jellyfin.Server.Implementations.Events;
  13. using Jellyfin.Server.Implementations.Security;
  14. using Jellyfin.Server.Implementations.Users;
  15. using MediaBrowser.Controller;
  16. using MediaBrowser.Controller.BaseItemManager;
  17. using MediaBrowser.Controller.Devices;
  18. using MediaBrowser.Controller.Drawing;
  19. using MediaBrowser.Controller.Events;
  20. using MediaBrowser.Controller.Library;
  21. using MediaBrowser.Controller.Lyrics;
  22. using MediaBrowser.Controller.Net;
  23. using MediaBrowser.Controller.Security;
  24. using MediaBrowser.Model.Activity;
  25. using Microsoft.Extensions.Configuration;
  26. using Microsoft.Extensions.DependencyInjection;
  27. using Microsoft.Extensions.Logging;
  28. namespace Jellyfin.Server
  29. {
  30. /// <summary>
  31. /// Implementation of the abstract <see cref="ApplicationHost" /> class.
  32. /// </summary>
  33. public class CoreAppHost : ApplicationHost
  34. {
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="CoreAppHost" /> class.
  37. /// </summary>
  38. /// <param name="applicationPaths">The <see cref="ServerApplicationPaths" /> to be used by the <see cref="CoreAppHost" />.</param>
  39. /// <param name="loggerFactory">The <see cref="ILoggerFactory" /> to be used by the <see cref="CoreAppHost" />.</param>
  40. /// <param name="options">The <see cref="StartupOptions" /> to be used by the <see cref="CoreAppHost" />.</param>
  41. /// <param name="startupConfig">The <see cref="IConfiguration" /> to be used by the <see cref="CoreAppHost" />.</param>
  42. public CoreAppHost(
  43. IServerApplicationPaths applicationPaths,
  44. ILoggerFactory loggerFactory,
  45. IStartupOptions options,
  46. IConfiguration startupConfig)
  47. : base(
  48. applicationPaths,
  49. loggerFactory,
  50. options,
  51. startupConfig)
  52. {
  53. }
  54. /// <inheritdoc/>
  55. protected override void RegisterServices(IServiceCollection serviceCollection)
  56. {
  57. // Register an image encoder
  58. bool useSkiaEncoder = SkiaEncoder.IsNativeLibAvailable();
  59. Type imageEncoderType = useSkiaEncoder
  60. ? typeof(SkiaEncoder)
  61. : typeof(NullImageEncoder);
  62. serviceCollection.AddSingleton(typeof(IImageEncoder), imageEncoderType);
  63. // Log a warning if the Skia encoder could not be used
  64. if (!useSkiaEncoder)
  65. {
  66. Logger.LogWarning("Skia not available. Will fallback to {ImageEncoder}.", nameof(NullImageEncoder));
  67. }
  68. serviceCollection.AddEventServices();
  69. serviceCollection.AddSingleton<IBaseItemManager, BaseItemManager>();
  70. serviceCollection.AddSingleton<IEventManager, EventManager>();
  71. serviceCollection.AddSingleton<IActivityManager, ActivityManager>();
  72. serviceCollection.AddSingleton<IUserManager, UserManager>();
  73. serviceCollection.AddScoped<IDisplayPreferencesManager, DisplayPreferencesManager>();
  74. serviceCollection.AddSingleton<IDeviceManager, DeviceManager>();
  75. // TODO search the assemblies instead of adding them manually?
  76. serviceCollection.AddSingleton<IWebSocketListener, SessionWebSocketListener>();
  77. serviceCollection.AddSingleton<IWebSocketListener, ActivityLogWebSocketListener>();
  78. serviceCollection.AddSingleton<IWebSocketListener, ScheduledTasksWebSocketListener>();
  79. serviceCollection.AddSingleton<IWebSocketListener, SessionInfoWebSocketListener>();
  80. serviceCollection.AddSingleton<IAuthorizationContext, AuthorizationContext>();
  81. serviceCollection.AddScoped<IAuthenticationManager, AuthenticationManager>();
  82. foreach (var type in GetExportTypes<ILyricProvider>())
  83. {
  84. serviceCollection.AddSingleton(typeof(ILyricProvider), type);
  85. }
  86. base.RegisterServices(serviceCollection);
  87. }
  88. /// <inheritdoc />
  89. protected override void RestartInternal() => Program.Restart();
  90. /// <inheritdoc />
  91. protected override IEnumerable<Assembly> GetAssembliesWithPartsInternal()
  92. {
  93. // Jellyfin.Server
  94. yield return typeof(CoreAppHost).Assembly;
  95. // Jellyfin.Server.Implementations
  96. yield return typeof(JellyfinDbContext).Assembly;
  97. }
  98. /// <inheritdoc />
  99. protected override void ShutdownInternal() => Program.Shutdown();
  100. }
  101. }