CoreAppHost.cs 4.9 KB

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