Startup.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. .ConfigureHttpClient((sp, options) => {})
  48. .ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler())
  49. .AddHttpMessageHandler<UserAgentDelegatingHandler>();
  50. }
  51. /// <summary>
  52. /// Configures the app builder for the webhost.
  53. /// </summary>
  54. /// <param name="app">The application builder.</param>
  55. /// <param name="env">The webhost environment.</param>
  56. /// <param name="serverApplicationHost">The server application host.</param>
  57. public void Configure(
  58. IApplicationBuilder app,
  59. IWebHostEnvironment env,
  60. IServerApplicationHost serverApplicationHost)
  61. {
  62. if (env.IsDevelopment())
  63. {
  64. app.UseDeveloperExceptionPage();
  65. }
  66. app.UseMiddleware<ExceptionMiddleware>();
  67. app.UseMiddleware<ResponseTimeMiddleware>();
  68. app.UseWebSockets();
  69. app.UseResponseCompression();
  70. // TODO app.UseMiddleware<WebSocketMiddleware>();
  71. app.UseAuthentication();
  72. app.UseJellyfinApiSwagger(_serverConfigurationManager);
  73. app.UseRouting();
  74. app.UseCors(ServerCorsPolicy.DefaultPolicyName);
  75. app.UseAuthorization();
  76. if (_serverConfigurationManager.Configuration.EnableMetrics)
  77. {
  78. // Must be registered after any middleware that could chagne HTTP response codes or the data will be bad
  79. app.UseHttpMetrics();
  80. }
  81. app.UseEndpoints(endpoints =>
  82. {
  83. endpoints.MapControllers();
  84. if (_serverConfigurationManager.Configuration.EnableMetrics)
  85. {
  86. endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
  87. }
  88. });
  89. app.Use(serverApplicationHost.ExecuteHttpHandlerAsync);
  90. // Add type descriptor for legacy datetime parsing.
  91. TypeDescriptor.AddAttributes(typeof(DateTime?), new TypeConverterAttribute(typeof(DateTimeTypeConverter)));
  92. }
  93. }
  94. }