CoreAppHost.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 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. public CoreAppHost(
  44. IServerApplicationPaths applicationPaths,
  45. ILoggerFactory loggerFactory,
  46. IStartupOptions options,
  47. IConfiguration startupConfig)
  48. : base(
  49. applicationPaths,
  50. loggerFactory,
  51. options,
  52. startupConfig)
  53. {
  54. }
  55. /// <inheritdoc/>
  56. protected override void RegisterServices(IServiceCollection serviceCollection)
  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 {ImageEncoder}.", nameof(NullImageEncoder));
  68. }
  69. serviceCollection.AddDbContextPool<JellyfinDb>(
  70. options => options
  71. .UseLoggerFactory(LoggerFactory)
  72. .UseSqlite($"Filename={Path.Combine(ApplicationPaths.DataPath, "jellyfin.db")}"));
  73. serviceCollection.AddEventServices();
  74. serviceCollection.AddSingleton<IBaseItemManager, BaseItemManager>();
  75. serviceCollection.AddSingleton<IEventManager, EventManager>();
  76. serviceCollection.AddSingleton<JellyfinDbProvider>();
  77. serviceCollection.AddSingleton<IActivityManager, ActivityManager>();
  78. serviceCollection.AddSingleton<IUserManager, UserManager>();
  79. serviceCollection.AddSingleton<IDisplayPreferencesManager, DisplayPreferencesManager>();
  80. serviceCollection.AddSingleton<IDeviceManager, DeviceManager>();
  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. serviceCollection.AddSingleton<IAuthorizationContext, AuthorizationContext>();
  87. serviceCollection.AddScoped<IAuthenticationManager, AuthenticationManager>();
  88. base.RegisterServices(serviceCollection);
  89. }
  90. /// <inheritdoc />
  91. protected override void RestartInternal() => Program.Restart();
  92. /// <inheritdoc />
  93. protected override IEnumerable<Assembly> GetAssembliesWithPartsInternal()
  94. {
  95. // Jellyfin.Server
  96. yield return typeof(CoreAppHost).Assembly;
  97. // Jellyfin.Server.Implementations
  98. yield return typeof(JellyfinDb).Assembly;
  99. }
  100. /// <inheritdoc />
  101. protected override void ShutdownInternal() => Program.Shutdown();
  102. }
  103. }