diff --git a/NzbDrone.Api/Bootstrapper.cs b/NzbDrone.Api/Bootstrapper.cs index 81d509be7..818341466 100644 --- a/NzbDrone.Api/Bootstrapper.cs +++ b/NzbDrone.Api/Bootstrapper.cs @@ -1,27 +1,37 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Linq; using AutoMapper; +using Autofac; +using NLog; +using Nancy.Bootstrapper; +using Nancy.Bootstrappers.Autofac; +using NzbDrone.Api.ErrorManagment; using NzbDrone.Api.QualityProfiles; using NzbDrone.Api.QualityType; using NzbDrone.Api.Resolvers; +using NzbDrone.Core; using NzbDrone.Core.Repository.Quality; namespace NzbDrone.Api { - public static class Bootstrapper + + public class Bootstrapper : AutofacNancyBootstrapper { + private readonly Logger _logger; + + + public static void Initialize() { //QualityProfiles Mapper.CreateMap<QualityProfileModel, QualityProfile>() .ForMember(dest => dest.QualityProfileId, opt => opt.MapFrom(src => src.Id)) - .ForMember(dest => dest.Allowed, opt => opt.ResolveUsing<QualitiesToAllowedResolver>().FromMember(src => src.Qualities)); + .ForMember(dest => dest.Allowed, + opt => opt.ResolveUsing<QualitiesToAllowedResolver>().FromMember(src => src.Qualities)); Mapper.CreateMap<QualityProfile, QualityProfileModel>() .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityProfileId)) - .ForMember(dest => dest.Qualities, opt => opt.ResolveUsing<AllowedToQualitiesResolver>().FromMember(src => src.Allowed)); + .ForMember(dest => dest.Qualities, + opt => opt.ResolveUsing<AllowedToQualitiesResolver>().FromMember(src => src.Allowed)); Mapper.CreateMap<QualityTypes, QualityProfileType>() .ForMember(dest => dest.Allowed, opt => opt.Ignore()); @@ -33,5 +43,42 @@ namespace NzbDrone.Api Mapper.CreateMap<Core.Repository.Quality.QualityType, QualityTypeModel>() .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityTypeId)); } + + protected override ILifetimeScope GetApplicationContainer() + { + _logger.Info("Starting NzbDrone API"); + + + var builder = new ContainerBuilder(); + + builder.RegisterCoreServices(); + + builder.RegisterAssemblyTypes(typeof(Bootstrapper).Assembly) + .AsImplementedInterfaces() + .SingleInstance(); + + builder.RegisterType<ErrorPipeline>().AsSelf().SingleInstance(); + + + var container = builder.Build(); + + ApplicationPipelines.OnError.AddItemToEndOfPipeline(container.Resolve<ErrorPipeline>().HandleException); + + + return container; + } + + protected override NancyInternalConfiguration InternalConfiguration + { + get + { + var internalConfig = NancyInternalConfiguration.Default; + + internalConfig.StatusCodeHandlers.Add(typeof(ErrorHandler)); + + + return internalConfig; + } + } } -} +} \ No newline at end of file diff --git a/NzbDrone.Api/ErrorManagment/ApiException.cs b/NzbDrone.Api/ErrorManagment/ApiException.cs new file mode 100644 index 000000000..f9d1e1265 --- /dev/null +++ b/NzbDrone.Api/ErrorManagment/ApiException.cs @@ -0,0 +1,41 @@ +using System; +using System.Linq; +using Nancy; +using Nancy.Responses; +using Newtonsoft.Json; +using NzbDrone.Api.QualityType; + +namespace NzbDrone.Api.ErrorManagment +{ + public abstract class ApiException : Exception + { + public object Content { get; private set; } + + + public HttpStatusCode StatusCode { get; private set; } + + protected ApiException(HttpStatusCode statusCode, object content = null) + : base(GetMessage(statusCode, content)) + { + StatusCode = statusCode; + Content = content; + } + + public JsonResponse<ErrorModel> ToErrorResponse() + { + return new ErrorModel(this).AsResponse(StatusCode); + } + + private static string GetMessage(HttpStatusCode statusCode, object content) + { + var result = statusCode.ToString(); + + if (content != null) + { + result = result + " :" + JsonConvert.SerializeObject(content); + } + + return result; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Api/ErrorManagment/ErrorHandler.cs b/NzbDrone.Api/ErrorManagment/ErrorHandler.cs new file mode 100644 index 000000000..9dd643e46 --- /dev/null +++ b/NzbDrone.Api/ErrorManagment/ErrorHandler.cs @@ -0,0 +1,33 @@ +using System.Linq; +using Nancy; +using Nancy.ErrorHandling; +using NzbDrone.Api.QualityType; + +namespace NzbDrone.Api.ErrorManagment +{ + public class ErrorHandler : IStatusCodeHandler + { + public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context) + { + return true; + } + + public void Handle(HttpStatusCode statusCode, NancyContext context) + { + if (statusCode == HttpStatusCode.SeeOther || statusCode == HttpStatusCode.OK) + return; + + if (statusCode == HttpStatusCode.Continue) + { + context.Response = new Response { StatusCode = statusCode }; + return; + } + + if (context.Response.ContentType == "text/html" || context.Response.ContentType == "text/plain") + context.Response = new ErrorModel + { + Message = statusCode.ToString() + }.AsResponse(statusCode); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Api/ErrorManagment/ErrorModel.cs b/NzbDrone.Api/ErrorManagment/ErrorModel.cs new file mode 100644 index 000000000..81f83a0bf --- /dev/null +++ b/NzbDrone.Api/ErrorManagment/ErrorModel.cs @@ -0,0 +1,21 @@ +using System.Linq; + +namespace NzbDrone.Api.ErrorManagment +{ + public class ErrorModel + { + public string Message { get; set; } + public string Description { get; set; } + public object Content { get; set; } + + public ErrorModel(ApiException exception) + { + Message = exception.Message; + Content = exception.Content; + } + + public ErrorModel() + { + } + } +} \ No newline at end of file diff --git a/NzbDrone.Api/ErrorManagment/ErrorPipeline.cs b/NzbDrone.Api/ErrorManagment/ErrorPipeline.cs new file mode 100644 index 000000000..528ce5792 --- /dev/null +++ b/NzbDrone.Api/ErrorManagment/ErrorPipeline.cs @@ -0,0 +1,28 @@ +using System; +using System.Linq; +using NLog; +using Nancy; + +namespace NzbDrone.Api.ErrorManagment +{ + public class ErrorPipeline + { + private readonly Logger _logger; + + public ErrorPipeline(Logger logger) + { + _logger = logger; + } + + public Response HandleException(NancyContext context, Exception exception) + { + if (exception is ApiException) + { + _logger.WarnException("API Error", exception); + return ((ApiException)exception).ToErrorResponse(); + } + _logger.ErrorException("Unexpected error", exception); + return null; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index c2a22bebd..b4ea6f09a 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -72,6 +72,10 @@ <SpecificVersion>False</SpecificVersion> <HintPath>..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll</HintPath> </Reference> + <Reference Include="NLog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL"> + <SpecificVersion>False</SpecificVersion> + <HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> @@ -81,7 +85,11 @@ <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> + <Compile Include="ErrorManagment\ApiException.cs" /> <Compile Include="Bootstrapper.cs" /> + <Compile Include="ErrorManagment\ErrorHandler.cs" /> + <Compile Include="ErrorManagment\ErrorModel.cs" /> + <Compile Include="ErrorManagment\ErrorPipeline.cs" /> <Compile Include="Exceptions\InvalidApiKeyException.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="QualityProfiles\QualityProfileModel.cs" /> diff --git a/NzbDrone.Api/QualityType/RequestExtensions.cs b/NzbDrone.Api/QualityType/RequestExtensions.cs index d7afc8ff0..f14c3f7d8 100644 --- a/NzbDrone.Api/QualityType/RequestExtensions.cs +++ b/NzbDrone.Api/QualityType/RequestExtensions.cs @@ -21,10 +21,10 @@ namespace NzbDrone.Api.QualityType }); } - public static Response AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK) + public static JsonResponse<TModel> AsResponse<TModel>(this TModel model, HttpStatusCode statusCode = HttpStatusCode.OK) { ISerializer serializer = new DefaultJsonSerializer(); - var jsonResponse = new JsonResponse<TModel>(model, serializer) {StatusCode = statusCode}; + var jsonResponse = new JsonResponse<TModel>(model, serializer) { StatusCode = statusCode }; return jsonResponse; } } diff --git a/NzbDrone.Api/packages.config b/NzbDrone.Api/packages.config index aee7f94d0..b3fd32088 100644 --- a/NzbDrone.Api/packages.config +++ b/NzbDrone.Api/packages.config @@ -6,4 +6,5 @@ <package id="Nancy.Bootstrappers.Autofac" version="0.15.3" targetFramework="net40" /> <package id="Nancy.Hosting.Aspnet" version="0.15.3" targetFramework="net40" /> <package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" /> + <package id="NLog" version="2.0.0.2000" targetFramework="net40" /> </packages> \ No newline at end of file diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index 87210a0b0..6627cc4e3 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -1,109 +1,30 @@ using System; -using System.Collections.Generic; using System.Diagnostics; -using System.IO; using System.Linq; using Autofac; -using Autofac.Core; using NLog; using NzbDrone.Common; using NzbDrone.Core.Instrumentation; -using NzbDrone.Core.Jobs; -using NzbDrone.Core.Providers; using NzbDrone.Core.Providers.Core; -using NzbDrone.Core.Providers.ExternalNotification; -using NzbDrone.Core.Providers.Indexer; -using NzbDrone.Core.Providers.Metadata; -using NzbDrone.Core.Repository; -using PetaPoco; using SignalR; -using Connection = NzbDrone.Core.Datastore.Connection; namespace NzbDrone.Core { public class CentralDispatch { - private static readonly Logger logger = LogManager.GetCurrentClassLogger(); + private readonly Logger _logger; private readonly EnvironmentProvider _environmentProvider; public ContainerBuilder ContainerBuilder { get; private set; } public CentralDispatch() { + _logger = LogManager.GetCurrentClassLogger(); _environmentProvider = new EnvironmentProvider(); - logger.Debug("Initializing ContainerBuilder:"); + _logger.Debug("Initializing ContainerBuilder:"); ContainerBuilder = new ContainerBuilder(); - ContainerBuilder.RegisterAssemblyTypes(typeof(DiskProvider).Assembly).SingleInstance(); - ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly).SingleInstance(); - ContainerBuilder.RegisterType<EnvironmentProvider>(); - - InitDatabase(); - RegisterExternalNotifications(); - RegisterMetadataProviders(); - RegisterIndexers(); - RegisterJobs(); - } - - private void InitDatabase() - { - logger.Info("Registering Database..."); - - var appDataPath = _environmentProvider.GetAppDataPath(); - if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath); - - ContainerBuilder.Register(c => c.Resolve<Connection>().GetMainPetaPocoDb()) - .As<IDatabase>(); - - ContainerBuilder.Register(c => c.Resolve<Connection>().GetLogPetaPocoDb(false)) - .SingleInstance() - .Named<IDatabase>("DatabaseTarget"); - - ContainerBuilder.Register(c => c.Resolve<Connection>().GetLogPetaPocoDb()) - .Named<IDatabase>("LogProvider"); - - ContainerBuilder.RegisterType<DatabaseTarget>().WithParameter(ResolvedParameter.ForNamed<IDatabase>("DatabaseTarget")); - ContainerBuilder.RegisterType<LogProvider>().WithParameter(ResolvedParameter.ForNamed<IDatabase>("LogProvider")); - } - - private void RegisterIndexers() - { - logger.Debug("Registering Indexers..."); - - ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly) - .Where(t => t.BaseType == typeof(IndexerBase)) - .As<IndexerBase>(); - } - - private void RegisterJobs() - { - logger.Debug("Registering Background Jobs..."); - - ContainerBuilder.RegisterType<JobProvider>().SingleInstance(); - - ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly) - .Where(t => t.GetInterfaces().Contains(typeof(IJob))) - .As<IJob>() - .SingleInstance(); - } - - private void RegisterExternalNotifications() - { - logger.Debug("Registering External Notifications..."); - - ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly) - .Where(t => t.BaseType == typeof(ExternalNotificationBase)) - .As<ExternalNotificationBase>(); - } - - private void RegisterMetadataProviders() - { - logger.Debug("Registering Metadata Providers..."); - - ContainerBuilder.RegisterAssemblyTypes(typeof(CentralDispatch).Assembly) - .Where(t => t.IsSubclassOf(typeof(MetadataBase))) - .As<MetadataBase>(); } private void RegisterReporting(IContainer container) @@ -113,12 +34,6 @@ namespace NzbDrone.Core ReportingService.SetupExceptronDriver(); } - private void RegisterQuality(IContainer container) - { - logger.Debug("Initializing Quality..."); - container.Resolve<QualityProvider>().SetupDefaultProfiles(); - container.Resolve<QualityTypeProvider>().SetupDefault(); - } public void DedicateToHost() { @@ -126,67 +41,49 @@ namespace NzbDrone.Core { var pid = _environmentProvider.NzbDroneProcessIdFromEnviroment; - logger.Debug("Attaching to parent process ({0}) for automatic termination.", pid); + _logger.Debug("Attaching to parent process ({0}) for automatic termination.", pid); var hostProcess = Process.GetProcessById(Convert.ToInt32(pid)); hostProcess.EnableRaisingEvents = true; hostProcess.Exited += (delegate { - logger.Info("Host has been terminated. Shutting down web server."); + _logger.Info("Host has been terminated. Shutting down web server."); ShutDown(); }); - logger.Debug("Successfully Attached to host. Process [{0}]", hostProcess.ProcessName); + _logger.Debug("Successfully Attached to host. Process [{0}]", hostProcess.ProcessName); } catch (Exception e) { - logger.FatalException("An error has occurred while dedicating to host.", e); + _logger.FatalException("An error has occurred while dedicating to host.", e); } } public IContainer BuildContainer() { - var container = ContainerBuilder.Build(); + _logger.Debug("Initializing Components"); - logger.Debug("Initializing Components"); + ContainerBuilder.RegisterCoreServices(); + + var container = ContainerBuilder.Build(); container.Resolve<DatabaseTarget>().Register(); LogConfiguration.Reload(); RegisterReporting(container); - RegisterQuality(container); - var indexers = container.Resolve<IEnumerable<IndexerBase>>(); - container.Resolve<IndexerProvider>().InitializeIndexers(indexers.ToList()); - - var newznabIndexers = new List<NewznabDefinition> - { - new NewznabDefinition { Enable = false, Name = "Nzbs.org", Url = "http://nzbs.org", BuiltIn = true }, - new NewznabDefinition { Enable = false, Name = "Nzb.su", Url = "https://nzb.su", BuiltIn = true }, - new NewznabDefinition { Enable = false, Name = "Dognzb.cr", Url = "https://dognzb.cr", BuiltIn = true } - }; - - container.Resolve<NewznabProvider>().InitializeNewznabIndexers(newznabIndexers); - - container.Resolve<JobProvider>().Initialize(); container.Resolve<WebTimer>().StartTimer(30); - var notifiers = container.Resolve<IEnumerable<ExternalNotificationBase>>(); - container.Resolve<ExternalNotificationProvider>().InitializeNotifiers(notifiers.ToList()); - - var providers = container.Resolve<IEnumerable<MetadataBase>>(); - container.Resolve<MetadataProvider>().Initialize(providers.ToList()); - //SignalR GlobalHost.DependencyResolver = new AutofacSignalrDependencyResolver(container.BeginLifetimeScope("SignalR")); return container; } - private static void ShutDown() + private void ShutDown() { - logger.Info("Shutting down application..."); + _logger.Info("Shutting down application..."); WebTimer.Stop(); Process.GetCurrentProcess().Kill(); } diff --git a/NzbDrone.Core/ContainerExtentions.cs b/NzbDrone.Core/ContainerExtentions.cs new file mode 100644 index 000000000..a3ab7b4d7 --- /dev/null +++ b/NzbDrone.Core/ContainerExtentions.cs @@ -0,0 +1,86 @@ +using System.IO; +using System.Linq; +using System.Reflection; +using Autofac; +using Autofac.Core; +using NLog; +using NzbDrone.Common; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Instrumentation; +using NzbDrone.Core.Providers.ExternalNotification; +using NzbDrone.Core.Providers.Indexer; +using NzbDrone.Core.Providers.Metadata; +using NzbDrone.Core.Providers.Search; +using PetaPoco; + +namespace NzbDrone.Core +{ + public static class ContainerExtentions + { + + private static Logger _logger = LogManager.GetLogger("ServiceRegistration"); + + public static void RegisterCoreServices(this ContainerBuilder container) + { + var core = Assembly.Load("NzbDrone.Core"); + var common = Assembly.Load("NzbDrone.Common"); + + + container.RegisterAssembly(core); + container.RegisterAssembly(common); + + container.InitDatabase(); + } + + + private static void RegisterAssembly(this ContainerBuilder container, Assembly assembly) + { + container.RegisterAssemblyTypes(assembly) + .AsSelf() + .SingleInstance(); + + container.RegisterAssemblyTypes(assembly) + .AsImplementedInterfaces() + .SingleInstance(); + + container.RegisterAssemblyTypes(assembly) + .Where(t => t.BaseType == typeof(IndexerBase)) + .As<IndexerBase>().SingleInstance(); + + container.RegisterAssemblyTypes(assembly) + .Where(t => t.BaseType == typeof(SearchBase)) + .As<SearchBase>().SingleInstance(); + + container.RegisterAssemblyTypes(assembly) + .Where(t => t.BaseType == typeof(ExternalNotificationBase)) + .As<ExternalNotificationBase>().SingleInstance(); + + container.RegisterAssemblyTypes(assembly) + .Where(t => t.BaseType == typeof(MetadataBase)) + .As<MetadataBase>().SingleInstance(); + } + + + + private static void InitDatabase(this ContainerBuilder container) + { + _logger.Info("Registering Database..."); + + var appDataPath = new EnvironmentProvider().GetAppDataPath(); + if (!Directory.Exists(appDataPath)) Directory.CreateDirectory(appDataPath); + + container.Register(c => c.Resolve<Connection>().GetMainPetaPocoDb()) + .As<IDatabase>(); + + container.Register(c => c.Resolve<Connection>().GetLogPetaPocoDb(false)) + .SingleInstance() + .Named<IDatabase>("DatabaseTarget"); + + container.Register(c => c.Resolve<Connection>().GetLogPetaPocoDb()) + .Named<IDatabase>("LogProvider"); + + container.RegisterType<DatabaseTarget>().WithParameter(ResolvedParameter.ForNamed<IDatabase>("DatabaseTarget")); + container.RegisterType<LogProvider>().WithParameter(ResolvedParameter.ForNamed<IDatabase>("LogProvider")); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Jobs/JobProvider.cs b/NzbDrone.Core/Jobs/JobProvider.cs index 0c23f9280..544a7c342 100644 --- a/NzbDrone.Core/Jobs/JobProvider.cs +++ b/NzbDrone.Core/Jobs/JobProvider.cs @@ -1,5 +1,3 @@ -//https://github.com/kayone/NzbDrone/blob/master/NzbDrone.Core/Providers/Jobs/JobProvider.cs - using System; using System.Collections.Generic; using System.ComponentModel; @@ -29,7 +27,7 @@ namespace NzbDrone.Core.Jobs private Thread _jobThread; public Stopwatch StopWatch { get; private set; } - private readonly object executionLock = new object(); + private readonly object _executionLock = new object(); private readonly List<JobQueueItem> _queue = new List<JobQueueItem>(); private ProgressNotification _notification; @@ -42,6 +40,7 @@ namespace NzbDrone.Core.Jobs _notificationProvider = notificationProvider; _jobs = jobs; ResetThread(); + Initialize(); } /// <summary> @@ -67,7 +66,7 @@ namespace NzbDrone.Core.Jobs return _database.Fetch<JobDefinition>().ToList(); } - public virtual void Initialize() + private void Initialize() { var currentJobs = All(); logger.Debug("Initializing jobs. Available: {0} Existing:{1}", _jobs.Count(), currentJobs.Count); @@ -121,7 +120,7 @@ namespace NzbDrone.Core.Jobs public virtual void QueueScheduled() { - lock (executionLock) + lock (_executionLock) { VerifyThreadTime(); @@ -153,7 +152,7 @@ namespace NzbDrone.Core.Jobs logger.Debug("Attempting to queue {0}", queueItem); - lock (executionLock) + lock (_executionLock) { VerifyThreadTime(); @@ -215,7 +214,7 @@ namespace NzbDrone.Core.Jobs { if (Queue.Count != 0) { - job = Queue.OrderBy(c=>c.Source).First(); + job = Queue.OrderBy(c => c.Source).First(); logger.Trace("Popping {0} from the queue.", job); Queue.Remove(job); } diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index ea837fe2e..63e862f90 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -222,6 +222,7 @@ <Link>Properties\SharedAssemblyInfo.cs</Link> </Compile> <Compile Include="Constants.cs" /> + <Compile Include="ContainerExtentions.cs" /> <Compile Include="Datastore\Connection.cs" /> <Compile Include="Datastore\MigrationLogger.cs" /> <Compile Include="Datastore\MigrationsHelper.cs" /> @@ -613,6 +614,7 @@ </ItemGroup> <ItemGroup> <None Include="App.config" /> + <None Include="ClassDiagram1.cd" /> <None Include="packages.config" /> <None Include="Properties\AnalysisRules.ruleset" /> </ItemGroup> diff --git a/NzbDrone.Core/Properties/AssemblyInfo.cs b/NzbDrone.Core/Properties/AssemblyInfo.cs index 0445cde9a..3e61062d3 100644 --- a/NzbDrone.Core/Properties/AssemblyInfo.cs +++ b/NzbDrone.Core/Properties/AssemblyInfo.cs @@ -18,4 +18,4 @@ using System.Runtime.InteropServices; // by using the '*' as shown below: [assembly: AssemblyVersion("1.0.0.*")] -[assembly: AssemblyFileVersion("1.0.0.*")] \ No newline at end of file +[assembly: AssemblyFileVersion("1.0.0.*")] diff --git a/NzbDrone.Core/Providers/ExternalNotificationProvider.cs b/NzbDrone.Core/Providers/ExternalNotificationProvider.cs index e17c50b8b..4dcb261ca 100644 --- a/NzbDrone.Core/Providers/ExternalNotificationProvider.cs +++ b/NzbDrone.Core/Providers/ExternalNotificationProvider.cs @@ -14,12 +14,14 @@ namespace NzbDrone.Core.Providers private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private readonly IDatabase _database; - private IEnumerable<ExternalNotificationBase> _notifiers; + private IList<ExternalNotificationBase> _notifiers; public ExternalNotificationProvider(IDatabase database, IEnumerable<ExternalNotificationBase> notifiers) { _database = database; - _notifiers = notifiers; + _notifiers = notifiers.ToList(); + + InitializeNotifiers(_notifiers); } public ExternalNotificationProvider() @@ -58,7 +60,7 @@ namespace NzbDrone.Core.Providers return _notifiers.Where(i => all.Exists(c => c.ExternalNotificationProviderType == i.GetType().ToString() && c.Enable)).ToList(); } - public virtual void InitializeNotifiers(IList<ExternalNotificationBase> notifiers) + private void InitializeNotifiers(IList<ExternalNotificationBase> notifiers) { Logger.Debug("Initializing notifiers. Count {0}", notifiers.Count); diff --git a/NzbDrone.Core/Providers/IndexerProvider.cs b/NzbDrone.Core/Providers/IndexerProvider.cs index 8c3703121..acb487fe3 100644 --- a/NzbDrone.Core/Providers/IndexerProvider.cs +++ b/NzbDrone.Core/Providers/IndexerProvider.cs @@ -13,12 +13,14 @@ namespace NzbDrone.Core.Providers private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private readonly IDatabase _database; - private IEnumerable<IndexerBase> _indexers; + private IList<IndexerBase> _indexers; public IndexerProvider(IDatabase database, IEnumerable<IndexerBase> indexers) { _database = database; - _indexers = indexers; + _indexers = indexers.ToList(); + InitializeIndexers(); + } public IndexerProvider() @@ -56,15 +58,14 @@ namespace NzbDrone.Core.Providers return _database.Single<IndexerDefinition>("WHERE IndexProviderType = @0", type.ToString()); } - public virtual void InitializeIndexers(IList<IndexerBase> indexers) + private void InitializeIndexers() { - Logger.Debug("Initializing indexers. Count {0}", indexers.Count); + Logger.Debug("Initializing indexers. Count {0}", _indexers.Count); - _indexers = indexers; var currentIndexers = All(); - foreach (var feedProvider in indexers) + foreach (var feedProvider in _indexers) { IndexerBase indexerLocal = feedProvider; if (!currentIndexers.Exists(c => c.IndexProviderType == indexerLocal.GetType().ToString())) diff --git a/NzbDrone.Core/Providers/MetadataProvider.cs b/NzbDrone.Core/Providers/MetadataProvider.cs index b5ca8146e..c0ebde1ac 100644 --- a/NzbDrone.Core/Providers/MetadataProvider.cs +++ b/NzbDrone.Core/Providers/MetadataProvider.cs @@ -16,15 +16,17 @@ namespace NzbDrone.Core.Providers private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private readonly IDatabase _database; - private IEnumerable<MetadataBase> _metadataProviders; + private IList<MetadataBase> _metadataProviders; private readonly TvDbProvider _tvDbProvider; public MetadataProvider(IDatabase database, IEnumerable<MetadataBase> metadataProviders, TvDbProvider tvDbProvider) { _database = database; - _metadataProviders = metadataProviders; + _metadataProviders = metadataProviders.ToList(); _tvDbProvider = tvDbProvider; + + Initialize(_metadataProviders); } public MetadataProvider() @@ -63,7 +65,7 @@ namespace NzbDrone.Core.Providers return _metadataProviders.Where(i => all.Exists(c => c.MetadataProviderType == i.GetType().ToString() && c.Enable)).ToList(); } - public virtual void Initialize(IList<MetadataBase> metabaseProviders) + private void Initialize(IList<MetadataBase> metabaseProviders) { Logger.Debug("Initializing metabases. Count {0}", metabaseProviders.Count); diff --git a/NzbDrone.Core/Providers/NewznabProvider.cs b/NzbDrone.Core/Providers/NewznabProvider.cs index 5c91de71a..cd3642aaf 100644 --- a/NzbDrone.Core/Providers/NewznabProvider.cs +++ b/NzbDrone.Core/Providers/NewznabProvider.cs @@ -17,6 +17,15 @@ namespace NzbDrone.Core.Providers public NewznabProvider(IDatabase database) { _database = database; + + var newznabIndexers = new List<NewznabDefinition> + { + new NewznabDefinition { Enable = false, Name = "Nzbs.org", Url = "http://nzbs.org", BuiltIn = true }, + new NewznabDefinition { Enable = false, Name = "Nzb.su", Url = "https://nzb.su", BuiltIn = true }, + new NewznabDefinition { Enable = false, Name = "Dognzb.cr", Url = "https://dognzb.cr", BuiltIn = true } + }; + + InitializeNewznabIndexers(newznabIndexers); } public NewznabProvider() @@ -55,7 +64,7 @@ namespace NzbDrone.Core.Providers var definitionsList = definitions.ToList(); //Cleanup the URL for each definition - foreach(var newznabDefinition in definitionsList) + foreach (var newznabDefinition in definitionsList) { CheckHostname(newznabDefinition.Url); //newznabDefinition.Url = new Uri(newznabDefinition.Url).ParentUriString(); @@ -64,7 +73,7 @@ namespace NzbDrone.Core.Providers _database.UpdateMany(definitionsList); } - public virtual void InitializeNewznabIndexers(IList<NewznabDefinition> indexers) + private void InitializeNewznabIndexers(IList<NewznabDefinition> indexers) { Logger.Debug("Initializing Newznab indexers. Count {0}", indexers.Count); @@ -78,7 +87,7 @@ namespace NzbDrone.Core.Providers currentIndexers = All(); - foreach(var feedProvider in indexers) + foreach (var feedProvider in indexers) { try { @@ -115,7 +124,7 @@ namespace NzbDrone.Core.Providers } } } - catch(Exception ex) + catch (Exception ex) { Logger.ErrorException("An Error occurred while initializing Newznab Indexers", ex); } diff --git a/NzbDrone.Core/Providers/QualityProvider.cs b/NzbDrone.Core/Providers/QualityProvider.cs index a1240276d..82410935a 100644 --- a/NzbDrone.Core/Providers/QualityProvider.cs +++ b/NzbDrone.Core/Providers/QualityProvider.cs @@ -19,6 +19,7 @@ namespace NzbDrone.Core.Providers public QualityProvider(IDatabase database) { _database = database; + SetupDefaultProfiles(); } public virtual int Add(QualityProfile profile) @@ -54,7 +55,7 @@ namespace NzbDrone.Core.Providers return _database.Single<QualityProfile>(profileId); } - public virtual void SetupDefaultProfiles() + private void SetupDefaultProfiles() { if (All().Count != 0) return; diff --git a/NzbDrone.Core/Providers/QualityTypeProvider.cs b/NzbDrone.Core/Providers/QualityTypeProvider.cs index fba4d417c..3b4086feb 100644 --- a/NzbDrone.Core/Providers/QualityTypeProvider.cs +++ b/NzbDrone.Core/Providers/QualityTypeProvider.cs @@ -17,11 +17,12 @@ namespace NzbDrone.Core.Providers public QualityTypeProvider(IDatabase database) { _database = database; + SetupDefault(); } public QualityTypeProvider() { - + } public virtual void Update(QualityType qualityType) @@ -52,13 +53,13 @@ namespace NzbDrone.Core.Providers return _database.Fetch<QualityType>(query); } - public virtual void SetupDefault() + private void SetupDefault() { var inDb = All(); Logger.Debug("Setting up default quality types"); - foreach(var qualityType in QualityTypes.All()) + foreach (var qualityType in QualityTypes.All()) { //Skip UNKNOWN if (qualityType.Id == 0) continue; diff --git a/NzbDrone.Web/Global.asax.cs b/NzbDrone.Web/Global.asax.cs index 36a8bc371..4c158a2dd 100644 --- a/NzbDrone.Web/Global.asax.cs +++ b/NzbDrone.Web/Global.asax.cs @@ -1,7 +1,6 @@ using System; using System.Data.Common; using System.Linq; -using System.Reflection; using System.Threading; using System.Web; using System.Web.Mvc; @@ -9,16 +8,10 @@ using System.Web.Routing; using Autofac; using Autofac.Integration.Mvc; using LowercaseRoutesMVC; -using NLog.Config; using NLog; -using NzbDrone.Api; -using NzbDrone.Common; using NzbDrone.Core; -using ServiceStack.CacheAccess; -using ServiceStack.CacheAccess.Providers; using NzbDrone.Core.Repository.Quality; using NzbDrone.Web.Helpers.Binders; -using ServiceStack.ServiceInterface; using SignalR; namespace NzbDrone.Web @@ -82,9 +75,6 @@ namespace NzbDrone.Web //SignalR RouteTable.Routes.MapHubs(); - //ServiceStack - dispatch.ContainerBuilder.RegisterType<MemoryCacheClient>().As<ICacheClient>().SingleInstance(); - dispatch.ContainerBuilder.RegisterType<SessionFactory>().As<ISessionFactory>().SingleInstance(); } private static void MVCRegistration(ContainerBuilder builder) diff --git a/NzbDrone.Web/NzbDrone.Web.csproj b/NzbDrone.Web/NzbDrone.Web.csproj index 54959030f..6fdcc3518 100644 --- a/NzbDrone.Web/NzbDrone.Web.csproj +++ b/NzbDrone.Web/NzbDrone.Web.csproj @@ -107,38 +107,6 @@ <Reference Include="NLog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL"> <HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath> </Reference> - <Reference Include="ServiceStack, Version=3.9.25.0, Culture=neutral, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>..\packages\ServiceStack.3.9.25\lib\net35\ServiceStack.dll</HintPath> - </Reference> - <Reference Include="ServiceStack.Common, Version=3.9.25.0, Culture=neutral, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>..\packages\ServiceStack.Common.3.9.25\lib\net35\ServiceStack.Common.dll</HintPath> - </Reference> - <Reference Include="ServiceStack.FluentValidation.Mvc3, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>..\packages\ServiceStack.Mvc.3.9.25\lib\net40\ServiceStack.FluentValidation.Mvc3.dll</HintPath> - </Reference> - <Reference Include="ServiceStack.Interfaces, Version=3.9.25.0, Culture=neutral, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>..\packages\ServiceStack.Common.3.9.25\lib\net35\ServiceStack.Interfaces.dll</HintPath> - </Reference> - <Reference Include="ServiceStack.OrmLite, Version=3.9.26.0, Culture=neutral, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>..\packages\ServiceStack.OrmLite.SqlServer.3.9.26\lib\ServiceStack.OrmLite.dll</HintPath> - </Reference> - <Reference Include="ServiceStack.OrmLite.SqlServer, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>..\packages\ServiceStack.OrmLite.SqlServer.3.9.26\lib\ServiceStack.OrmLite.SqlServer.dll</HintPath> - </Reference> - <Reference Include="ServiceStack.Redis, Version=3.9.25.0, Culture=neutral, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>..\packages\ServiceStack.Redis.3.9.25\lib\net35\ServiceStack.Redis.dll</HintPath> - </Reference> - <Reference Include="ServiceStack.ServiceInterface, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>..\packages\ServiceStack.3.9.25\lib\net35\ServiceStack.ServiceInterface.dll</HintPath> - </Reference> <Reference Include="ServiceStack.Text, Version=3.9.27.0, Culture=neutral, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..\packages\ServiceStack.Text.3.9.27\lib\net35\ServiceStack.Text.dll</HintPath> diff --git a/NzbDrone.Web/packages.config b/NzbDrone.Web/packages.config index 8ace939a0..0d7d3f721 100644 --- a/NzbDrone.Web/packages.config +++ b/NzbDrone.Web/packages.config @@ -28,11 +28,6 @@ <package id="MiniProfiler.MVC3" version="2.0.2" /> <package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" /> <package id="NLog" version="2.0.0.2000" /> - <package id="ServiceStack" version="3.9.25" targetFramework="net40" /> - <package id="ServiceStack.Common" version="3.9.25" targetFramework="net40" /> - <package id="ServiceStack.Mvc" version="3.9.25" targetFramework="net40" /> - <package id="ServiceStack.OrmLite.SqlServer" version="3.9.26" targetFramework="net40" /> - <package id="ServiceStack.Redis" version="3.9.25" targetFramework="net40" /> <package id="ServiceStack.Text" version="3.9.27" targetFramework="net40" /> <package id="SignalR.Hosting.AspNet" version="0.5.3" targetFramework="net40" /> <package id="SignalR.Hosting.Common" version="0.5.3" targetFramework="net40" />