jellyfin/Jellyfin.Server/Startup.cs

125 lines
4.7 KiB
C#
Raw Normal View History

using System;
using System.ComponentModel;
2020-08-31 14:47:38 +00:00
using System.Net.Http.Headers;
using Jellyfin.Api.TypeConverters;
using Jellyfin.Server.Extensions;
2020-04-24 03:24:40 +00:00
using Jellyfin.Server.Middleware;
2020-06-01 17:03:08 +00:00
using Jellyfin.Server.Models;
2020-08-31 14:47:38 +00:00
using MediaBrowser.Common;
2020-08-19 12:31:45 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2020-04-26 01:35:51 +00:00
using Prometheus;
namespace Jellyfin.Server
{
/// <summary>
/// Startup configuration for the Kestrel webhost.
/// </summary>
public class Startup
{
private readonly IServerConfigurationManager _serverConfigurationManager;
2020-08-31 14:47:38 +00:00
private readonly IApplicationHost _applicationHost;
/// <summary>
/// Initializes a new instance of the <see cref="Startup" /> class.
/// </summary>
/// <param name="serverConfigurationManager">The server configuration manager.</param>
2020-08-31 14:47:38 +00:00
/// <param name="applicationHost">The application host.</param>
public Startup(IServerConfigurationManager serverConfigurationManager, IApplicationHost applicationHost)
{
_serverConfigurationManager = serverConfigurationManager;
2020-08-31 14:47:38 +00:00
_applicationHost = applicationHost;
}
/// <summary>
/// Configures the service collection for the webhost.
/// </summary>
/// <param name="services">The service collection.</param>
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression();
services.AddHttpContextAccessor();
2020-08-31 15:53:55 +00:00
services.AddJellyfinApi(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/'), _applicationHost.GetApiPluginAssemblies());
services.AddJellyfinApiSwagger();
// configure custom legacy authentication
services.AddCustomAuthentication();
services.AddJellyfinApiAuthorization();
2020-08-19 12:31:45 +00:00
2020-08-31 15:15:20 +00:00
var productHeader = new ProductInfoHeaderValue(_applicationHost.Name.Replace(' ', '-'), _applicationHost.ApplicationVersionString);
2020-08-19 12:31:45 +00:00
services
2020-08-31 14:47:38 +00:00
.AddHttpClient(NamedClient.Default, c =>
{
2020-08-31 15:15:20 +00:00
c.DefaultRequestHeaders.UserAgent.Add(productHeader);
2020-08-31 14:47:38 +00:00
})
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
services.AddHttpClient(NamedClient.MusicBrainz, c =>
{
2020-08-31 15:15:20 +00:00
c.DefaultRequestHeaders.UserAgent.Add(productHeader);
2020-08-31 14:52:21 +00:00
c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue($"({_applicationHost.ApplicationUserAgentAddress})"));
2020-08-31 14:47:38 +00:00
})
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
}
/// <summary>
/// Configures the app builder for the webhost.
/// </summary>
/// <param name="app">The application builder.</param>
/// <param name="env">The webhost environment.</param>
/// <param name="serverApplicationHost">The server application host.</param>
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
IServerApplicationHost serverApplicationHost)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
2020-04-24 03:24:40 +00:00
app.UseMiddleware<ExceptionMiddleware>();
2020-04-21 13:36:22 +00:00
app.UseMiddleware<ResponseTimeMiddleware>();
app.UseWebSockets();
app.UseResponseCompression();
// TODO app.UseMiddleware<WebSocketMiddleware>();
2020-06-01 17:03:08 +00:00
app.UseAuthentication();
app.UseJellyfinApiSwagger(_serverConfigurationManager);
app.UseRouting();
2020-06-01 17:03:08 +00:00
app.UseCors(ServerCorsPolicy.DefaultPolicyName);
app.UseAuthorization();
if (_serverConfigurationManager.Configuration.EnableMetrics)
{
2020-04-27 12:42:46 +00:00
// Must be registered after any middleware that could chagne HTTP response codes or the data will be bad
app.UseHttpMetrics();
}
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
if (_serverConfigurationManager.Configuration.EnableMetrics)
{
2020-04-26 15:52:01 +00:00
endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
}
});
app.Use(serverApplicationHost.ExecuteHttpHandlerAsync);
// Add type descriptor for legacy datetime parsing.
TypeDescriptor.AddAttributes(typeof(DateTime?), new TypeConverterAttribute(typeof(DateTimeTypeConverter)));
}
}
}