2020-08-10 10:58:06 -07:00
|
|
|
using System;
|
|
|
|
using System.ComponentModel;
|
2020-08-31 07:47:38 -07:00
|
|
|
using System.Net.Http.Headers;
|
2020-08-10 10:58:06 -07:00
|
|
|
using Jellyfin.Api.TypeConverters;
|
2019-11-24 07:27:58 -07:00
|
|
|
using Jellyfin.Server.Extensions;
|
2020-04-23 20:24:40 -07:00
|
|
|
using Jellyfin.Server.Middleware;
|
2020-06-01 10:03:08 -07:00
|
|
|
using Jellyfin.Server.Models;
|
2020-08-31 07:47:38 -07:00
|
|
|
using MediaBrowser.Common;
|
2020-08-19 05:31:45 -07:00
|
|
|
using MediaBrowser.Common.Net;
|
2019-11-24 07:27:58 -07:00
|
|
|
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-25 18:35:51 -07:00
|
|
|
using Prometheus;
|
2019-11-24 07:27:58 -07:00
|
|
|
|
|
|
|
namespace Jellyfin.Server
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Startup configuration for the Kestrel webhost.
|
|
|
|
/// </summary>
|
|
|
|
public class Startup
|
|
|
|
{
|
|
|
|
private readonly IServerConfigurationManager _serverConfigurationManager;
|
2020-09-02 15:31:42 -07:00
|
|
|
private readonly IServerApplicationHost _serverApplicationHost;
|
2019-11-24 07:27:58 -07:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="Startup" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="serverConfigurationManager">The server configuration manager.</param>
|
2020-09-02 15:31:42 -07:00
|
|
|
/// <param name="serverApplicationHost">The server application host.</param>
|
|
|
|
public Startup(
|
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
|
|
|
IServerApplicationHost serverApplicationHost)
|
2019-11-24 07:27:58 -07:00
|
|
|
{
|
|
|
|
_serverConfigurationManager = serverConfigurationManager;
|
2020-09-02 15:31:42 -07:00
|
|
|
_serverApplicationHost = serverApplicationHost;
|
2019-11-24 07:27:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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-09-02 15:38:52 -07:00
|
|
|
services.AddHttpsRedirection(options =>
|
|
|
|
{
|
|
|
|
options.HttpsPort = _serverApplicationHost.HttpsPort;
|
|
|
|
});
|
2020-09-02 15:31:42 -07:00
|
|
|
services.AddJellyfinApi(
|
|
|
|
_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/'),
|
|
|
|
_serverApplicationHost.GetApiPluginAssemblies());
|
2019-11-24 07:27:58 -07:00
|
|
|
|
|
|
|
services.AddJellyfinApiSwagger();
|
|
|
|
|
|
|
|
// configure custom legacy authentication
|
|
|
|
services.AddCustomAuthentication();
|
|
|
|
|
|
|
|
services.AddJellyfinApiAuthorization();
|
2020-08-19 05:31:45 -07:00
|
|
|
|
2020-09-02 15:31:42 -07:00
|
|
|
var productHeader = new ProductInfoHeaderValue(
|
|
|
|
_serverApplicationHost.Name.Replace(' ', '-'),
|
|
|
|
_serverApplicationHost.ApplicationVersionString);
|
2020-08-19 05:31:45 -07:00
|
|
|
services
|
2020-08-31 07:47:38 -07:00
|
|
|
.AddHttpClient(NamedClient.Default, c =>
|
|
|
|
{
|
2020-08-31 08:15:20 -07:00
|
|
|
c.DefaultRequestHeaders.UserAgent.Add(productHeader);
|
2020-08-31 07:47:38 -07:00
|
|
|
})
|
|
|
|
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
|
|
|
|
|
|
|
|
services.AddHttpClient(NamedClient.MusicBrainz, c =>
|
|
|
|
{
|
2020-08-31 08:15:20 -07:00
|
|
|
c.DefaultRequestHeaders.UserAgent.Add(productHeader);
|
2020-09-02 15:31:42 -07:00
|
|
|
c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue($"({_serverApplicationHost.ApplicationUserAgentAddress})"));
|
2020-08-31 07:47:38 -07:00
|
|
|
})
|
|
|
|
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
|
2019-11-24 07:27:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Configures the app builder for the webhost.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="app">The application builder.</param>
|
|
|
|
/// <param name="env">The webhost environment.</param>
|
|
|
|
public void Configure(
|
|
|
|
IApplicationBuilder app,
|
2020-09-03 02:32:22 -07:00
|
|
|
IWebHostEnvironment env)
|
2019-11-24 07:27:58 -07:00
|
|
|
{
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
}
|
|
|
|
|
2020-04-23 20:24:40 -07:00
|
|
|
app.UseMiddleware<ExceptionMiddleware>();
|
2020-04-21 06:36:22 -07:00
|
|
|
|
2020-07-14 04:26:47 -07:00
|
|
|
app.UseMiddleware<ResponseTimeMiddleware>();
|
|
|
|
|
2019-11-24 07:27:58 -07:00
|
|
|
app.UseWebSockets();
|
|
|
|
|
|
|
|
app.UseResponseCompression();
|
|
|
|
|
2020-09-03 05:05:16 -07:00
|
|
|
app.UseCors(ServerCorsPolicy.DefaultPolicyName);
|
|
|
|
|
2020-09-02 15:31:42 -07:00
|
|
|
if (_serverConfigurationManager.Configuration.RequireHttps
|
|
|
|
&& _serverApplicationHost.ListenWithHttps)
|
|
|
|
{
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
}
|
2019-11-24 07:27:58 -07:00
|
|
|
|
2020-06-01 10:03:08 -07:00
|
|
|
app.UseAuthentication();
|
2020-04-21 15:15:31 -07:00
|
|
|
app.UseJellyfinApiSwagger(_serverConfigurationManager);
|
2019-11-24 07:27:58 -07:00
|
|
|
app.UseRouting();
|
|
|
|
app.UseAuthorization();
|
2020-04-26 08:28:17 -07:00
|
|
|
if (_serverConfigurationManager.Configuration.EnableMetrics)
|
|
|
|
{
|
2020-04-27 05:42:46 -07:00
|
|
|
// Must be registered after any middleware that could chagne HTTP response codes or the data will be bad
|
|
|
|
app.UseHttpMetrics();
|
2020-04-26 08:28:17 -07:00
|
|
|
}
|
|
|
|
|
2020-09-03 02:32:22 -07:00
|
|
|
app.UseLanFiltering();
|
|
|
|
app.UseIpBasedAccessValidation();
|
|
|
|
app.UseBaseUrlRedirection();
|
|
|
|
app.UseWebSocketHandler();
|
2020-09-03 02:54:38 -07:00
|
|
|
app.UseServerStartupMessage();
|
2020-09-02 15:31:42 -07:00
|
|
|
|
2019-11-24 07:27:58 -07:00
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
{
|
|
|
|
endpoints.MapControllers();
|
2020-04-26 08:28:17 -07:00
|
|
|
if (_serverConfigurationManager.Configuration.EnableMetrics)
|
|
|
|
{
|
2020-04-26 08:52:01 -07:00
|
|
|
endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
|
2020-04-26 08:28:17 -07:00
|
|
|
}
|
2019-11-24 07:27:58 -07:00
|
|
|
});
|
|
|
|
|
2020-08-10 10:58:06 -07:00
|
|
|
// Add type descriptor for legacy datetime parsing.
|
|
|
|
TypeDescriptor.AddAttributes(typeof(DateTime?), new TypeConverterAttribute(typeof(DateTimeTypeConverter)));
|
2019-11-24 07:27:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|