CoreAppHost.cs 3.8 KB

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