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