CoreAppHost.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.Lyrics;
  23. using MediaBrowser.Controller.Net;
  24. using MediaBrowser.Controller.Security;
  25. using MediaBrowser.Model.Activity;
  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. public CoreAppHost(
  45. IServerApplicationPaths applicationPaths,
  46. ILoggerFactory loggerFactory,
  47. IStartupOptions options,
  48. IConfiguration startupConfig)
  49. : base(
  50. applicationPaths,
  51. loggerFactory,
  52. options,
  53. startupConfig)
  54. {
  55. }
  56. /// <inheritdoc/>
  57. protected override void RegisterServices(IServiceCollection serviceCollection)
  58. {
  59. // Register an image encoder
  60. bool useSkiaEncoder = SkiaEncoder.IsNativeLibAvailable();
  61. Type imageEncoderType = useSkiaEncoder
  62. ? typeof(SkiaEncoder)
  63. : typeof(NullImageEncoder);
  64. serviceCollection.AddSingleton(typeof(IImageEncoder), imageEncoderType);
  65. // Log a warning if the Skia encoder could not be used
  66. if (!useSkiaEncoder)
  67. {
  68. Logger.LogWarning("Skia not available. Will fallback to {ImageEncoder}.", nameof(NullImageEncoder));
  69. }
  70. serviceCollection.AddDbContextPool<JellyfinDb>(
  71. options => options
  72. .UseLoggerFactory(LoggerFactory)
  73. .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. serviceCollection.AddSingleton<IDeviceManager, DeviceManager>();
  82. // TODO search the assemblies instead of adding them manually?
  83. serviceCollection.AddSingleton<IWebSocketListener, SessionWebSocketListener>();
  84. serviceCollection.AddSingleton<IWebSocketListener, ActivityLogWebSocketListener>();
  85. serviceCollection.AddSingleton<IWebSocketListener, ScheduledTasksWebSocketListener>();
  86. serviceCollection.AddSingleton<IWebSocketListener, SessionInfoWebSocketListener>();
  87. serviceCollection.AddSingleton<IAuthorizationContext, AuthorizationContext>();
  88. serviceCollection.AddScoped<IAuthenticationManager, AuthenticationManager>();
  89. foreach (var type in GetExportTypes<ILyricProvider>())
  90. {
  91. serviceCollection.AddSingleton(typeof(ILyricProvider), type);
  92. }
  93. base.RegisterServices(serviceCollection);
  94. }
  95. /// <inheritdoc />
  96. protected override void RestartInternal() => Program.Restart();
  97. /// <inheritdoc />
  98. protected override IEnumerable<Assembly> GetAssembliesWithPartsInternal()
  99. {
  100. // Jellyfin.Server
  101. yield return typeof(CoreAppHost).Assembly;
  102. // Jellyfin.Server.Implementations
  103. yield return typeof(JellyfinDb).Assembly;
  104. }
  105. /// <inheritdoc />
  106. protected override void ShutdownInternal() => Program.Shutdown();
  107. }
  108. }