From 0e1f1ecd4077aa0338c0912e7fdc6793a218b75c Mon Sep 17 00:00:00 2001 From: tidusjar Date: Wed, 29 Jun 2016 17:06:58 +0100 Subject: [PATCH 001/138] Moved over to using Ninject --- PlexRequests.UI/Bootstrapper.cs | 114 +++++------------- PlexRequests.UI/Helpers/ServiceLocator.cs | 10 +- PlexRequests.UI/NinjectModules/ApiModule.cs | 50 ++++++++ .../NinjectModules/ConfigurationModule.cs | 55 +++++++++ .../NinjectModules/DependancyResolver.cs | 48 ++++++++ .../NinjectModules/RepositoryModule.cs | 52 ++++++++ .../NinjectModules/ServicesModule.cs | 55 +++++++++ .../NinjectModules/SettingServiceModule.cs | 40 ++++++ PlexRequests.UI/PlexRequests.UI.csproj | 18 +++ PlexRequests.UI/Startup.cs | 28 ++++- PlexRequests.UI/packages.config | 3 + 11 files changed, 383 insertions(+), 90 deletions(-) create mode 100644 PlexRequests.UI/NinjectModules/ApiModule.cs create mode 100644 PlexRequests.UI/NinjectModules/ConfigurationModule.cs create mode 100644 PlexRequests.UI/NinjectModules/DependancyResolver.cs create mode 100644 PlexRequests.UI/NinjectModules/RepositoryModule.cs create mode 100644 PlexRequests.UI/NinjectModules/ServicesModule.cs create mode 100644 PlexRequests.UI/NinjectModules/SettingServiceModule.cs diff --git a/PlexRequests.UI/Bootstrapper.cs b/PlexRequests.UI/Bootstrapper.cs index d29c9c324..249198332 100644 --- a/PlexRequests.UI/Bootstrapper.cs +++ b/PlexRequests.UI/Bootstrapper.cs @@ -32,43 +32,46 @@ using Mono.Data.Sqlite; using Nancy; using Nancy.Authentication.Forms; using Nancy.Bootstrapper; +using Nancy.Bootstrappers.Ninject; using Nancy.Conventions; using Nancy.Cryptography; using Nancy.Diagnostics; using Nancy.Session; -using Nancy.TinyIoc; -using PlexRequests.Api; using PlexRequests.Api.Interfaces; using PlexRequests.Core; using PlexRequests.Core.SettingModels; using PlexRequests.Helpers; -using PlexRequests.Services; using PlexRequests.Services.Interfaces; using PlexRequests.Services.Notification; using PlexRequests.Store; -using PlexRequests.Store.Models; using PlexRequests.Store.Repository; using PlexRequests.UI.Helpers; using Nancy.Json; -using PlexRequests.Helpers.Analytics; -using PlexRequests.Services.Jobs; -using PlexRequests.UI.Jobs; - -using Quartz; -using Quartz.Impl; -using Quartz.Spi; +using Ninject; namespace PlexRequests.UI { - public class Bootstrapper : DefaultNancyBootstrapper + public class Bootstrapper : NinjectNancyBootstrapper { // The bootstrapper enables you to reconfigure the composition of the framework, // by overriding the various methods and properties. // For more information https://github.com/NancyFx/Nancy/wiki/Bootstrapper - protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) + public Bootstrapper(IKernel kernel) + { + _kernel = kernel; + } + + private IKernel _kernel; + protected override IKernel GetApplicationContainer() + { + _kernel.Load(); + return _kernel; + } + + protected override void ApplicationStartup(IKernel container, IPipelines pipelines) { ConfigureContainer(container); @@ -87,7 +90,7 @@ namespace PlexRequests.UI var formsAuthConfiguration = new FormsAuthenticationConfiguration { RedirectUrl = redirect, - UserMapper = container.Resolve() + UserMapper = container.Get() }; FormsAuthentication.Enable(pipelines, formsAuthConfiguration); @@ -115,40 +118,40 @@ namespace PlexRequests.UI protected override DiagnosticsConfiguration DiagnosticsConfiguration => new DiagnosticsConfiguration { Password = @"password" }; - private void SubscribeAllObservers(TinyIoCContainer container) + private void SubscribeAllObservers(IKernel container) { - var notificationService = container.Resolve(); + var notificationService = container.Get(); - var emailSettingsService = container.Resolve>(); + var emailSettingsService = container.Get>(); var emailSettings = emailSettingsService.GetSettings(); if (emailSettings.Enabled) { notificationService.Subscribe(new EmailMessageNotification(emailSettingsService)); } - var pushbulletService = container.Resolve>(); + var pushbulletService = container.Get>(); var pushbulletSettings = pushbulletService.GetSettings(); if (pushbulletSettings.Enabled) { - notificationService.Subscribe(new PushbulletNotification(container.Resolve(), pushbulletService)); + notificationService.Subscribe(new PushbulletNotification(container.Get(), pushbulletService)); } - var pushoverService = container.Resolve>(); + var pushoverService = container.Get>(); var pushoverSettings = pushoverService.GetSettings(); if (pushoverSettings.Enabled) { - notificationService.Subscribe(new PushoverNotification(container.Resolve(), pushoverService)); + notificationService.Subscribe(new PushoverNotification(container.Get(), pushoverService)); } - var slackService = container.Resolve>(); + var slackService = container.Get>(); var slackSettings = slackService.GetSettings(); if (slackSettings.Enabled) { - notificationService.Subscribe(new SlackNotification(container.Resolve(), slackService)); + notificationService.Subscribe(new SlackNotification(container.Get(), slackService)); } } - protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context) + protected override void RequestStartup(IKernel container, IPipelines pipelines, NancyContext context) { //CORS Enable pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) => @@ -161,68 +164,9 @@ namespace PlexRequests.UI base.RequestStartup(container, pipelines, context); } - private void ConfigureContainer(TinyIoCContainer container) + private void ConfigureContainer(IKernel container) { - container.Register().AsSingleton(); - container.Register(new DbConfiguration(new SqliteFactory())); - container.Register, UserRepository>(); - container.Register(); - container.Register(); - container.Register, SettingsServiceV2>(); - container.Register, SettingsServiceV2>(); - container.Register, SettingsServiceV2>(); - container.Register, SettingsServiceV2>(); - container.Register, SettingsServiceV2>(); - - // Notification Service - container.Register().AsSingleton(); - // Settings - container.Register, SettingsServiceV2>(); - container.Register, SettingsServiceV2>(); - container.Register, SettingsServiceV2>(); - container.Register, SettingsServiceV2>(); - container.Register, SettingsServiceV2>(); - container.Register, SettingsServiceV2>(); - - container.Register, SettingsServiceV2>(); - container.Register, SettingsServiceV2>(); - container.Register, SettingsServiceV2>(); - - // Repo's - container.Register, GenericRepository>(); - container.Register, GenericRepository>(); - container.Register, GenericRepository>(); - container.Register, GenericRepository>(); - container.Register, GenericRepository>(); - container.Register, GenericRepository>(); - container.Register(); - container.Register(); - container.Register(); - container.Register(); - - // Services - container.Register(); - container.Register(); - container.Register(); - container.Register(); - container.Register(); - - container.Register(); - container.Register(); - container.Register(); - - - // Api - container.Register(); - container.Register(); - container.Register(); - container.Register(); - container.Register(); - container.Register(); - container.Register(); - container.Register(); - container.Register(); - + var loc = ServiceLocator.Instance; loc.SetContainer(container); } diff --git a/PlexRequests.UI/Helpers/ServiceLocator.cs b/PlexRequests.UI/Helpers/ServiceLocator.cs index 40e82e434..54abe5bcd 100644 --- a/PlexRequests.UI/Helpers/ServiceLocator.cs +++ b/PlexRequests.UI/Helpers/ServiceLocator.cs @@ -28,6 +28,8 @@ using System; using Nancy.TinyIoc; +using Ninject; + namespace PlexRequests.UI.Helpers { public class ServiceLocator : IServiceLocator @@ -37,21 +39,21 @@ namespace PlexRequests.UI.Helpers Singleton = new ServiceLocator(); } private static ServiceLocator Singleton { get; } - private TinyIoCContainer Container { get; set; } + private IKernel Container { get; set; } public static ServiceLocator Instance => Singleton; - public void SetContainer(TinyIoCContainer con) + public void SetContainer(IKernel con) { Container = con; } public T Resolve() where T : class { - return Container?.Resolve(); + return Container?.Get(); } public object Resolve(Type type) { - return Container.Resolve(type); + return Container.Get(type); } } diff --git a/PlexRequests.UI/NinjectModules/ApiModule.cs b/PlexRequests.UI/NinjectModules/ApiModule.cs new file mode 100644 index 000000000..aad96d22b --- /dev/null +++ b/PlexRequests.UI/NinjectModules/ApiModule.cs @@ -0,0 +1,50 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: ApiModule.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using Ninject.Modules; + +using PlexRequests.Api; +using PlexRequests.Api.Interfaces; + +namespace PlexRequests.UI.NinjectModules +{ + public class ApiModule : NinjectModule + { + public override void Load() + { + Bind().To(); + Bind().To(); + Bind().To(); + Bind().To(); + Bind().To(); + Bind().To(); + Bind().To(); + Bind().To(); + Bind().To(); + Bind().To(); + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/NinjectModules/ConfigurationModule.cs b/PlexRequests.UI/NinjectModules/ConfigurationModule.cs new file mode 100644 index 000000000..5bb10dca3 --- /dev/null +++ b/PlexRequests.UI/NinjectModules/ConfigurationModule.cs @@ -0,0 +1,55 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: ConfigurationModule.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using Mono.Data.Sqlite; + +using Nancy; +using Nancy.Authentication.Forms; + +using Ninject.Modules; + +using PlexRequests.Core; +using PlexRequests.Helpers; +using PlexRequests.Services.Interfaces; +using PlexRequests.Services.Notification; +using PlexRequests.Store; + +namespace PlexRequests.UI.NinjectModules +{ + public class ConfigurationModule : NinjectModule + { + public override void Load() + { + Bind().To().InSingletonScope(); + Bind().To().WithConstructorArgument("provider", new SqliteFactory()); + + Bind().To(); + Bind().To(); + + Bind().To().InSingletonScope(); + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/NinjectModules/DependancyResolver.cs b/PlexRequests.UI/NinjectModules/DependancyResolver.cs new file mode 100644 index 000000000..3a245eced --- /dev/null +++ b/PlexRequests.UI/NinjectModules/DependancyResolver.cs @@ -0,0 +1,48 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: DependancyResolver.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using System; +using System.Linq; +using System.Reflection; + +using Ninject.Modules; + +namespace PlexRequests.UI.NinjectModules +{ + public class DependancyResolver + { + public NinjectModule[] GetModules() + { + var path = Assembly.GetAssembly(typeof(SettingServiceModule)).Location; + var result = Assembly.LoadFrom(path).GetTypes() + .Where(a => + a.IsClass && + a.BaseType == typeof(NinjectModule)); + + return result.Select(r => Activator.CreateInstance(r) as NinjectModule).ToArray(); + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/NinjectModules/RepositoryModule.cs b/PlexRequests.UI/NinjectModules/RepositoryModule.cs new file mode 100644 index 000000000..850926f74 --- /dev/null +++ b/PlexRequests.UI/NinjectModules/RepositoryModule.cs @@ -0,0 +1,52 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: RepositoryModule.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using Ninject.Modules; + +using PlexRequests.Core; +using PlexRequests.Services.Interfaces; +using PlexRequests.Services.Jobs; +using PlexRequests.Store; +using PlexRequests.Store.Repository; + +namespace PlexRequests.UI.NinjectModules +{ + public class RepositoryModule : NinjectModule + { + public override void Load() + { + Bind>().To>(); + Bind(typeof(IRepository<>)).To(typeof(GenericRepository<>)); + + Bind().To(); + Bind().To(); + Bind().To(); + Bind().To(); + Bind().To(); + } + + } +} \ No newline at end of file diff --git a/PlexRequests.UI/NinjectModules/ServicesModule.cs b/PlexRequests.UI/NinjectModules/ServicesModule.cs new file mode 100644 index 000000000..e2ce94dd7 --- /dev/null +++ b/PlexRequests.UI/NinjectModules/ServicesModule.cs @@ -0,0 +1,55 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: ServicesModule.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using Ninject.Modules; + +using PlexRequests.Helpers.Analytics; +using PlexRequests.Services.Interfaces; +using PlexRequests.Services.Jobs; +using PlexRequests.UI.Jobs; + +using Quartz; +using Quartz.Impl; +using Quartz.Spi; + +namespace PlexRequests.UI.NinjectModules +{ + public class ServicesModule : NinjectModule + { + public override void Load() + { + Bind().To(); + Bind().To(); + Bind().To(); + Bind().To(); + Bind().To(); + + Bind().To(); + Bind().To(); + Bind().To(); + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/NinjectModules/SettingServiceModule.cs b/PlexRequests.UI/NinjectModules/SettingServiceModule.cs new file mode 100644 index 000000000..309c5e281 --- /dev/null +++ b/PlexRequests.UI/NinjectModules/SettingServiceModule.cs @@ -0,0 +1,40 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: SettingServiceModule.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using Ninject.Modules; + +using PlexRequests.Core; + +namespace PlexRequests.UI.NinjectModules +{ + public class SettingServiceModule : NinjectModule + { + public override void Load() + { + Bind(typeof(ISettingsService<>)).To(typeof(SettingsServiceV2<>)); + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/PlexRequests.UI.csproj b/PlexRequests.UI/PlexRequests.UI.csproj index 0495bb18b..b04cda748 100644 --- a/PlexRequests.UI/PlexRequests.UI.csproj +++ b/PlexRequests.UI/PlexRequests.UI.csproj @@ -57,6 +57,10 @@ ..\packages\Nancy.1.4.3\lib\net40\Nancy.dll True + + ..\packages\Nancy.Bootstrappers.Ninject.1.4.1\lib\net40\Nancy.Bootstrappers.Ninject.dll + True + ..\packages\Nancy.Metadata.Modules.1.4.1\lib\net40\Nancy.Metadata.Modules.dll True @@ -65,6 +69,14 @@ ..\packages\Nancy.Swagger.0.1.0-alpha3\lib\net40\Nancy.Swagger.dll True + + ..\packages\Ninject.3.0.1.10\lib\net45-full\Ninject.dll + True + + + ..\packages\Ninject.Extensions.ChildKernel.3.0.0.5\lib\net45-full\Ninject.Extensions.ChildKernel.dll + True + ..\packages\NLog.4.3.4\lib\net45\NLog.dll True @@ -202,6 +214,12 @@ + + + + + + True True diff --git a/PlexRequests.UI/Startup.cs b/PlexRequests.UI/Startup.cs index 6f7f5fd8c..43e91815e 100644 --- a/PlexRequests.UI/Startup.cs +++ b/PlexRequests.UI/Startup.cs @@ -28,12 +28,17 @@ using System; using Nancy.TinyIoc; +using Ninject; +using Ninject.Modules; +using Ninject.Planning.Bindings.Resolvers; + using NLog; using Owin; using PlexRequests.UI.Helpers; using PlexRequests.UI.Jobs; +using PlexRequests.UI.NinjectModules; namespace PlexRequests.UI { @@ -45,7 +50,28 @@ namespace PlexRequests.UI { try { - app.UseNancy(); + var resolver = new DependancyResolver(); + var modules = resolver.GetModules(); + var kernel = new StandardKernel(modules); + + //kernel.Bind(x => x.FromThisAssembly() + // .SelectAllClasses() + // .InheritedFromAny( + // new[] + // { + // typeof(IRequestHandler<,>), + // typeof(IAsyncRequestHandler<,>), + // }) + // .BindDefaultInterfaces()); + + //kernel.Components.Add(); + //kernel.Bind(scan => scan.FromAssemblyContaining().SelectAllClasses().BindDefaultInterface()); + //kernel.Bind(scan => scan.FromAssemblyContaining().SelectAllInterfaces().BindAllInterfaces()); + + //kernel.Bind().ToMethod(ctx => t => ctx.Kernel.Get(t)); + //kernel.Bind().ToMethod(ctx => t => ctx.Kernel.GetAll(t)); + + app.UseNancy(options => options.Bootstrapper = new Bootstrapper(kernel)); var scheduler = new Scheduler(); scheduler.StartScheduler(); } diff --git a/PlexRequests.UI/packages.config b/PlexRequests.UI/packages.config index 7fa8497ea..e8f1649ae 100644 --- a/PlexRequests.UI/packages.config +++ b/PlexRequests.UI/packages.config @@ -19,6 +19,7 @@ + @@ -27,6 +28,8 @@ + + From d4074fc0f338470d13fa2d7c3fdf7518e123c41e Mon Sep 17 00:00:00 2001 From: tidusjar Date: Wed, 29 Jun 2016 17:07:51 +0100 Subject: [PATCH 002/138] Added MediatR --- PlexRequests.Core/PlexRequests.Core.csproj | 4 ++++ PlexRequests.Core/packages.config | 1 + PlexRequests.UI/PlexRequests.UI.csproj | 4 ++++ PlexRequests.UI/packages.config | 1 + 4 files changed, 10 insertions(+) diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 964bb6d01..09706dcb8 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -31,6 +31,10 @@ 4 + + ..\packages\MediatR.2.0.2\lib\net45\MediatR.dll + True + ..\Assemblies\Mono.Data.Sqlite.dll diff --git a/PlexRequests.Core/packages.config b/PlexRequests.Core/packages.config index 9f54b7236..4f9a2bb1f 100644 --- a/PlexRequests.Core/packages.config +++ b/PlexRequests.Core/packages.config @@ -1,5 +1,6 @@  + diff --git a/PlexRequests.UI/PlexRequests.UI.csproj b/PlexRequests.UI/PlexRequests.UI.csproj index b04cda748..c4592ef65 100644 --- a/PlexRequests.UI/PlexRequests.UI.csproj +++ b/PlexRequests.UI/PlexRequests.UI.csproj @@ -53,6 +53,10 @@ 4 + + ..\packages\MediatR.2.0.2\lib\net45\MediatR.dll + True + ..\packages\Nancy.1.4.3\lib\net40\Nancy.dll True diff --git a/PlexRequests.UI/packages.config b/PlexRequests.UI/packages.config index e8f1649ae..11c8e9abb 100644 --- a/PlexRequests.UI/packages.config +++ b/PlexRequests.UI/packages.config @@ -9,6 +9,7 @@ + From 7a6ee7e98dd8e2aa92d057a420bcfdb204c0a5ee Mon Sep 17 00:00:00 2001 From: tidusjar Date: Wed, 29 Jun 2016 17:16:50 +0100 Subject: [PATCH 003/138] Plugged in MediatR --- PlexRequests.UI.Tests/app.config | 8 +++ .../Helpers/ContravariantBindingResolver.cs | 65 +++++++++++++++++++ .../NinjectModules/MediatRModule.cs | 52 +++++++++++++++ PlexRequests.UI/PlexRequests.UI.csproj | 14 ++-- PlexRequests.UI/Startup.cs | 20 ++---- PlexRequests.UI/app.config | 8 +++ PlexRequests.UI/packages.config | 5 +- 7 files changed, 151 insertions(+), 21 deletions(-) create mode 100644 PlexRequests.UI/Helpers/ContravariantBindingResolver.cs create mode 100644 PlexRequests.UI/NinjectModules/MediatRModule.cs diff --git a/PlexRequests.UI.Tests/app.config b/PlexRequests.UI.Tests/app.config index 3799f3a1a..be6958e15 100644 --- a/PlexRequests.UI.Tests/app.config +++ b/PlexRequests.UI.Tests/app.config @@ -15,6 +15,14 @@ + + + + + + + + diff --git a/PlexRequests.UI/Helpers/ContravariantBindingResolver.cs b/PlexRequests.UI/Helpers/ContravariantBindingResolver.cs new file mode 100644 index 000000000..f7551d280 --- /dev/null +++ b/PlexRequests.UI/Helpers/ContravariantBindingResolver.cs @@ -0,0 +1,65 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: ContravariantBindingResolver.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +using Ninject.Components; +using Ninject.Infrastructure; +using Ninject.Planning.Bindings; +using Ninject.Planning.Bindings.Resolvers; + +namespace PlexRequests.UI.Helpers +{ + public class ContravariantBindingResolver : NinjectComponent, IBindingResolver + { + /// + /// Returns any bindings from the specified collection that match the specified service. + /// + public IEnumerable Resolve(Multimap bindings, Type service) + { + if (service.IsGenericType) + { + var genericType = service.GetGenericTypeDefinition(); + var genericArguments = genericType.GetGenericArguments(); + if (genericArguments.Length == 1 && genericArguments.Single().GenericParameterAttributes.HasFlag(GenericParameterAttributes.Contravariant)) + { + var argument = service.GetGenericArguments().Single(); + var matches = + bindings.Where( + kvp => + kvp.Key.IsGenericType && kvp.Key.GetGenericTypeDefinition() == genericType && kvp.Key.GetGenericArguments().Single() != argument + && kvp.Key.GetGenericArguments().Single().IsAssignableFrom(argument)).SelectMany(kvp => kvp.Value); + return matches; + } + } + + return Enumerable.Empty(); + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/NinjectModules/MediatRModule.cs b/PlexRequests.UI/NinjectModules/MediatRModule.cs new file mode 100644 index 000000000..a63fc4a26 --- /dev/null +++ b/PlexRequests.UI/NinjectModules/MediatRModule.cs @@ -0,0 +1,52 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: MediatRModule.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using MediatR; + +using Ninject; +using Ninject.Extensions.Conventions; +using Ninject.Modules; + +namespace PlexRequests.UI.NinjectModules +{ + public class MediatRModule : NinjectModule + { + public override void Load() + { + Kernel.Bind(scan => scan.FromAssemblyContaining().SelectAllClasses().BindDefaultInterface()); + + Bind().ToMethod(ctx => t => ctx.Kernel.Get(t)); + Bind().ToMethod(ctx => t => ctx.Kernel.GetAll(t)); + + Kernel.Bind(x => x.FromThisAssembly() + .SelectAllClasses() + .InheritedFromAny(typeof(IRequestHandler<,>), typeof(IAsyncRequestHandler<,>)) + .BindDefaultInterfaces()); + + //kernel.Bind(scan => scan.FromAssemblyContaining().SelectAllInterfaces().BindAllInterfaces()); + } + } +} \ No newline at end of file diff --git a/PlexRequests.UI/PlexRequests.UI.csproj b/PlexRequests.UI/PlexRequests.UI.csproj index c4592ef65..150f818c7 100644 --- a/PlexRequests.UI/PlexRequests.UI.csproj +++ b/PlexRequests.UI/PlexRequests.UI.csproj @@ -73,12 +73,16 @@ ..\packages\Nancy.Swagger.0.1.0-alpha3\lib\net40\Nancy.Swagger.dll True - - ..\packages\Ninject.3.0.1.10\lib\net45-full\Ninject.dll + + ..\packages\Ninject.3.2.0.0\lib\net45-full\Ninject.dll True - - ..\packages\Ninject.Extensions.ChildKernel.3.0.0.5\lib\net45-full\Ninject.Extensions.ChildKernel.dll + + ..\packages\Ninject.Extensions.ChildKernel.3.2.0.0\lib\net45-full\Ninject.Extensions.ChildKernel.dll + True + + + ..\packages\Ninject.Extensions.Conventions.3.2.0.0\lib\net45-full\Ninject.Extensions.Conventions.dll True @@ -175,6 +179,7 @@ + @@ -221,6 +226,7 @@ + diff --git a/PlexRequests.UI/Startup.cs b/PlexRequests.UI/Startup.cs index 43e91815e..2d0bcb221 100644 --- a/PlexRequests.UI/Startup.cs +++ b/PlexRequests.UI/Startup.cs @@ -26,9 +26,12 @@ #endregion using System; +using MediatR; + using Nancy.TinyIoc; using Ninject; +using Ninject.Extensions.Conventions; using Ninject.Modules; using Ninject.Planning.Bindings.Resolvers; @@ -54,22 +57,9 @@ namespace PlexRequests.UI var modules = resolver.GetModules(); var kernel = new StandardKernel(modules); - //kernel.Bind(x => x.FromThisAssembly() - // .SelectAllClasses() - // .InheritedFromAny( - // new[] - // { - // typeof(IRequestHandler<,>), - // typeof(IAsyncRequestHandler<,>), - // }) - // .BindDefaultInterfaces()); - - //kernel.Components.Add(); - //kernel.Bind(scan => scan.FromAssemblyContaining().SelectAllClasses().BindDefaultInterface()); - //kernel.Bind(scan => scan.FromAssemblyContaining().SelectAllInterfaces().BindAllInterfaces()); + kernel.Components.Add(); - //kernel.Bind().ToMethod(ctx => t => ctx.Kernel.Get(t)); - //kernel.Bind().ToMethod(ctx => t => ctx.Kernel.GetAll(t)); + app.UseNancy(options => options.Bootstrapper = new Bootstrapper(kernel)); var scheduler = new Scheduler(); diff --git a/PlexRequests.UI/app.config b/PlexRequests.UI/app.config index 1de7c9330..6cc1ad354 100644 --- a/PlexRequests.UI/app.config +++ b/PlexRequests.UI/app.config @@ -39,6 +39,14 @@ + + + + + + + + diff --git a/PlexRequests.UI/packages.config b/PlexRequests.UI/packages.config index 11c8e9abb..365e6cd83 100644 --- a/PlexRequests.UI/packages.config +++ b/PlexRequests.UI/packages.config @@ -29,8 +29,9 @@ - - + + + From 6c4b5e44c950a4bd43d883cf4f05564ce88e6f3b Mon Sep 17 00:00:00 2001 From: tidusjar Date: Wed, 29 Jun 2016 17:17:35 +0100 Subject: [PATCH 004/138] Tidy --- PlexRequests.UI/Bootstrapper.cs | 1 - PlexRequests.UI/Startup.cs | 10 +--------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/PlexRequests.UI/Bootstrapper.cs b/PlexRequests.UI/Bootstrapper.cs index 249198332..213b6c1eb 100644 --- a/PlexRequests.UI/Bootstrapper.cs +++ b/PlexRequests.UI/Bootstrapper.cs @@ -166,7 +166,6 @@ namespace PlexRequests.UI private void ConfigureContainer(IKernel container) { - var loc = ServiceLocator.Instance; loc.SetContainer(container); } diff --git a/PlexRequests.UI/Startup.cs b/PlexRequests.UI/Startup.cs index 2d0bcb221..184f2bba6 100644 --- a/PlexRequests.UI/Startup.cs +++ b/PlexRequests.UI/Startup.cs @@ -26,13 +26,7 @@ #endregion using System; -using MediatR; - -using Nancy.TinyIoc; - using Ninject; -using Ninject.Extensions.Conventions; -using Ninject.Modules; using Ninject.Planning.Bindings.Resolvers; using NLog; @@ -59,9 +53,7 @@ namespace PlexRequests.UI kernel.Components.Add(); - - - app.UseNancy(options => options.Bootstrapper = new Bootstrapper(kernel)); + app.UseNancy(options => options.Bootstrapper = new Bootstrapper(kernel)); var scheduler = new Scheduler(); scheduler.StartScheduler(); } From d7f01dcbb5c15179bed51924ad2d64bc262b1d6f Mon Sep 17 00:00:00 2001 From: tidusjar Date: Tue, 5 Jul 2016 17:29:42 +0100 Subject: [PATCH 005/138] WIP on notification resolver --- .../AuthenticationSettingsTests.cs | 5 +- .../NotificationMessageResolution.cs | 34 ++ .../NotificationMessageResolver.cs | 33 +- PlexRequests.Core/PlexRequests.Core.csproj | 1 + .../SettingModels/NotificationSettings.cs | 13 + PlexRequests.UI/Program.cs | 406 +++++++++--------- 6 files changed, 276 insertions(+), 216 deletions(-) create mode 100644 PlexRequests.Core/NotificationMessageResolution.cs diff --git a/PlexRequests.Core.Tests/AuthenticationSettingsTests.cs b/PlexRequests.Core.Tests/AuthenticationSettingsTests.cs index ba4a666da..fd7987131 100644 --- a/PlexRequests.Core.Tests/AuthenticationSettingsTests.cs +++ b/PlexRequests.Core.Tests/AuthenticationSettingsTests.cs @@ -38,12 +38,13 @@ namespace PlexRequests.Core.Tests public class NotificationMessageResolverTests { [TestCaseSource(nameof(MessageResolver))] - public string Resolve(Dictionary message, Dictionary param) + [Ignore("WIP")] + public NotificationMessageResolution Resolve(Dictionary body, Dictionary param) { var n = new NotificationMessageResolver(); var s = new NotificationSettings { - Message = message, + Message = body, CustomParamaters = param }; diff --git a/PlexRequests.Core/NotificationMessageResolution.cs b/PlexRequests.Core/NotificationMessageResolution.cs new file mode 100644 index 000000000..70979bc60 --- /dev/null +++ b/PlexRequests.Core/NotificationMessageResolution.cs @@ -0,0 +1,34 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: NotificationMessageResolution.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +namespace PlexRequests.Core +{ + public class NotificationMessageResolution + { + public string Subject { get; set; } + public string Body { get; set; } + } +} \ No newline at end of file diff --git a/PlexRequests.Core/NotificationMessageResolver.cs b/PlexRequests.Core/NotificationMessageResolver.cs index 6dd4006c8..1178ec607 100644 --- a/PlexRequests.Core/NotificationMessageResolver.cs +++ b/PlexRequests.Core/NotificationMessageResolver.cs @@ -36,30 +36,41 @@ namespace PlexRequests.Core { private const char StartChar = (char)123; private const char EndChar = (char)125; - public string ParseMessage(T notification, NotificationType type) where T : NotificationSettings + public NotificationMessageResolution ParseMessage(T notification, NotificationType type) where T : NotificationSettings { - var notificationToParse = notification.Message.FirstOrDefault(x => x.Key == type).Value; - if (string.IsNullOrEmpty(notificationToParse)) - return string.Empty; + var bodyToParse = notification.Message.FirstOrDefault(x => x.Key == type).Value; + var subjectToParse = notification.Subject.FirstOrDefault(x => x.Key == type).Value; + //if (string.IsNullOrEmpty(notificationToParse)) + // return string.Empty; - return Resolve(notificationToParse, notification.CustomParamaters); + return Resolve(bodyToParse, subjectToParse, notification.CustomParamaters); } - private string Resolve(string message, Dictionary paramaters) + private NotificationMessageResolution Resolve(string body, string subject, Dictionary paramaters) { - var fields = FindCurlyFields(message); - - foreach (var f in fields) + var bodyFields = FindCurlyFields(body); + var subjectFields = FindCurlyFields(subject); + + foreach (var f in bodyFields) { string outString; if (paramaters.TryGetValue(f, out outString)) { - message = message.Replace($"{{{f}}}", outString); + body = body.Replace($"{{{f}}}", outString); + } + } + + foreach (var s in subjectFields) + { + string outString; + if (paramaters.TryGetValue(s, out outString)) + { + subject = subject.Replace($"{{{s}}}", outString); } } - return message; + return new NotificationMessageResolution { Body = body, Subject = subject }; } private IEnumerable FindCurlyFields(string message) diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 09706dcb8..86cba7a20 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -71,6 +71,7 @@ + diff --git a/PlexRequests.Core/SettingModels/NotificationSettings.cs b/PlexRequests.Core/SettingModels/NotificationSettings.cs index 0ba781958..9eae41072 100644 --- a/PlexRequests.Core/SettingModels/NotificationSettings.cs +++ b/PlexRequests.Core/SettingModels/NotificationSettings.cs @@ -32,7 +32,20 @@ namespace PlexRequests.Core.SettingModels { public class NotificationSettings : Settings { + public NotificationSettings() + { + CustomParamaters = new Dictionary + { + {"Username", string.Empty }, + {"Date", string.Empty }, + {"Title", string.Empty }, + {"RequestType", string.Empty }, + {"Issue", string.Empty }, + + }; + } public Dictionary Message { get; set; } + public Dictionary Subject { get; set; } public Dictionary CustomParamaters { get; set; } } } \ No newline at end of file diff --git a/PlexRequests.UI/Program.cs b/PlexRequests.UI/Program.cs index 884dbcd59..d270dd382 100644 --- a/PlexRequests.UI/Program.cs +++ b/PlexRequests.UI/Program.cs @@ -1,203 +1,203 @@ -#region Copyright -// /************************************************************************ -// Copyright (c) 2016 Jamie Rees -// File: Program.cs -// Created By: Jamie Rees -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// ************************************************************************/ -#endregion -using System; - -using Microsoft.Owin.Hosting; - -using Mono.Data.Sqlite; -using Mono.Unix; -using Mono.Unix.Native; - -using NLog; -using PlexRequests.Core; -using PlexRequests.Core.SettingModels; -using PlexRequests.Helpers; -using PlexRequests.Store; -using PlexRequests.Store.Repository; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Windows.Forms; - -using CommandLine; - -using PlexRequests.UI.Start; - -namespace PlexRequests.UI -{ - class Program - { - private static Logger Log = LogManager.GetCurrentClassLogger(); - static void Main(string[] args) - { - - var result = Parser.Default.ParseArguments(args); - var baseUrl = result.MapResult( - o => o.BaseUrl, - e => string.Empty); - - var port = result.MapResult( - x => x.Port, - e => -1); - - var updated = result.MapResult(x => x.Updated, e => UpdateValue.None); - CheckUpdate(updated); - - PrintToConsole("Starting Up! Please wait, this can usually take a few seconds.", ConsoleColor.Yellow); - - Log.Trace("Getting product version"); - WriteOutVersion(); - - var s = new Setup(); - var cn = s.SetupDb(baseUrl); - s.CacheQualityProfiles(); - ConfigureTargets(cn); - SetupLogging(); - - if (port == -1 || port == 3579) - port = GetStartupPort(); - - var options = new StartOptions(Debugger.IsAttached ? $"http://localhost:{port}" : $"http://+:{port}") - { - ServerFactory = "Microsoft.Owin.Host.HttpListener" - }; - try - { - using (WebApp.Start(options)) - { - Console.WriteLine($"Plex Requests is running on the following: http://+:{port}/{baseUrl}"); - - PrintToConsole("All setup, Plex Requests is now ready!", ConsoleColor.Yellow); - if (Type.GetType("Mono.Runtime") != null) - { - Log.Info("We are on Mono!"); - - // on mono, processes will usually run as daemons - this allows you to listen - // for termination signals (ctrl+c, shutdown, etc) and finalize correctly - UnixSignal.WaitAny( - new[] { new UnixSignal(Signum.SIGINT), new UnixSignal(Signum.SIGTERM), new UnixSignal(Signum.SIGQUIT) }); - } - else - { - Log.Info("This is not Mono"); - Console.WriteLine("Press any key to exit"); - Console.ReadLine(); - } - } - } - catch (Exception e) - { - Log.Fatal(e); - Console.WriteLine(e); - throw; - } - } - - private static void WriteOutVersion() - { - var assemblyVer = AssemblyHelper.GetProductVersion(); - Log.Info($"Version: {assemblyVer}"); - Console.WriteLine($"Version: {assemblyVer}"); - } - - private static int GetStartupPort() - { - Log.Trace("Getting startup Port"); - var port = 3579; - var service = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); - var settings = service.GetSettings(); - Log.Trace("Port: {0}", settings.Port); - if (settings.Port != 0) - { - port = settings.Port; - } - - return port; - } - - private static void ConfigureTargets(string connectionString) - { - LoggingHelper.ConfigureLogging(connectionString); - } - - private static void SetupLogging() - { - var settingsService = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); - var logSettings = settingsService.GetSettings(); - - if (logSettings != null) - { - LoggingHelper.ReconfigureLogLevel(LogLevel.FromOrdinal(logSettings.Level)); - } - } - - private static void PrintToConsole(string message, ConsoleColor colour = ConsoleColor.Gray) - { - Console.ForegroundColor = colour; - Console.WriteLine(message); - Console.ForegroundColor = ConsoleColor.Gray; - } - - private static void CheckUpdate(UpdateValue val) - { - if (val == UpdateValue.Failed) - { - PrintToConsole("Update Failed", ConsoleColor.Red); - } - if (val == UpdateValue.Updated) - { - PrintToConsole("Finishing Update", ConsoleColor.Yellow); - var applicationPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath)); - var files = Directory.GetFiles(applicationPath, "PlexRequests.*", SearchOption.TopDirectoryOnly); - var oldUpdater = files.FirstOrDefault(x => x == $"{applicationPath}\\PlexRequests.Updater.exe"); - var newUpdater = files.FirstOrDefault(x => x == $"{applicationPath}\\PlexRequests.Updater.exe_Updated"); - - if (oldUpdater == null || newUpdater == null) - { - PrintToConsole("Looks like there was nothing to update.", ConsoleColor.Yellow); - return; - } - - try - { - File.Copy(oldUpdater, "PlexRequests.Updater.exe_Old", true); - File.Delete(oldUpdater); - File.Copy(newUpdater, "PlexRequests.Updater.exe", true); - File.Delete(newUpdater); - - File.Delete("PlexRequests.Updater.exe_Old"); // Cleanup - } - catch (Exception e) - { - Console.WriteLine(e.Message); - } - - PrintToConsole("Finished Update!", ConsoleColor.Yellow); - } - } - } -} +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: Program.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using System; + +using Microsoft.Owin.Hosting; + +using Mono.Data.Sqlite; +using Mono.Unix; +using Mono.Unix.Native; + +using NLog; +using PlexRequests.Core; +using PlexRequests.Core.SettingModels; +using PlexRequests.Helpers; +using PlexRequests.Store; +using PlexRequests.Store.Repository; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Windows.Forms; + +using CommandLine; + +using PlexRequests.UI.Start; + +namespace PlexRequests.UI +{ + class Program + { + private static Logger Log = LogManager.GetCurrentClassLogger(); + static void Main(string[] args) + { + + var result = Parser.Default.ParseArguments(args); + var baseUrl = result.MapResult( + o => o.BaseUrl, + e => string.Empty); + + var port = result.MapResult( + x => x.Port, + e => -1); + + var updated = result.MapResult(x => x.Updated, e => UpdateValue.None); + CheckUpdate(updated); + + PrintToConsole("Starting Up! Please wait, this can usually take a few seconds.", ConsoleColor.Yellow); + + Log.Trace("Getting product version"); + WriteOutVersion(); + + var s = new Setup(); + var cn = s.SetupDb(baseUrl); + s.CacheQualityProfiles(); + ConfigureTargets(cn); + SetupLogging(); + + if (port == -1 || port == 3579) + port = GetStartupPort(); + + var options = new StartOptions(Debugger.IsAttached ? $"http://localhost:{port}" : $"http://+:{port}") + { + ServerFactory = "Microsoft.Owin.Host.HttpListener" + }; + try + { + using (WebApp.Start(options)) + { + Console.WriteLine($"Plex Requests is running on the following: http://+:{port}/{baseUrl}"); + + PrintToConsole("All setup, Plex Requests is now ready!", ConsoleColor.Yellow); + if (Type.GetType("Mono.Runtime") != null) + { + Log.Info("We are on Mono!"); + + // on mono, processes will usually run as daemons - this allows you to listen + // for termination signals (ctrl+c, shutdown, etc) and finalize correctly + UnixSignal.WaitAny( + new[] { new UnixSignal(Signum.SIGINT), new UnixSignal(Signum.SIGTERM), new UnixSignal(Signum.SIGQUIT) }); + } + else + { + Log.Info("This is not Mono"); + Console.WriteLine("Press any key to exit"); + Console.ReadLine(); + } + } + } + catch (Exception e) + { + Log.Fatal(e); + Console.WriteLine(e); + throw; + } + } + + private static void WriteOutVersion() + { + var assemblyVer = AssemblyHelper.GetProductVersion(); + Log.Info($"Version: {assemblyVer}"); + Console.WriteLine($"Version: {assemblyVer}"); + } + + private static int GetStartupPort() + { + Log.Trace("Getting startup Port"); + var port = 3579; + var service = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); + var settings = service.GetSettings(); + Log.Trace("Port: {0}", settings.Port); + if (settings.Port != 0) + { + port = settings.Port; + } + + return port; + } + + private static void ConfigureTargets(string connectionString) + { + LoggingHelper.ConfigureLogging(connectionString); + } + + private static void SetupLogging() + { + var settingsService = new SettingsServiceV2(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider())); + var logSettings = settingsService.GetSettings(); + + if (logSettings != null) + { + LoggingHelper.ReconfigureLogLevel(LogLevel.FromOrdinal(logSettings.Level)); + } + } + + private static void PrintToConsole(string message, ConsoleColor colour = ConsoleColor.Gray) + { + Console.ForegroundColor = colour; + Console.WriteLine(message); + Console.ForegroundColor = ConsoleColor.Gray; + } + + private static void CheckUpdate(UpdateValue val) + { + if (val == UpdateValue.Failed) + { + PrintToConsole("Update Failed", ConsoleColor.Red); + } + if (val == UpdateValue.Updated) + { + PrintToConsole("Finishing Update", ConsoleColor.Yellow); + var applicationPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath)); + var files = Directory.GetFiles(applicationPath, "PlexRequests.*", SearchOption.TopDirectoryOnly); + var oldUpdater = files.FirstOrDefault(x => x == $"{applicationPath}\\PlexRequests.Updater.exe"); + var newUpdater = files.FirstOrDefault(x => x == $"{applicationPath}\\PlexRequests.Updater.exe_Updated"); + + if (oldUpdater == null || newUpdater == null) + { + PrintToConsole("Looks like there was nothing to update.", ConsoleColor.Yellow); + return; + } + + try + { + File.Copy(oldUpdater, "PlexRequests.Updater.exe_Old", true); + File.Delete(oldUpdater); + File.Copy(newUpdater, "PlexRequests.Updater.exe", true); + File.Delete(newUpdater); + + File.Delete("PlexRequests.Updater.exe_Old"); // Cleanup + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + + PrintToConsole("Finished Update!", ConsoleColor.Yellow); + } + } + } +} From e82fad7428cc1e4d1cbdb34ccbb2225f85b9dfcc Mon Sep 17 00:00:00 2001 From: tidusjar Date: Wed, 6 Jul 2016 08:53:32 +0100 Subject: [PATCH 006/138] Trycatch around the availbility checker --- PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs b/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs index dfadadd80..6f026aed4 100644 --- a/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs +++ b/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs @@ -420,7 +420,14 @@ namespace PlexRequests.Services.Jobs public void Execute(IJobExecutionContext context) { - CheckAndUpdateAll(); + try + { + CheckAndUpdateAll(); + } + catch (Exception e) + { + Log.Error(e); + } } } } \ No newline at end of file From a01e80c600235730e0572e4759828ab330c1be14 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Wed, 6 Jul 2016 09:53:29 +0100 Subject: [PATCH 007/138] Fixed #409 --- PlexRequests.UI/Modules/SearchModule.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 79bbd9d7d..d1bd4ce85 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -910,7 +910,7 @@ namespace PlexRequests.UI.Modules model.Approved = true; await RequestService.AddRequestAsync(model); - if (ShouldSendNotification(RequestType.Movie, settings)) + if (ShouldSendNotification(model.Type, settings)) { var notificationModel = new NotificationModel { @@ -918,7 +918,7 @@ namespace PlexRequests.UI.Modules User = Username, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest, - RequestType = RequestType.Movie + RequestType = model.Type }; await NotificationService.Publish(notificationModel); } From d8111bcedb10e3f8676911c292d9a28e52fb0618 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Wed, 6 Jul 2016 10:03:43 +0100 Subject: [PATCH 008/138] Changed the way the donate button works for #414 --- PlexRequests.UI/Views/Shared/_Layout.cshtml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/PlexRequests.UI/Views/Shared/_Layout.cshtml b/PlexRequests.UI/Views/Shared/_Layout.cshtml index 3cc50a93b..80e11ac31 100644 --- a/PlexRequests.UI/Views/Shared/_Layout.cshtml +++ b/PlexRequests.UI/Views/Shared/_Layout.cshtml @@ -43,7 +43,7 @@ @Html.GetNavbarUrl(Context, "/issues", UI.Layout_Issues, "exclamation", "") @if (Context.CurrentUser.IsAuthenticated()) // TODO replace with IsAdmin { -
  • +
  • }