CoreAppHost.cs 3.4 KB

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