CoreAppHost.cs 5.1 KB

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