CoreAppHost.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Emby.Drawing;
  5. using Emby.Server.Implementations;
  6. using Jellyfin.Drawing.Skia;
  7. using Jellyfin.Server.Implementations;
  8. using Jellyfin.Server.Implementations.Activity;
  9. using Jellyfin.Server.Implementations.Events;
  10. using Jellyfin.Server.Implementations.Users;
  11. using MediaBrowser.Common.Net;
  12. using MediaBrowser.Controller;
  13. using MediaBrowser.Controller.Drawing;
  14. using MediaBrowser.Controller.Events;
  15. using MediaBrowser.Controller.Library;
  16. using MediaBrowser.Model.Activity;
  17. using MediaBrowser.Model.IO;
  18. using Microsoft.Extensions.DependencyInjection;
  19. using Microsoft.Extensions.Logging;
  20. namespace Jellyfin.Server
  21. {
  22. /// <summary>
  23. /// Implementation of the abstract <see cref="ApplicationHost" /> class.
  24. /// </summary>
  25. public class CoreAppHost : ApplicationHost
  26. {
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="CoreAppHost" /> class.
  29. /// </summary>
  30. /// <param name="applicationPaths">The <see cref="ServerApplicationPaths" /> to be used by the <see cref="CoreAppHost" />.</param>
  31. /// <param name="loggerFactory">The <see cref="ILoggerFactory" /> to be used by the <see cref="CoreAppHost" />.</param>
  32. /// <param name="options">The <see cref="StartupOptions" /> to be used by the <see cref="CoreAppHost" />.</param>
  33. /// <param name="fileSystem">The <see cref="IFileSystem" /> to be used by the <see cref="CoreAppHost" />.</param>
  34. /// <param name="networkManager">The <see cref="INetworkManager" /> to be used by the <see cref="CoreAppHost" />.</param>
  35. /// <param name="collection">The <see cref="IServiceCollection"/> to be used by the <see cref="CoreAppHost"/>.</param>
  36. public CoreAppHost(
  37. IServerApplicationPaths applicationPaths,
  38. ILoggerFactory loggerFactory,
  39. IStartupOptions options,
  40. IFileSystem fileSystem,
  41. INetworkManager networkManager,
  42. IServiceCollection collection)
  43. : base(
  44. applicationPaths,
  45. loggerFactory,
  46. options,
  47. fileSystem,
  48. networkManager,
  49. collection)
  50. {
  51. }
  52. /// <inheritdoc/>
  53. protected override void RegisterServices()
  54. {
  55. // Register an image encoder
  56. bool useSkiaEncoder = SkiaEncoder.IsNativeLibAvailable();
  57. Type imageEncoderType = useSkiaEncoder
  58. ? typeof(SkiaEncoder)
  59. : typeof(NullImageEncoder);
  60. ServiceCollection.AddSingleton(typeof(IImageEncoder), imageEncoderType);
  61. // Log a warning if the Skia encoder could not be used
  62. if (!useSkiaEncoder)
  63. {
  64. Logger.LogWarning($"Skia not available. Will fallback to {nameof(NullImageEncoder)}.");
  65. }
  66. // TODO: Set up scoping and use AddDbContextPool,
  67. // can't register as Transient since tracking transient in GC is funky
  68. // serviceCollection.AddDbContext<JellyfinDb>(
  69. // options => options
  70. // .UseSqlite($"Filename={Path.Combine(ApplicationPaths.DataPath, "jellyfin.db")}"),
  71. // ServiceLifetime.Transient);
  72. ServiceCollection.AddEventServices();
  73. ServiceCollection.AddSingleton<IEventManager, EventManager>();
  74. ServiceCollection.AddSingleton<JellyfinDbProvider>();
  75. ServiceCollection.AddSingleton<IActivityManager, ActivityManager>();
  76. ServiceCollection.AddSingleton<IUserManager, UserManager>();
  77. ServiceCollection.AddSingleton<IDisplayPreferencesManager, DisplayPreferencesManager>();
  78. base.RegisterServices();
  79. }
  80. /// <inheritdoc />
  81. protected override void RestartInternal() => Program.Restart();
  82. /// <inheritdoc />
  83. protected override IEnumerable<Assembly> GetAssembliesWithPartsInternal()
  84. {
  85. // Jellyfin.Server
  86. yield return typeof(CoreAppHost).Assembly;
  87. // Jellyfin.Server.Implementations
  88. yield return typeof(JellyfinDb).Assembly;
  89. }
  90. /// <inheritdoc />
  91. protected override void ShutdownInternal() => Program.Shutdown();
  92. }
  93. }