Jackett/src/Jackett.Server/Program.cs

185 lines
7.3 KiB
C#
Raw Normal View History

2020-02-09 18:08:34 +00:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using CommandLine;
2018-05-01 12:00:02 +00:00
using CommandLine.Text;
using Jackett.Common.Models.Config;
using Jackett.Common.Services;
2018-05-20 11:21:08 +00:00
using Jackett.Common.Services.Interfaces;
2018-05-01 12:00:02 +00:00
using Jackett.Common.Utils;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
2018-05-20 11:21:08 +00:00
using NLog;
2018-06-18 12:01:25 +00:00
using NLog.Web;
namespace Jackett.Server
{
2018-06-03 11:11:18 +00:00
public static class Program
{
2018-05-01 12:00:02 +00:00
public static IConfiguration Configuration { get; set; }
2018-06-03 11:11:18 +00:00
private static RuntimeSettings Settings { get; set; }
2018-06-10 02:33:16 +00:00
public static bool isWebHostRestart = false;
2018-06-03 11:11:18 +00:00
public static void Main(string[] args)
{
2018-06-03 11:11:18 +00:00
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
2018-06-17 04:12:52 +00:00
var commandLineParser = new Parser(settings => settings.CaseSensitive = false);
2018-06-10 02:33:16 +00:00
var optionsResult = commandLineParser.ParseArguments<ConsoleOptions>(args);
var runtimeDictionary = new Dictionary<string, string>();
var consoleOptions = new ConsoleOptions();
2018-06-03 11:11:18 +00:00
2018-05-01 12:00:02 +00:00
optionsResult.WithNotParsed(errors =>
{
var text = HelpText.AutoBuild(optionsResult);
text.Copyright = " ";
text.Heading = "Jackett " + EnvironmentUtil.JackettVersion();
2018-06-03 11:11:18 +00:00
Console.WriteLine(text);
2018-05-20 11:21:08 +00:00
Environment.Exit(1);
2018-05-01 12:00:02 +00:00
});
optionsResult.WithParsed(options =>
{
2018-06-03 11:11:18 +00:00
if (string.IsNullOrEmpty(options.Client))
options.Client = DotNetCoreUtil.IsRunningOnDotNetCore ? "httpclient2" : "httpclient";
2018-06-03 11:11:18 +00:00
Settings = options.ToRunTimeSettings();
2018-05-20 11:21:08 +00:00
consoleOptions = options;
2018-06-10 02:33:16 +00:00
runtimeDictionary = GetValues(Settings);
2018-05-01 12:00:02 +00:00
});
2018-06-22 12:35:58 +00:00
LogManager.Configuration = LoggingSetup.GetLoggingConfiguration(Settings);
var logger = LogManager.GetCurrentClassLogger();
logger.Info("Starting Jackett " + EnvironmentUtil.JackettVersion());
2018-06-16 08:32:08 +00:00
// create PID file early
if (!string.IsNullOrWhiteSpace(Settings.PIDFile))
{
try
{
var proc = Process.GetCurrentProcess();
File.WriteAllText(Settings.PIDFile, proc.Id.ToString());
}
catch (Exception e)
{
logger.Error($"Error while creating the PID file\n{e}");
}
}
2018-09-15 05:20:31 +00:00
Initialisation.CheckEnvironmentalVariables(logger);
Initialisation.ProcessSettings(Settings, logger);
ISerializeService serializeService = new SerializeService();
IProcessService processService = new ProcessService(logger);
IConfigurationService configurationService = new ConfigurationService(serializeService, processService, logger, Settings);
if (consoleOptions.Install || consoleOptions.Uninstall || consoleOptions.StartService || consoleOptions.StopService || consoleOptions.ReserveUrls)
{
var isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
if (isWindows)
{
var serverConfig = configurationService.BuildServerConfig(Settings);
Initialisation.ProcessWindowsSpecificArgs(consoleOptions, processService, serverConfig, logger);
}
else
{
logger.Error("ReserveUrls and service arguments only apply to Windows, please remove them from your start arguments");
Environment.Exit(1);
}
}
2018-05-01 12:00:02 +00:00
var builder = new ConfigurationBuilder();
builder.AddInMemoryCollection(runtimeDictionary);
builder.AddJsonFile(Path.Combine(configurationService.GetAppDataFolder(), "appsettings.json"), optional: true);
2018-05-01 12:00:02 +00:00
Configuration = builder.Build();
do
2018-05-20 11:21:08 +00:00
{
if (!isWebHostRestart)
2018-05-20 11:21:08 +00:00
{
2018-06-17 04:39:49 +00:00
if (consoleOptions.Port != 0 || consoleOptions.ListenPublic || consoleOptions.ListenPrivate)
2018-05-20 11:21:08 +00:00
{
var serverConfiguration = configurationService.BuildServerConfig(Settings);
2018-06-17 04:39:49 +00:00
Initialisation.ProcessConsoleOverrides(consoleOptions, processService, serverConfiguration, configurationService, logger);
2018-05-20 11:21:08 +00:00
}
}
var serverConfig = configurationService.BuildServerConfig(Settings);
int.TryParse(serverConfig.Port.ToString(), out var configPort);
var url = serverConfig.GetListenAddresses(serverConfig.AllowExternal);
2018-05-20 11:21:08 +00:00
2018-06-10 02:33:16 +00:00
isWebHostRestart = false;
try
{
2018-07-10 12:22:02 +00:00
logger.Debug("Creating web host...");
var applicationFolder = Path.Combine(configurationService.ApplicationFolder(), "Content");
logger.Debug($"Content root path is: {applicationFolder}");
CreateWebHostBuilder(args, url, applicationFolder).Build().Run();
}
catch (Exception e)
{
if (e.InnerException is Microsoft.AspNetCore.Connections.AddressInUseException)
{
logger.Error($"Address already in use: Most likely Jackett is already running. {e.Message}");
Environment.Exit(1);
}
logger.Error(e);
throw;
}
2018-06-10 02:33:16 +00:00
} while (isWebHostRestart);
2018-05-01 12:00:02 +00:00
}
public static Dictionary<string, string> GetValues(object obj)
{
return obj
.GetType()
.GetProperties()
.ToDictionary(p => "RuntimeSettings:" + p.Name, p => p.GetValue(obj) == null ? null : p.GetValue(obj).ToString());
}
2018-06-03 11:11:18 +00:00
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))
2018-06-03 11:11:18 +00:00
{
Console.WriteLine("Deleting PID file " + pidFile);
File.Delete(pidFile);
2018-06-03 11:11:18 +00:00
}
LogManager.Shutdown();
2018-06-03 11:11:18 +00:00
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString(), "Error while deleting the PID file");
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args, string[] urls, string contentRoot) =>
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(contentRoot)
.UseWebRoot(contentRoot)
2018-06-10 02:33:16 +00:00
.UseUrls(urls)
.PreferHostingUrls(true)
.UseConfiguration(Configuration)
2018-06-18 12:01:25 +00:00
.UseStartup<Startup>()
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
2018-06-18 12:01:25 +00:00
.UseNLog();
}
}