Startup.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.Controller;
  8. using MediaBrowser.Controller.Configuration;
  9. using Microsoft.AspNetCore.Builder;
  10. using Microsoft.AspNetCore.Hosting;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.Hosting;
  13. using Prometheus;
  14. namespace Jellyfin.Server
  15. {
  16. /// <summary>
  17. /// Startup configuration for the Kestrel webhost.
  18. /// </summary>
  19. public class Startup
  20. {
  21. private readonly IServerConfigurationManager _serverConfigurationManager;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="Startup" /> class.
  24. /// </summary>
  25. /// <param name="serverConfigurationManager">The server configuration manager.</param>
  26. public Startup(IServerConfigurationManager serverConfigurationManager)
  27. {
  28. _serverConfigurationManager = serverConfigurationManager;
  29. }
  30. /// <summary>
  31. /// Configures the service collection for the webhost.
  32. /// </summary>
  33. /// <param name="services">The service collection.</param>
  34. public void ConfigureServices(IServiceCollection services)
  35. {
  36. services.AddResponseCompression();
  37. services.AddHttpContextAccessor();
  38. services.AddJellyfinApi(
  39. _serverConfigurationManager.Configuration.BaseUrl.TrimStart('/'),
  40. _serverConfigurationManager.Configuration.CorsHosts);
  41. services.AddJellyfinApiSwagger();
  42. // configure custom legacy authentication
  43. services.AddCustomAuthentication();
  44. services.AddJellyfinApiAuthorization();
  45. services.AddHttpClient();
  46. }
  47. /// <summary>
  48. /// Configures the app builder for the webhost.
  49. /// </summary>
  50. /// <param name="app">The application builder.</param>
  51. /// <param name="env">The webhost environment.</param>
  52. /// <param name="serverApplicationHost">The server application host.</param>
  53. public void Configure(
  54. IApplicationBuilder app,
  55. IWebHostEnvironment env,
  56. IServerApplicationHost serverApplicationHost)
  57. {
  58. if (env.IsDevelopment())
  59. {
  60. app.UseDeveloperExceptionPage();
  61. }
  62. app.UseMiddleware<ExceptionMiddleware>();
  63. app.UseMiddleware<ResponseTimeMiddleware>();
  64. app.UseWebSockets();
  65. app.UseResponseCompression();
  66. // TODO app.UseMiddleware<WebSocketMiddleware>();
  67. app.UseAuthentication();
  68. app.UseJellyfinApiSwagger(_serverConfigurationManager);
  69. app.UseRouting();
  70. app.UseCors(ServerCorsPolicy.DefaultPolicyName);
  71. app.UseAuthorization();
  72. if (_serverConfigurationManager.Configuration.EnableMetrics)
  73. {
  74. // Must be registered after any middleware that could change HTTP response codes or the data will be bad
  75. app.UseHttpMetrics();
  76. }
  77. app.UseEndpoints(endpoints =>
  78. {
  79. endpoints.MapControllers();
  80. if (_serverConfigurationManager.Configuration.EnableMetrics)
  81. {
  82. endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
  83. }
  84. });
  85. app.Use(serverApplicationHost.ExecuteHttpHandlerAsync);
  86. // Add type descriptor for legacy datetime parsing.
  87. TypeDescriptor.AddAttributes(typeof(DateTime?), new TypeConverterAttribute(typeof(DateTimeTypeConverter)));
  88. }
  89. }
  90. }