Startup.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.Middleware;
  7. using Jellyfin.Server.Models;
  8. using MediaBrowser.Common;
  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 IApplicationHost _applicationHost;
  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="applicationHost">The application host.</param>
  31. public Startup(IServerConfigurationManager serverConfigurationManager, IApplicationHost applicationHost)
  32. {
  33. _serverConfigurationManager = serverConfigurationManager;
  34. _applicationHost = applicationHost;
  35. }
  36. /// <summary>
  37. /// Configures the service collection for the webhost.
  38. /// </summary>
  39. /// <param name="services">The service collection.</param>
  40. public void ConfigureServices(IServiceCollection services)
  41. {
  42. services.AddResponseCompression();
  43. services.AddHttpContextAccessor();
  44. services.AddJellyfinApi(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/'), _applicationHost.GetApiPluginAssemblies());
  45. services.AddJellyfinApiSwagger();
  46. // configure custom legacy authentication
  47. services.AddCustomAuthentication();
  48. services.AddJellyfinApiAuthorization();
  49. var productHeader = new ProductInfoHeaderValue(_applicationHost.Name.Replace(' ', '-'), _applicationHost.ApplicationVersionString);
  50. services
  51. .AddHttpClient(NamedClient.Default, c =>
  52. {
  53. c.DefaultRequestHeaders.UserAgent.Add(productHeader);
  54. })
  55. .ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
  56. services.AddHttpClient(NamedClient.MusicBrainz, c =>
  57. {
  58. c.DefaultRequestHeaders.UserAgent.Add(productHeader);
  59. c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue($"({_applicationHost.ApplicationUserAgentAddress})"));
  60. })
  61. .ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
  62. }
  63. /// <summary>
  64. /// Configures the app builder for the webhost.
  65. /// </summary>
  66. /// <param name="app">The application builder.</param>
  67. /// <param name="env">The webhost environment.</param>
  68. /// <param name="serverApplicationHost">The server application host.</param>
  69. public void Configure(
  70. IApplicationBuilder app,
  71. IWebHostEnvironment env,
  72. IServerApplicationHost serverApplicationHost)
  73. {
  74. if (env.IsDevelopment())
  75. {
  76. app.UseDeveloperExceptionPage();
  77. }
  78. app.UseMiddleware<ExceptionMiddleware>();
  79. app.UseMiddleware<ResponseTimeMiddleware>();
  80. app.UseWebSockets();
  81. app.UseResponseCompression();
  82. // TODO app.UseMiddleware<WebSocketMiddleware>();
  83. app.UseAuthentication();
  84. app.UseJellyfinApiSwagger(_serverConfigurationManager);
  85. app.UseRouting();
  86. app.UseCors(ServerCorsPolicy.DefaultPolicyName);
  87. app.UseAuthorization();
  88. if (_serverConfigurationManager.Configuration.EnableMetrics)
  89. {
  90. // Must be registered after any middleware that could chagne HTTP response codes or the data will be bad
  91. app.UseHttpMetrics();
  92. }
  93. app.UseEndpoints(endpoints =>
  94. {
  95. endpoints.MapControllers();
  96. if (_serverConfigurationManager.Configuration.EnableMetrics)
  97. {
  98. endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
  99. }
  100. });
  101. app.Use(serverApplicationHost.ExecuteHttpHandlerAsync);
  102. // Add type descriptor for legacy datetime parsing.
  103. TypeDescriptor.AddAttributes(typeof(DateTime?), new TypeConverterAttribute(typeof(DateTimeTypeConverter)));
  104. }
  105. }
  106. }