CoreAppHost.cs 4.8 KB

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