CoreAppHost.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.Controller;
  15. using MediaBrowser.Controller.BaseItemManager;
  16. using MediaBrowser.Controller.Drawing;
  17. using MediaBrowser.Controller.Events;
  18. using MediaBrowser.Controller.Library;
  19. using MediaBrowser.Controller.Net;
  20. using MediaBrowser.Model.Activity;
  21. using MediaBrowser.Model.IO;
  22. using Microsoft.EntityFrameworkCore;
  23. using Microsoft.Extensions.DependencyInjection;
  24. using Microsoft.Extensions.Logging;
  25. namespace Jellyfin.Server
  26. {
  27. /// <summary>
  28. /// Implementation of the abstract <see cref="ApplicationHost" /> class.
  29. /// </summary>
  30. public class CoreAppHost : ApplicationHost
  31. {
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="CoreAppHost" /> class.
  34. /// </summary>
  35. /// <param name="applicationPaths">The <see cref="ServerApplicationPaths" /> to be used by the <see cref="CoreAppHost" />.</param>
  36. /// <param name="loggerFactory">The <see cref="ILoggerFactory" /> to be used by the <see cref="CoreAppHost" />.</param>
  37. /// <param name="options">The <see cref="StartupOptions" /> to be used by the <see cref="CoreAppHost" />.</param>
  38. /// <param name="fileSystem">The <see cref="IFileSystem" /> to be used by the <see cref="CoreAppHost" />.</param>
  39. /// <param name="collection">The <see cref="IServiceCollection"/> to be used by the <see cref="CoreAppHost"/>.</param>
  40. public CoreAppHost(
  41. IServerApplicationPaths applicationPaths,
  42. ILoggerFactory loggerFactory,
  43. IStartupOptions options,
  44. IFileSystem fileSystem,
  45. IServiceCollection collection)
  46. : base(
  47. applicationPaths,
  48. loggerFactory,
  49. options,
  50. fileSystem,
  51. collection)
  52. {
  53. }
  54. /// <inheritdoc/>
  55. protected override void RegisterServices()
  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 {nameof(NullImageEncoder)}.");
  67. }
  68. ServiceCollection.AddDbContextPool<JellyfinDb>(
  69. options => options.UseSqlite($"Filename={Path.Combine(ApplicationPaths.DataPath, "jellyfin.db")}"));
  70. ServiceCollection.AddEventServices();
  71. ServiceCollection.AddSingleton<IBaseItemManager, BaseItemManager>();
  72. ServiceCollection.AddSingleton<IEventManager, EventManager>();
  73. ServiceCollection.AddSingleton<JellyfinDbProvider>();
  74. ServiceCollection.AddSingleton<IActivityManager, ActivityManager>();
  75. ServiceCollection.AddSingleton<IUserManager, UserManager>();
  76. ServiceCollection.AddSingleton<IDisplayPreferencesManager, DisplayPreferencesManager>();
  77. // TODO search the assemblies instead of adding them manually?
  78. ServiceCollection.AddSingleton<IWebSocketListener, SessionWebSocketListener>();
  79. ServiceCollection.AddSingleton<IWebSocketListener, ActivityLogWebSocketListener>();
  80. ServiceCollection.AddSingleton<IWebSocketListener, ScheduledTasksWebSocketListener>();
  81. ServiceCollection.AddSingleton<IWebSocketListener, SessionInfoWebSocketListener>();
  82. base.RegisterServices();
  83. }
  84. /// <inheritdoc />
  85. protected override void RestartInternal() => Program.Restart();
  86. /// <inheritdoc />
  87. protected override IEnumerable<Assembly> GetAssembliesWithPartsInternal()
  88. {
  89. // Jellyfin.Server
  90. yield return typeof(CoreAppHost).Assembly;
  91. // Jellyfin.Server.Implementations
  92. yield return typeof(JellyfinDb).Assembly;
  93. }
  94. /// <inheritdoc />
  95. protected override void ShutdownInternal() => Program.Shutdown();
  96. }
  97. }