Startup.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.ComponentModel;
  3. using System.Data.OleDb;
  4. using System.Net.Http.Headers;
  5. using Jellyfin.Api.TypeConverters;
  6. using Jellyfin.Server.Extensions;
  7. using Jellyfin.Server.HealthChecks;
  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 Prometheus;
  18. namespace Jellyfin.Server
  19. {
  20. /// <summary>
  21. /// Startup configuration for the Kestrel webhost.
  22. /// </summary>
  23. public class Startup
  24. {
  25. private readonly IServerConfigurationManager _serverConfigurationManager;
  26. private readonly IServerApplicationHost _serverApplicationHost;
  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. /// <param name="serverApplicationHost">The server application host.</param>
  32. public Startup(
  33. IServerConfigurationManager serverConfigurationManager,
  34. IServerApplicationHost serverApplicationHost)
  35. {
  36. _serverConfigurationManager = serverConfigurationManager;
  37. _serverApplicationHost = serverApplicationHost;
  38. }
  39. /// <summary>
  40. /// Configures the service collection for the webhost.
  41. /// </summary>
  42. /// <param name="services">The service collection.</param>
  43. public void ConfigureServices(IServiceCollection services)
  44. {
  45. services.AddResponseCompression();
  46. services.AddHttpContextAccessor();
  47. services.AddHttpsRedirection(options =>
  48. {
  49. options.HttpsPort = _serverApplicationHost.HttpsPort;
  50. });
  51. services.AddJellyfinApi(
  52. _serverConfigurationManager.Configuration.BaseUrl.TrimStart('/'),
  53. _serverApplicationHost.GetApiPluginAssemblies());
  54. services.AddJellyfinApiSwagger();
  55. // configure custom legacy authentication
  56. services.AddCustomAuthentication();
  57. services.AddJellyfinApiAuthorization();
  58. var productHeader = new ProductInfoHeaderValue(
  59. _serverApplicationHost.Name.Replace(' ', '-'),
  60. _serverApplicationHost.ApplicationVersionString);
  61. services
  62. .AddHttpClient(NamedClient.Default, c =>
  63. {
  64. c.DefaultRequestHeaders.UserAgent.Add(productHeader);
  65. })
  66. .ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
  67. services.AddHttpClient(NamedClient.MusicBrainz, c =>
  68. {
  69. c.DefaultRequestHeaders.UserAgent.Add(productHeader);
  70. c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue($"({_serverApplicationHost.ApplicationUserAgentAddress})"));
  71. })
  72. .ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
  73. services.AddHealthChecks()
  74. .AddCheck<JellyfinDbHealthCheck>("JellyfinDb");
  75. }
  76. /// <summary>
  77. /// Configures the app builder for the webhost.
  78. /// </summary>
  79. /// <param name="app">The application builder.</param>
  80. /// <param name="env">The webhost environment.</param>
  81. public void Configure(
  82. IApplicationBuilder app,
  83. IWebHostEnvironment env)
  84. {
  85. if (env.IsDevelopment())
  86. {
  87. app.UseDeveloperExceptionPage();
  88. }
  89. app.UseMiddleware<ExceptionMiddleware>();
  90. app.UseMiddleware<ResponseTimeMiddleware>();
  91. app.UseWebSockets();
  92. app.UseResponseCompression();
  93. app.UseCors(ServerCorsPolicy.DefaultPolicyName);
  94. if (_serverConfigurationManager.Configuration.RequireHttps
  95. && _serverApplicationHost.ListenWithHttps)
  96. {
  97. app.UseHttpsRedirection();
  98. }
  99. app.UseAuthentication();
  100. app.UseJellyfinApiSwagger(_serverConfigurationManager);
  101. app.UseRouting();
  102. app.UseAuthorization();
  103. if (_serverConfigurationManager.Configuration.EnableMetrics)
  104. {
  105. // Must be registered after any middleware that could chagne HTTP response codes or the data will be bad
  106. app.UseHttpMetrics();
  107. }
  108. app.UseLanFiltering();
  109. app.UseIpBasedAccessValidation();
  110. app.UseBaseUrlRedirection();
  111. app.UseWebSocketHandler();
  112. app.UseServerStartupMessage();
  113. app.UseEndpoints(endpoints =>
  114. {
  115. endpoints.MapControllers();
  116. if (_serverConfigurationManager.Configuration.EnableMetrics)
  117. {
  118. endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
  119. }
  120. endpoints.MapHealthChecks(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/health");
  121. });
  122. // Add type descriptor for legacy datetime parsing.
  123. TypeDescriptor.AddAttributes(typeof(DateTime?), new TypeConverterAttribute(typeof(DateTimeTypeConverter)));
  124. }
  125. }
  126. }