From 23f55ef33ad7422dca78041a44f022065625c30a Mon Sep 17 00:00:00 2001 From: flightlevel Date: Sun, 3 Jun 2018 21:11:18 +1000 Subject: [PATCH] Continue moving configuration across --- .../ServerConfigurationController.cs | 2 +- .../{Initialisation.cs => Helper.cs} | 99 +++++++++++++++---- .../Middleware/RedirectRules.cs | 2 +- src/Jackett.Server/Middleware/RewriteRules.cs | 2 +- src/Jackett.Server/Program.cs | 50 ++++++++-- src/Jackett.Server/Startup.cs | 17 +++- 6 files changed, 136 insertions(+), 36 deletions(-) rename src/Jackett.Server/{Initialisation.cs => Helper.cs} (72%) diff --git a/src/Jackett.Server/Controllers/ServerConfigurationController.cs b/src/Jackett.Server/Controllers/ServerConfigurationController.cs index c37c84f6b..94b62993c 100644 --- a/src/Jackett.Server/Controllers/ServerConfigurationController.cs +++ b/src/Jackett.Server/Controllers/ServerConfigurationController.cs @@ -95,7 +95,7 @@ namespace Jackett.Server.Controllers serverConfig.RuntimeSettings.BasePath = serverService.BasePath(); configService.SaveConfig(serverConfig); - Initialisation.SetLogLevel(logging ? LogLevel.Debug : LogLevel.Info); + Helper.SetLogLevel(logging ? LogLevel.Debug : LogLevel.Info); serverConfig.RuntimeSettings.TracingEnabled = logging; if (omdbApiKey != serverConfig.OmdbApiKey) diff --git a/src/Jackett.Server/Initialisation.cs b/src/Jackett.Server/Helper.cs similarity index 72% rename from src/Jackett.Server/Initialisation.cs rename to src/Jackett.Server/Helper.cs index 9c1aa4e9f..4485a3b81 100644 --- a/src/Jackett.Server/Initialisation.cs +++ b/src/Jackett.Server/Helper.cs @@ -9,13 +9,14 @@ using Jackett.Common.Utils.Clients; using NLog; using NLog.Config; using NLog.Targets; +using System; using System.IO; using System.Linq; using System.Text; namespace Jackett.Server { - public class Initialisation + public static class Helper { public static IContainer ApplicationContainer { get; set; } @@ -31,10 +32,87 @@ namespace Jackett.Server _automapperInitialised = true; } + ProcessRuntimeSettings(); + //Load the indexers ServerService.Initalize(); } + private static void ProcessRuntimeSettings() + { + RuntimeSettings runtimeSettings = ServerConfiguration.RuntimeSettings; + + if (runtimeSettings.ClientOverride != "httpclient" && runtimeSettings.ClientOverride != "httpclient2") + { + Logger.Error($"Client override ({runtimeSettings.ClientOverride}) has been deprecated, please remove it from your start arguments"); + Environment.Exit(1); + } + + if (runtimeSettings.DoSSLFix != null) + { + Logger.Error("SSLFix has been deprecated, please remove it from your start arguments"); + Environment.Exit(1); + } + + if (runtimeSettings.LogRequests) + { + Logger.Info("Logging enabled."); + } + + if (runtimeSettings.TracingEnabled) + { + Logger.Info("Tracing enabled."); + } + + if (runtimeSettings.IgnoreSslErrors == true) + { + Logger.Info("Jackett will ignore SSL certificate errors."); + } + + if (!string.IsNullOrWhiteSpace(runtimeSettings.CustomDataFolder)) + { + Logger.Info("Jackett Data will be stored in: " + runtimeSettings.CustomDataFolder); + } + + // Use Proxy + if (runtimeSettings.ProxyConnection != null) + { + Logger.Info("Proxy enabled. " + runtimeSettings.ProxyConnection); + } + } + + public static IConfigurationService ConfigService + { + get + { + return ApplicationContainer.Resolve(); + } + } + + public static IServerService ServerService + { + get + { + return ApplicationContainer.Resolve(); + } + } + + public static ServerConfig ServerConfiguration + { + get + { + return ApplicationContainer.Resolve(); + } + } + + public static Logger Logger + { + get + { + return ApplicationContainer.Resolve(); + } + } + private static void InitAutomapper() { Mapper.Initialize(cfg => @@ -62,8 +140,7 @@ namespace Jackett.Server { if (r.Category != null) { - var CategoryDesc = string.Join(", ", r.Category.Select(x => TorznabCatType.GetCatDesc(x)).Where(x => !string.IsNullOrEmpty(x))); - t.CategoryDesc = CategoryDesc; + t.CategoryDesc = string.Join(", ", r.Category.Select(x => TorznabCatType.GetCatDesc(x)).Where(x => !string.IsNullOrEmpty(x))); } else { @@ -73,22 +150,6 @@ namespace Jackett.Server }); } - public static IConfigurationService ConfigService - { - get - { - return ApplicationContainer.Resolve(); - } - } - - public static IServerService ServerService - { - get - { - return ApplicationContainer.Resolve(); - } - } - public static void SetupLogging(RuntimeSettings settings, ContainerBuilder builder) { var logFileName = settings.CustomLogFileName ?? "log.txt"; diff --git a/src/Jackett.Server/Middleware/RedirectRules.cs b/src/Jackett.Server/Middleware/RedirectRules.cs index 547c944cc..f93a7a050 100644 --- a/src/Jackett.Server/Middleware/RedirectRules.cs +++ b/src/Jackett.Server/Middleware/RedirectRules.cs @@ -15,7 +15,7 @@ namespace Jackett.Server.Middleware || request.Path.ToString().Equals("/index.html", StringComparison.OrdinalIgnoreCase)) { // 301 is the status code of permanent redirect - var redir = Initialisation.ServerService.BasePath() + "/UI/Dashboard"; + var redir = Helper.ServerService.BasePath() + "/UI/Dashboard"; var response = context.HttpContext.Response; response.StatusCode = StatusCodes.Status301MovedPermanently; context.Result = RuleResult.EndResponse; diff --git a/src/Jackett.Server/Middleware/RewriteRules.cs b/src/Jackett.Server/Middleware/RewriteRules.cs index 8d87f7797..3361b0d2b 100644 --- a/src/Jackett.Server/Middleware/RewriteRules.cs +++ b/src/Jackett.Server/Middleware/RewriteRules.cs @@ -10,7 +10,7 @@ namespace Jackett.Server.Middleware { var request = context.HttpContext.Request; - string serverBasePath = Initialisation.ServerService.BasePath() ?? string.Empty; + string serverBasePath = Helper.ServerService.BasePath() ?? string.Empty; if (request.Path != null && request.Path.HasValue && serverBasePath.Length > 0 && request.Path.Value.StartsWith(serverBasePath, StringComparison.Ordinal)) { diff --git a/src/Jackett.Server/Program.cs b/src/Jackett.Server/Program.cs index b5d0c7091..3dabf3f6b 100644 --- a/src/Jackett.Server/Program.cs +++ b/src/Jackett.Server/Program.cs @@ -18,30 +18,42 @@ using System.Runtime.InteropServices; namespace Jackett.Server { - public class Program + public static class Program { public static IConfiguration Configuration { get; set; } + private static RuntimeSettings Settings { get; set; } + public static void Main(string[] args) { - var optionsResult = Parser.Default.ParseArguments(args); + AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; + + var parser = new Parser(); + var optionsResult = parser.ParseArguments(args); + optionsResult.WithNotParsed(errors => { var text = HelpText.AutoBuild(optionsResult); text.Copyright = " "; - text.Heading = "Jackett v" + EnvironmentUtil.JackettVersion + " options:"; + text.Heading = "Jackett v" + EnvironmentUtil.JackettVersion; + Console.WriteLine(text); Environment.Exit(1); return; }); var runtimeDictionary = new Dictionary(); - RuntimeSettings runtimeSettings = new RuntimeSettings(); ConsoleOptions consoleOptions = new ConsoleOptions(); optionsResult.WithParsed(options => { - runtimeSettings = options.ToRunTimeSettings(); + if (string.IsNullOrEmpty(options.Client)) + { + //TODO: Remove libcurl once off owin + options.Client = "httpclient"; + } + + Settings = options.ToRunTimeSettings(); consoleOptions = options; - runtimeDictionary = GetValues(runtimeSettings); + runtimeDictionary = GetValues(Settings); }); var builder = new ConfigurationBuilder(); @@ -51,8 +63,8 @@ namespace Jackett.Server //hack TODO: Get the configuration without any DI var containerBuilder = new ContainerBuilder(); - Initialisation.SetupLogging(runtimeSettings, containerBuilder); - containerBuilder.RegisterModule(new JackettModule(runtimeSettings)); + Helper.SetupLogging(Settings, containerBuilder); + containerBuilder.RegisterModule(new JackettModule(Settings)); containerBuilder.RegisterType().As(); containerBuilder.RegisterType().As(); containerBuilder.RegisterType().As(); @@ -64,7 +76,7 @@ namespace Jackett.Server IServerService serverService = tempContainer.Resolve(); Int32.TryParse(serverConfig.Port.ToString(), out Int32 configPort); - DirectoryInfo dataProtectionFolder = new DirectoryInfo(Path.Combine(runtimeSettings.DataFolder, "DataProtection")); + DirectoryInfo dataProtectionFolder = new DirectoryInfo(Path.Combine(Settings.DataFolder, "DataProtection")); if (!dataProtectionFolder.Exists) { dataProtectionFolder.Create(); @@ -110,6 +122,26 @@ namespace Jackett.Server .ToDictionary(p => "RuntimeSettings:" + p.Name, p => p.GetValue(obj) == null ? null : p.GetValue(obj).ToString()); } + private static void CurrentDomain_ProcessExit(object sender, EventArgs e) + { + try + { + if (Settings != null && !string.IsNullOrWhiteSpace(Settings.PIDFile)) + { + var PIDFile = Settings.PIDFile; + if (File.Exists(PIDFile)) + { + Console.WriteLine("Deleting PID file " + PIDFile); + File.Delete(PIDFile); + } + } + } + catch (Exception ex) + { + Console.Error.WriteLine(ex.ToString(), "Error while deleting the PID file"); + } + } + public static IWebHostBuilder CreateWebHostBuilder(string[] args, string[] urls) => WebHost.CreateDefaultBuilder(args) .UseConfiguration(Configuration) diff --git a/src/Jackett.Server/Startup.cs b/src/Jackett.Server/Startup.cs index e7a6120c7..cc9c67bf8 100644 --- a/src/Jackett.Server/Startup.cs +++ b/src/Jackett.Server/Startup.cs @@ -74,7 +74,7 @@ namespace Jackett.Server var builder = new ContainerBuilder(); - Initialisation.SetupLogging(runtimeSettings, builder); + Helper.SetupLogging(runtimeSettings, builder); builder.Populate(services); builder.RegisterModule(new JackettModule(runtimeSettings)); @@ -83,15 +83,17 @@ namespace Jackett.Server builder.RegisterType().As(); IContainer container = builder.Build(); - Initialisation.ApplicationContainer = container; + Helper.ApplicationContainer = container; + + Helper.Initialize(); return new AutofacServiceProvider(container); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime applicationLifetime) { - Initialisation.Initialize(); + applicationLifetime.ApplicationStopping.Register(OnShutdown); app.UseResponseCompression(); @@ -107,7 +109,7 @@ namespace Jackett.Server app.UseFileServer(new FileServerOptions { - FileProvider = new PhysicalFileProvider(Initialisation.ConfigService.GetContentFolder()), + FileProvider = new PhysicalFileProvider(Helper.ConfigService.GetContentFolder()), RequestPath = "", EnableDefaultFiles = true, EnableDirectoryBrowsing = false @@ -117,5 +119,10 @@ namespace Jackett.Server app.UseMvc(); } + + private void OnShutdown() + { + //this code is called when the application stops + } } }