Startup.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/'));
  39. services.AddJellyfinApiSwagger();
  40. // configure custom legacy authentication
  41. services.AddCustomAuthentication();
  42. services.AddJellyfinApiAuthorization();
  43. services.AddHttpClient();
  44. }
  45. /// <summary>
  46. /// Configures the app builder for the webhost.
  47. /// </summary>
  48. /// <param name="app">The application builder.</param>
  49. /// <param name="env">The webhost environment.</param>
  50. /// <param name="serverApplicationHost">The server application host.</param>
  51. public void Configure(
  52. IApplicationBuilder app,
  53. IWebHostEnvironment env,
  54. IServerApplicationHost serverApplicationHost)
  55. {
  56. if (env.IsDevelopment())
  57. {
  58. app.UseDeveloperExceptionPage();
  59. }
  60. app.UseMiddleware<ExceptionMiddleware>();
  61. app.UseMiddleware<ResponseTimeMiddleware>();
  62. app.UseWebSockets();
  63. app.UseResponseCompression();
  64. // TODO app.UseMiddleware<WebSocketMiddleware>();
  65. app.UseAuthentication();
  66. app.UseJellyfinApiSwagger(_serverConfigurationManager);
  67. app.UseRouting();
  68. app.UseCors(ServerCorsPolicy.DefaultPolicyName);
  69. app.UseAuthorization();
  70. if (_serverConfigurationManager.Configuration.EnableMetrics)
  71. {
  72. // Must be registered after any middleware that could chagne HTTP response codes or the data will be bad
  73. app.UseHttpMetrics();
  74. }
  75. app.UseEndpoints(endpoints =>
  76. {
  77. endpoints.MapControllers();
  78. if (_serverConfigurationManager.Configuration.EnableMetrics)
  79. {
  80. endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
  81. }
  82. });
  83. app.Use(serverApplicationHost.ExecuteHttpHandlerAsync);
  84. // Add type descriptor for legacy datetime parsing.
  85. TypeDescriptor.AddAttributes(typeof(DateTime?), new TypeConverterAttribute(typeof(DateTimeTypeConverter)));
  86. }
  87. }
  88. }