Jackett/src/Jackett.Server/Startup.cs

135 lines
5.3 KiB
C#
Raw Normal View History

2018-05-01 12:00:02 +00:00
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Jackett.Common.Models.Config;
using Jackett.Common.Plumbing;
using Jackett.Common.Services.Interfaces;
using Jackett.Common.Utils.Clients;
using Jackett.Server.Middleware;
2018-05-01 12:00:02 +00:00
using Jackett.Server.Services;
2018-05-12 02:44:47 +00:00
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
2018-05-12 02:44:47 +00:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2018-05-12 02:44:47 +00:00
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2018-05-01 12:00:02 +00:00
using Microsoft.Extensions.FileProviders;
using Newtonsoft.Json.Serialization;
using System;
using System.IO;
2018-05-01 12:00:02 +00:00
using System.Text;
namespace Jackett.Server
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
2018-05-01 12:00:02 +00:00
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression();
2018-05-12 02:44:47 +00:00
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
options =>
{
options.LoginPath = new PathString("/UI/Login");
options.AccessDeniedPath = new PathString("/UI/Login");
options.LogoutPath = new PathString("/UI/Logout");
options.Cookie.Name = "Jackett";
});
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
})
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver(); //Web app uses Pascal Case JSON
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
2018-05-01 12:00:02 +00:00
RuntimeSettings runtimeSettings = new RuntimeSettings();
Configuration.GetSection("RuntimeSettings").Bind(runtimeSettings);
DirectoryInfo dataProtectionFolder = new DirectoryInfo(Path.Combine(runtimeSettings.DataFolder, "DataProtection"));
services.AddDataProtection()
.PersistKeysToFileSystem(dataProtectionFolder)
.SetApplicationName("Jackett");
2018-05-01 12:00:02 +00:00
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var builder = new ContainerBuilder();
Helper.SetupLogging(builder);
2018-05-01 12:00:02 +00:00
builder.Populate(services);
builder.RegisterModule(new JackettModule(runtimeSettings));
builder.RegisterType<SecuityService>().As<ISecuityService>();
builder.RegisterType<ServerService>().As<IServerService>();
builder.RegisterType<ProtectionService>().As<IProtectionService>();
2018-06-10 02:33:16 +00:00
builder.RegisterType<ServiceConfigService>().As<IServiceConfigService>();
if (runtimeSettings.ClientOverride == "httpclientnetcore")
builder.RegisterType<HttpWebClientNetCore>().As<WebClient>();
2018-05-01 12:00:02 +00:00
IContainer container = builder.Build();
2018-06-03 11:11:18 +00:00
Helper.ApplicationContainer = container;
Helper.Initialize();
2018-05-01 12:00:02 +00:00
return new AutofacServiceProvider(container);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2018-06-03 11:11:18 +00:00
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime applicationLifetime)
{
2018-06-03 11:11:18 +00:00
applicationLifetime.ApplicationStopping.Register(OnShutdown);
2018-06-10 02:33:16 +00:00
Helper.applicationLifetime = applicationLifetime;
app.UseResponseCompression();
2018-05-01 12:00:02 +00:00
app.UseDeveloperExceptionPage();
app.UseCustomExceptionHandler();
var rewriteOptions = new RewriteOptions()
.Add(RewriteRules.RewriteBasePath)
2018-06-18 11:48:45 +00:00
.AddRewrite(@"^torznab\/([\w-]*)", "api/v2.0/indexers/$1/results/torznab", skipRemainingRules: true) //legacy torznab route
.AddRewrite(@"^potato\/([\w-]*)", "api/v2.0/indexers/$1/results/potato", skipRemainingRules: true) //legacy potato route
.Add(RedirectRules.RedirectToDashboard);
app.UseRewriter(rewriteOptions);
2018-05-01 12:00:02 +00:00
app.UseFileServer(new FileServerOptions
{
2018-06-03 11:11:18 +00:00
FileProvider = new PhysicalFileProvider(Helper.ConfigService.GetContentFolder()),
2018-05-01 12:00:02 +00:00
RequestPath = "",
EnableDefaultFiles = true,
EnableDirectoryBrowsing = false
});
2018-05-12 02:44:47 +00:00
app.UseAuthentication();
app.UseMvc();
}
2018-06-03 11:11:18 +00:00
private void OnShutdown()
{
//this code is called when the application stops
}
}
}