Startup.cs 3.4 KB

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