Startup.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. app.Use(serverApplicationHost.ExecuteWebsocketHandlerAsync);
  58. // TODO use when old API is removed: app.UseAuthentication();
  59. app.UseJellyfinApiSwagger();
  60. app.UseRouting();
  61. app.UseAuthorization();
  62. if (_serverConfigurationManager.Configuration.EnableMetrics)
  63. {
  64. // Must be registered after any middleware that could chagne HTTP response codes or the data will be bad
  65. app.UseHttpMetrics();
  66. }
  67. app.UseEndpoints(endpoints =>
  68. {
  69. endpoints.MapControllers();
  70. if (_serverConfigurationManager.Configuration.EnableMetrics)
  71. {
  72. endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
  73. }
  74. });
  75. app.Use(serverApplicationHost.ExecuteHttpHandlerAsync);
  76. }
  77. }
  78. }