CoreAppHost.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.Configuration;
  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="startupConfig">The <see cref="IConfiguration" /> to be used by the <see cref="CoreAppHost" />.</param>
  40. /// <param name="fileSystem">The <see cref="IFileSystem" /> 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. IConfiguration startupConfig,
  47. IFileSystem fileSystem,
  48. IServiceCollection collection)
  49. : base(
  50. applicationPaths,
  51. loggerFactory,
  52. options,
  53. startupConfig,
  54. fileSystem,
  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. // TODO search the assemblies instead of adding them manually?
  82. ServiceCollection.AddSingleton<IWebSocketListener, SessionWebSocketListener>();
  83. ServiceCollection.AddSingleton<IWebSocketListener, ActivityLogWebSocketListener>();
  84. ServiceCollection.AddSingleton<IWebSocketListener, ScheduledTasksWebSocketListener>();
  85. ServiceCollection.AddSingleton<IWebSocketListener, SessionInfoWebSocketListener>();
  86. base.RegisterServices();
  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(JellyfinDb).Assembly;
  97. }
  98. /// <inheritdoc />
  99. protected override void ShutdownInternal() => Program.Shutdown();
  100. }
  101. }