Startup.cs 4.7 KB

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