Startup.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.ComponentModel;
  3. using System.Net.Http.Headers;
  4. using Jellyfin.Api.TypeConverters;
  5. using Jellyfin.Server.Extensions;
  6. using Jellyfin.Server.Implementations;
  7. using Jellyfin.Server.Middleware;
  8. using Jellyfin.Server.Models;
  9. using MediaBrowser.Common.Net;
  10. using MediaBrowser.Controller;
  11. using MediaBrowser.Controller.Configuration;
  12. using Microsoft.AspNetCore.Builder;
  13. using Microsoft.AspNetCore.Hosting;
  14. using Microsoft.Extensions.DependencyInjection;
  15. using Microsoft.Extensions.Hosting;
  16. using Prometheus;
  17. namespace Jellyfin.Server
  18. {
  19. /// <summary>
  20. /// Startup configuration for the Kestrel webhost.
  21. /// </summary>
  22. public class Startup
  23. {
  24. private readonly IServerConfigurationManager _serverConfigurationManager;
  25. private readonly IServerApplicationHost _serverApplicationHost;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="Startup" /> class.
  28. /// </summary>
  29. /// <param name="serverConfigurationManager">The server configuration manager.</param>
  30. /// <param name="serverApplicationHost">The server application host.</param>
  31. public Startup(
  32. IServerConfigurationManager serverConfigurationManager,
  33. IServerApplicationHost serverApplicationHost)
  34. {
  35. _serverConfigurationManager = serverConfigurationManager;
  36. _serverApplicationHost = serverApplicationHost;
  37. }
  38. /// <summary>
  39. /// Configures the service collection for the webhost.
  40. /// </summary>
  41. /// <param name="services">The service collection.</param>
  42. public void ConfigureServices(IServiceCollection services)
  43. {
  44. services.AddResponseCompression();
  45. services.AddHttpContextAccessor();
  46. services.AddHttpsRedirection(options =>
  47. {
  48. options.HttpsPort = _serverApplicationHost.HttpsPort;
  49. });
  50. services.AddJellyfinApi(
  51. _serverConfigurationManager.Configuration.BaseUrl.TrimStart('/'),
  52. _serverApplicationHost.GetApiPluginAssemblies());
  53. services.AddJellyfinApiSwagger();
  54. // configure custom legacy authentication
  55. services.AddCustomAuthentication();
  56. services.AddJellyfinApiAuthorization();
  57. var productHeader = new ProductInfoHeaderValue(
  58. _serverApplicationHost.Name.Replace(' ', '-'),
  59. _serverApplicationHost.ApplicationVersionString);
  60. services
  61. .AddHttpClient(NamedClient.Default, c =>
  62. {
  63. c.DefaultRequestHeaders.UserAgent.Add(productHeader);
  64. })
  65. .ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
  66. services.AddHttpClient(NamedClient.MusicBrainz, c =>
  67. {
  68. c.DefaultRequestHeaders.UserAgent.Add(productHeader);
  69. c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue($"({_serverApplicationHost.ApplicationUserAgentAddress})"));
  70. })
  71. .ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
  72. services.AddHealthChecks()
  73. .AddDbContextCheck<JellyfinDb>();
  74. }
  75. /// <summary>
  76. /// Configures the app builder for the webhost.
  77. /// </summary>
  78. /// <param name="app">The application builder.</param>
  79. /// <param name="env">The webhost environment.</param>
  80. public void Configure(
  81. IApplicationBuilder app,
  82. IWebHostEnvironment env)
  83. {
  84. if (env.IsDevelopment())
  85. {
  86. app.UseDeveloperExceptionPage();
  87. }
  88. app.UseMiddleware<ExceptionMiddleware>();
  89. app.UseMiddleware<ResponseTimeMiddleware>();
  90. app.UseWebSockets();
  91. app.UseResponseCompression();
  92. app.UseCors(ServerCorsPolicy.DefaultPolicyName);
  93. if (_serverConfigurationManager.Configuration.RequireHttps
  94. && _serverApplicationHost.ListenWithHttps)
  95. {
  96. app.UseHttpsRedirection();
  97. }
  98. app.UseStaticFiles();
  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. }