Startup.cs 3.9 KB

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