Startup.cs 3.3 KB

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