Startup.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Jellyfin.Server.Extensions;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Configuration;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Hosting;
  8. using Prometheus;
  9. namespace Jellyfin.Server
  10. {
  11. /// <summary>
  12. /// Startup configuration for the Kestrel webhost.
  13. /// </summary>
  14. public class Startup
  15. {
  16. private readonly IServerConfigurationManager _serverConfigurationManager;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="Startup" /> class.
  19. /// </summary>
  20. /// <param name="serverConfigurationManager">The server configuration manager.</param>
  21. public Startup(IServerConfigurationManager serverConfigurationManager)
  22. {
  23. _serverConfigurationManager = serverConfigurationManager;
  24. }
  25. /// <summary>
  26. /// Configures the service collection for the webhost.
  27. /// </summary>
  28. /// <param name="services">The service collection.</param>
  29. public void ConfigureServices(IServiceCollection services)
  30. {
  31. services.AddResponseCompression();
  32. services.AddHttpContextAccessor();
  33. services.AddJellyfinApi(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/'));
  34. services.AddJellyfinApiSwagger();
  35. // configure custom legacy authentication
  36. services.AddCustomAuthentication();
  37. services.AddJellyfinApiAuthorization();
  38. }
  39. /// <summary>
  40. /// Configures the app builder for the webhost.
  41. /// </summary>
  42. /// <param name="app">The application builder.</param>
  43. /// <param name="env">The webhost environment.</param>
  44. /// <param name="serverApplicationHost">The server application host.</param>
  45. public void Configure(
  46. IApplicationBuilder app,
  47. IWebHostEnvironment env,
  48. IServerApplicationHost serverApplicationHost)
  49. {
  50. if (env.IsDevelopment())
  51. {
  52. app.UseDeveloperExceptionPage();
  53. }
  54. app.UseWebSockets();
  55. app.UseResponseCompression();
  56. // TODO app.UseMiddleware<WebSocketMiddleware>();
  57. // TODO use when old API is removed: app.UseAuthentication();
  58. app.UseJellyfinApiSwagger();
  59. app.UseRouting();
  60. app.UseAuthorization();
  61. if (_serverConfigurationManager.Configuration.EnableMetrics)
  62. {
  63. // Must be registered after any middleware that could chagne HTTP response codes or the data will be bad
  64. app.UseHttpMetrics();
  65. }
  66. app.UseEndpoints(endpoints =>
  67. {
  68. endpoints.MapControllers();
  69. if (_serverConfigurationManager.Configuration.EnableMetrics)
  70. {
  71. endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
  72. }
  73. });
  74. app.Use(serverApplicationHost.ExecuteHttpHandlerAsync);
  75. }
  76. }
  77. }