CoreAppHost.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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="collection">The <see cref="IServiceCollection"/> to be used by the <see cref="CoreAppHost"/>.</param>
  41. public CoreAppHost(
  42. IServerApplicationPaths applicationPaths,
  43. ILoggerFactory loggerFactory,
  44. IStartupOptions options,
  45. IFileSystem fileSystem,
  46. IServiceCollection collection)
  47. : base(
  48. applicationPaths,
  49. loggerFactory,
  50. options,
  51. fileSystem,
  52. collection)
  53. {
  54. }
  55. /// <inheritdoc/>
  56. protected override void RegisterServices()
  57. {
  58. // Register an image encoder
  59. bool useSkiaEncoder = SkiaEncoder.IsNativeLibAvailable();
  60. Type imageEncoderType = useSkiaEncoder
  61. ? typeof(SkiaEncoder)
  62. : typeof(NullImageEncoder);
  63. ServiceCollection.AddSingleton(typeof(IImageEncoder), imageEncoderType);
  64. // Log a warning if the Skia encoder could not be used
  65. if (!useSkiaEncoder)
  66. {
  67. Logger.LogWarning($"Skia not available. Will fallback to {nameof(NullImageEncoder)}.");
  68. }
  69. ServiceCollection.AddDbContextPool<JellyfinDb>(
  70. options => options.UseSqlite($"Filename={Path.Combine(ApplicationPaths.DataPath, "jellyfin.db")}"));
  71. ServiceCollection.AddEventServices();
  72. ServiceCollection.AddSingleton<IBaseItemManager, BaseItemManager>();
  73. ServiceCollection.AddSingleton<IEventManager, EventManager>();
  74. ServiceCollection.AddSingleton<JellyfinDbProvider>();
  75. ServiceCollection.AddSingleton<IActivityManager, ActivityManager>();
  76. ServiceCollection.AddSingleton<IUserManager, UserManager>();
  77. ServiceCollection.AddSingleton<IDisplayPreferencesManager, DisplayPreferencesManager>();
  78. // TODO search the assemblies instead of adding them manually?
  79. ServiceCollection.AddSingleton<IWebSocketListener, SessionWebSocketListener>();
  80. ServiceCollection.AddSingleton<IWebSocketListener, ActivityLogWebSocketListener>();
  81. ServiceCollection.AddSingleton<IWebSocketListener, ScheduledTasksWebSocketListener>();
  82. ServiceCollection.AddSingleton<IWebSocketListener, SessionInfoWebSocketListener>();
  83. base.RegisterServices();
  84. }
  85. /// <inheritdoc />
  86. protected override void RestartInternal() => Program.Restart();
  87. /// <inheritdoc />
  88. protected override IEnumerable<Assembly> GetAssembliesWithPartsInternal()
  89. {
  90. // Jellyfin.Server
  91. yield return typeof(CoreAppHost).Assembly;
  92. // Jellyfin.Server.Implementations
  93. yield return typeof(JellyfinDb).Assembly;
  94. }
  95. /// <inheritdoc />
  96. protected override void ShutdownInternal() => Program.Shutdown();
  97. }
  98. }