Files
Jackett-Jackett/src/Jackett.Server/Helper.cs
junglebus 64abc61893 Update to .NET Core 3.0 (#6151)
* Update to .NET Core 3.0

Updated Jackett so that it runs on .NET Core 3.0 now

.NET Core 3.0 brings the following benefits https://devblogs.microsoft.com/dotnet/announcing-net-core-3-0/
One of the benefits is the ability to create single file executables. I haven't enabled this yet, but its only a one line change to turn it on (would likely also require some changes to the updater).

This means that builds for LinuxAMDx64, LinuxARM32, LinuxARM64 and macOS will now run on .NET Core 3.0 instead of 2.2. Windows and Mono remain on full framework. Once .NET Core 3.1 is released (November) I'll look to moving Windows over to .NET Core as well

Tested on
-Windows 10 x64
-Debian running Jackett with Mono
-Debian running Jackett standalone (.NET Core)
2019-10-15 06:51:33 +13:00

197 lines
6.0 KiB
C#

using Autofac;
using AutoMapper;
using Jackett.Common.Models;
using Jackett.Common.Models.Config;
using Jackett.Common.Services.Interfaces;
using Jackett.Common.Utils.Clients;
using Microsoft.AspNetCore.Hosting;
using NLog;
using System.Linq;
using System.Text;
#if !NET461
using Microsoft.Extensions.Hosting;
#endif
namespace Jackett.Server
{
public static class Helper
{
public static IContainer ApplicationContainer { get; set; }
private static bool _automapperInitialised = false;
#if NET461
public static IApplicationLifetime applicationLifetime;
#else
public static IHostApplicationLifetime applicationLifetime;
#endif
public static void Initialize()
{
if (_automapperInitialised == false)
{
//Automapper only likes being initialized once per app domain.
//Since we can restart Jackett from the command line it's possible that we'll build the container more than once. (tests do this too)
InitAutomapper();
_automapperInitialised = true;
}
//Load the indexers
ServerService.Initalize();
//Kicks off the update checker
ServerService.Start();
Logger.Debug("Helper initialization complete");
}
public static void RestartWebHost()
{
Logger.Info("Restart of the web application host (not process) initiated");
Program.isWebHostRestart = true;
applicationLifetime.StopApplication();
}
public static void StopWebHost()
{
Logger.Info("Jackett is being stopped");
applicationLifetime.StopApplication();
}
public static IConfigurationService ConfigService
{
get
{
return ApplicationContainer.Resolve<IConfigurationService>();
}
}
public static IServerService ServerService
{
get
{
return ApplicationContainer.Resolve<IServerService>();
}
}
public static IServiceConfigService ServiceConfigService
{
get
{
return ApplicationContainer.Resolve<IServiceConfigService>();
}
}
public static ServerConfig ServerConfiguration
{
get
{
return ApplicationContainer.Resolve<ServerConfig>();
}
}
public static Logger Logger
{
get
{
return ApplicationContainer.Resolve<Logger>();
}
}
private static void InitAutomapper()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<WebClientByteResult, WebClientStringResult>().ForMember(x => x.Content, opt => opt.Ignore()).AfterMap((be, str) =>
{
var encoding = be.Request.Encoding ?? Encoding.UTF8;
str.Content = encoding.GetString(be.Content);
});
cfg.CreateMap<WebClientStringResult, WebClientByteResult>().ForMember(x => x.Content, opt => opt.Ignore()).AfterMap((str, be) =>
{
if (!string.IsNullOrEmpty(str.Content))
{
var encoding = str.Request.Encoding ?? Encoding.UTF8;
be.Content = encoding.GetBytes(str.Content);
}
});
cfg.CreateMap<WebClientStringResult, WebClientStringResult>();
cfg.CreateMap<WebClientByteResult, WebClientByteResult>();
cfg.CreateMap<ReleaseInfo, ReleaseInfo>();
cfg.CreateMap<ReleaseInfo, TrackerCacheResult>().AfterMap((r, t) =>
{
if (r.Category != null)
{
t.CategoryDesc = string.Join(", ", r.Category.Select(x => TorznabCatType.GetCatDesc(x)).Where(x => !string.IsNullOrEmpty(x)));
}
else
{
t.CategoryDesc = "";
}
});
});
}
public static void SetupLogging(ContainerBuilder builder)
{
Logger logger = LogManager.GetCurrentClassLogger();
if (builder != null)
{
builder.RegisterInstance(logger).SingleInstance();
}
}
public static void SetLogLevel(LogLevel level)
{
foreach (var rule in LogManager.Configuration.LoggingRules)
{
if (rule.LoggerNamePattern == "Microsoft.*")
{
if (!rule.Levels.Contains(LogLevel.Debug))
{
//don't change the first microsoftRule
continue;
}
var targets = LogManager.Configuration.ConfiguredNamedTargets;
if (level == LogLevel.Debug)
{
foreach (var target in targets)
{
rule.Targets.Add(target);
}
}
else
{
foreach (var target in targets)
{
rule.Targets.Remove(target);
}
}
continue;
}
if (level == LogLevel.Debug)
{
if (!rule.Levels.Contains(LogLevel.Debug))
{
rule.EnableLoggingForLevel(LogLevel.Debug);
}
}
else
{
if (rule.Levels.Contains(LogLevel.Debug))
{
rule.DisableLoggingForLevel(LogLevel.Debug);
}
}
}
LogManager.ReconfigExistingLoggers();
}
}
}