Startup.cs 3.9 KB

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