CoreAppHost.cs 5.3 KB

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