From 379ffeddbad14e67ef2bf12c288073bb69e6b62c Mon Sep 17 00:00:00 2001 From: tidusjar Date: Tue, 5 Nov 2019 22:04:37 +0000 Subject: [PATCH] Updated to .net core 3.0 --- src/Ombi.Core.Tests/Ombi.Core.Tests.csproj | 4 +- src/Ombi.DependencyInjection/IocExtensions.cs | 4 +- .../Ombi.Helpers.Tests.csproj | 2 +- .../Ombi.Notifications.Tests.csproj | 2 +- .../Ombi.Schedule.Tests.csproj | 4 +- src/Ombi.Schedule/OmbiScheduler.cs | 6 +- .../Ombi.Settings.Tests.csproj | 5 +- src/Ombi.Tests/Ombi.Tests.csproj | 2 +- src/Ombi/Ombi.csproj | 13 ++-- src/Ombi/Program.cs | 20 +++-- src/Ombi/Startup.cs | 77 ++++++------------- src/Ombi/StartupExtensions.cs | 62 +++------------ src/Ombi/SwaggerOperationFilter.cs | 28 ------- 13 files changed, 66 insertions(+), 163 deletions(-) delete mode 100644 src/Ombi/SwaggerOperationFilter.cs diff --git a/src/Ombi.Core.Tests/Ombi.Core.Tests.csproj b/src/Ombi.Core.Tests/Ombi.Core.Tests.csproj index ed43d4991..89ff21a7c 100644 --- a/src/Ombi.Core.Tests/Ombi.Core.Tests.csproj +++ b/src/Ombi.Core.Tests/Ombi.Core.Tests.csproj @@ -1,7 +1,7 @@  - - netcoreapp2.2 + + netcoreapp3.0 diff --git a/src/Ombi.DependencyInjection/IocExtensions.cs b/src/Ombi.DependencyInjection/IocExtensions.cs index a5b3935bb..740e59d4f 100644 --- a/src/Ombi.DependencyInjection/IocExtensions.cs +++ b/src/Ombi.DependencyInjection/IocExtensions.cs @@ -120,8 +120,8 @@ namespace Ombi.DependencyInjection public static void RegisterApi(this IServiceCollection services) { - services.AddSingleton(); - services.AddSingleton(); // https://blogs.msdn.microsoft.com/alazarev/2017/12/29/disposable-finalizers-and-httpclient/ + services.AddScoped(); + services.AddScoped(); // https://blogs.msdn.microsoft.com/alazarev/2017/12/29/disposable-finalizers-and-httpclient/ services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/src/Ombi.Helpers.Tests/Ombi.Helpers.Tests.csproj b/src/Ombi.Helpers.Tests/Ombi.Helpers.Tests.csproj index 635186dbf..45f2f537e 100644 --- a/src/Ombi.Helpers.Tests/Ombi.Helpers.Tests.csproj +++ b/src/Ombi.Helpers.Tests/Ombi.Helpers.Tests.csproj @@ -1,7 +1,7 @@ - netcoreapp2.2 + netcoreapp3.0 false diff --git a/src/Ombi.Notifications.Tests/Ombi.Notifications.Tests.csproj b/src/Ombi.Notifications.Tests/Ombi.Notifications.Tests.csproj index d5db2cf26..8ce8ab43b 100644 --- a/src/Ombi.Notifications.Tests/Ombi.Notifications.Tests.csproj +++ b/src/Ombi.Notifications.Tests/Ombi.Notifications.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.2 + netcoreapp3.0 diff --git a/src/Ombi.Schedule.Tests/Ombi.Schedule.Tests.csproj b/src/Ombi.Schedule.Tests/Ombi.Schedule.Tests.csproj index 56acb9137..70a456b6a 100644 --- a/src/Ombi.Schedule.Tests/Ombi.Schedule.Tests.csproj +++ b/src/Ombi.Schedule.Tests/Ombi.Schedule.Tests.csproj @@ -1,7 +1,7 @@ - + - netcoreapp2.2 + netcoreapp3.0 diff --git a/src/Ombi.Schedule/OmbiScheduler.cs b/src/Ombi.Schedule/OmbiScheduler.cs index e1d136849..75305b84f 100644 --- a/src/Ombi.Schedule/OmbiScheduler.cs +++ b/src/Ombi.Schedule/OmbiScheduler.cs @@ -39,11 +39,11 @@ namespace Ombi.Schedule // .Build(); //} - public static async Task UseQuartz(this IApplicationBuilder app) + public static async Task UseQuartz(this IServiceProvider app) { // Job Factory through IOC container - var jobFactory = (IJobFactory)app.ApplicationServices.GetService(typeof(IJobFactory)); - var service = (ISettingsService)app.ApplicationServices.GetService(typeof(ISettingsService)); + var jobFactory = (IJobFactory)app.GetService(typeof(IJobFactory)); + var service = (ISettingsService)app.GetService(typeof(ISettingsService)); var s = service.GetSettings(); // Set job factory OmbiQuartz.Instance.UseJobFactory(jobFactory); diff --git a/src/Ombi.Settings.Tests/Ombi.Settings.Tests.csproj b/src/Ombi.Settings.Tests/Ombi.Settings.Tests.csproj index db214be34..ad99af008 100644 --- a/src/Ombi.Settings.Tests/Ombi.Settings.Tests.csproj +++ b/src/Ombi.Settings.Tests/Ombi.Settings.Tests.csproj @@ -1,7 +1,6 @@ - + - - netcoreapp2.2 + netcoreapp3.0 false diff --git a/src/Ombi.Tests/Ombi.Tests.csproj b/src/Ombi.Tests/Ombi.Tests.csproj index f8fd0bf24..a2ac1aac6 100644 --- a/src/Ombi.Tests/Ombi.Tests.csproj +++ b/src/Ombi.Tests/Ombi.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.2 + netcoreapp3.0 false diff --git a/src/Ombi/Ombi.csproj b/src/Ombi/Ombi.csproj index 6122a657e..9c17d6bdd 100644 --- a/src/Ombi/Ombi.csproj +++ b/src/Ombi/Ombi.csproj @@ -1,6 +1,6 @@  - netcoreapp2.2 + netcoreapp3.0 win10-x64;win10-x86;osx-x64;linux-x64;linux-arm;linux-arm64; false Latest @@ -58,14 +58,8 @@ - - - - - - @@ -75,8 +69,11 @@ - + + + + diff --git a/src/Ombi/Program.cs b/src/Ombi/Program.cs index dd0baa8d4..f7ff8e023 100644 --- a/src/Ombi/Program.cs +++ b/src/Ombi/Program.cs @@ -9,6 +9,7 @@ using CommandLine; using CommandLine.Text; using Microsoft.AspNetCore; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Hosting; using Ombi.Helpers; namespace Ombi @@ -115,7 +116,7 @@ namespace Ombi Console.WriteLine($"We are running on {urlValue}"); - CreateWebHostBuilder(args).Build().Run(); + CreateHostBuilder(args).Build().Run(); } private static void DeleteSchedules() @@ -283,11 +284,18 @@ namespace Ombi } } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup() - .UseUrls(UrlArgs) - .PreferHostingUrls(true); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.ConfigureKestrel(serverOptions => + { + // Set properties and call methods on options + }); + webBuilder.PreferHostingUrls(true) + .UseUrls(UrlArgs) + .UseStartup(); + }); private static string HelpOutput(ParserResult args) { diff --git a/src/Ombi/Startup.cs b/src/Ombi/Startup.cs index 7f55c5a74..d61664e42 100644 --- a/src/Ombi/Startup.cs +++ b/src/Ombi/Startup.cs @@ -3,21 +3,17 @@ using AutoMapper.EquivalencyExpression; using Hangfire; using Hangfire.Dashboard; using Hangfire.MemoryStorage; -using Hangfire.SQLite; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.SpaServices; -using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer; using Microsoft.AspNetCore.StaticFiles; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; using Ombi.Core.Authentication; using Ombi.Core.Settings; using Ombi.DependencyInjection; @@ -33,6 +29,8 @@ using Serilog; using SQLitePCL; using System; using System.IO; +using Microsoft.Extensions.Hosting; +using Newtonsoft.Json; using ILogger = Serilog.ILogger; namespace Ombi @@ -41,7 +39,7 @@ namespace Ombi { public static StoragePathSingleton StoragePath => StoragePathSingleton.Instance; - public Startup(IHostingEnvironment env) + public Startup(IWebHostEnvironment env) { Console.WriteLine(env.ContentRootPath); var builder = new ConfigurationBuilder() @@ -62,7 +60,7 @@ namespace Ombi public IConfigurationRoot Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. - public IServiceProvider ConfigureServices(IServiceCollection services) + public void ConfigureServices(IServiceCollection services) { services.AddIdentity() .AddEntityFrameworkStores() @@ -86,8 +84,7 @@ namespace Ombi services.AddJwtAuthentication(Configuration); services.AddMvc() - .AddJsonOptions(x => - x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); + .AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore); services.AddOmbiMappingProfile(); services.AddAutoMapper(expression => { expression.AddCollectionMappers(); }); @@ -99,7 +96,9 @@ namespace Ombi services.AddHangfire(x => { x.UseMemoryStorage(); +#pragma warning disable ASP0000 // Do not call 'IServiceCollection.BuildServiceProvider' in 'ConfigureServices' x.UseActivator(new IoCJobActivator(services.BuildServiceProvider())); +#pragma warning restore ASP0000 // Do not call 'IServiceCollection.BuildServiceProvider' in 'ConfigureServices' }); SQLitePCL.raw.sqlite3_config(raw.SQLITE_CONFIG_MULTITHREAD); @@ -112,38 +111,32 @@ namespace Ombi .AllowCredentials(); })); - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest); services.AddSignalR(); services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; }); - // Build the intermediate service provider - return services.BuildServiceProvider(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, - IMemoryCache cache, IServiceProvider serviceProvider) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider) { app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); - app.UseQuartz().GetAwaiter().GetResult(); + serviceProvider.UseQuartz().GetAwaiter().GetResult(); var ctx = serviceProvider.GetService(); loggerFactory.AddSerilog(); - - app.UseHealthChecks("/health"); - + app.UseSpaStaticFiles(); - - + var ombiService = - app.ApplicationServices.GetService>(); + serviceProvider.GetService>(); var settings = ombiService.GetSettings(); if (settings.ApiKey.IsNullOrEmpty()) { @@ -180,20 +173,7 @@ namespace Ombi app.UseHangfireServer(new BackgroundJobServerOptions {WorkerCount = 1, ServerTimeout = TimeSpan.FromDays(1), ShutdownTimeout = TimeSpan.FromDays(1)}); - if (env.IsDevelopment()) - { - app.UseHangfireDashboard(settings.BaseUrl.HasValue() ? $"{settings.BaseUrl}/hangfire" : "/hangfire", - new DashboardOptions - { - Authorization = new[] {new HangfireAuthorizationFilter()} - }); - } - - GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute {Attempts = 3}); - // Setup the scheduler - //var jobSetup = app.ApplicationServices.GetService(); - //jobSetup.Setup(); ctx.Seed(); var settingsctx = serviceProvider.GetService(); var externalctx = serviceProvider.GetService(); @@ -207,7 +187,10 @@ namespace Ombi ContentTypeProvider = provider, }); + app.UseRouting(); + app.UseAuthentication(); + app.UseAuthorization(); app.UseMiddleware(); app.UseMiddleware(); @@ -226,9 +209,12 @@ namespace Ombi } }); - app.UseSignalR(routes => { routes.MapHub("/hubs/notification"); }); - - app.UseMvcWithDefaultRoute(); + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + endpoints.MapHub("/hubs/notification"); + endpoints.MapHealthChecks("/health"); + }); app.UseSpa(spa => { @@ -240,24 +226,5 @@ namespace Ombi }); } - - private static bool IsSpaRoute(HttpContext context) - { - var path = context.Request.Path; - // This should probably be a compiled regex - return path.StartsWithSegments("/static") - || path.StartsWithSegments("/sockjs-node") - || path.StartsWithSegments("/socket.io") - || path.ToString().Contains(".hot-update."); - } - - public class HangfireAuthorizationFilter : IDashboardAuthorizationFilter - { - public bool Authorize(DashboardContext context) - { - return true; - } - } - } } diff --git a/src/Ombi/StartupExtensions.cs b/src/Ombi/StartupExtensions.cs index c127239ff..c493a3ccf 100644 --- a/src/Ombi/StartupExtensions.cs +++ b/src/Ombi/StartupExtensions.cs @@ -5,11 +5,13 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.JsonPatch.Operations; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; +using Microsoft.OpenApi.Models; using Ombi.Config; using Ombi.Helpers; using Ombi.Models.Identity; @@ -18,65 +20,34 @@ using Swashbuckle.AspNetCore.SwaggerGen; namespace Ombi { - public class AddRequiredHeaderParameter : IOperationFilter - { - - public void Apply(Operation operation, OperationFilterContext context) - { - if (operation.Parameters == null) - operation.Parameters = new List(); - - operation.Parameters.Add(new NonBodyParameter - { - Name = "ApiKey", - In = "header", - Type = "apiKey", - - }); - } - } - public static class StartupExtensions + public static class StartupExtensions { public static void AddSwagger(this IServiceCollection services) { services.AddSwaggerGen(c => { - c.DescribeAllEnumsAsStrings(); - c.SwaggerDoc("v1", new Info + c.SwaggerDoc("v1", new OpenApiInfo() { Version = "v1", Title = "Ombi Api V1", - Contact = new Contact + Contact = new OpenApiContact { Name = "Jamie Rees", - Url = "https://www.ombi.io/" + Url = new Uri("https://www.ombi.io/") } }); - var security = new Dictionary> - { - //{"Bearer", new string[] { }}, - {"ApiKey", new string[] { }}, - }; - //c.AddSecurityDefinition("Bearer", new ApiKeyScheme - //{ - // Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"", - // Name = "Authorization", - // In = "header", - // Type = "apiKey" - //}); - c.AddSecurityDefinition("ApiKey", new ApiKeyScheme + + c.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme { Description = "API Key provided by Ombi. Example: \"ApiKey: {token}\"", Name = "ApiKey", - In = "header", - Type = "apiKey" + In = ParameterLocation.Header, + Type = SecuritySchemeType.ApiKey }); - c.AddSecurityRequirement(security); c.CustomSchemaIds(x => x.FullName); - c.OperationFilter(); var basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); var xmlPath = Path.Combine(basePath, "Swagger.xml"); try @@ -89,22 +60,11 @@ namespace Ombi } - c.OperationFilter(); c.DescribeAllParametersInCamelCase(); }); } - public class JwtBearer : SecurityScheme - { - public string Name { get; set; } - - public string In { get; set; } - - public JwtBearer() - { - this.Type = "bearer"; - } - } + public static void AddAppSettingsValues(this IServiceCollection services, IConfigurationRoot configuration) { diff --git a/src/Ombi/SwaggerOperationFilter.cs b/src/Ombi/SwaggerOperationFilter.cs deleted file mode 100644 index 82eb97fdb..000000000 --- a/src/Ombi/SwaggerOperationFilter.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Reflection.Metadata; -using Microsoft.AspNetCore.Mvc.ApiExplorer; -using Microsoft.AspNetCore.Mvc.Authorization; -using Swashbuckle.AspNetCore.Swagger; -using Swashbuckle.AspNetCore.SwaggerGen; - -namespace Ombi -{ - public class SwaggerOperationFilter : IOperationFilter - { - public string Name { get; private set; } - - public SwaggerOperationFilter() - { - Name = "Authorization"; - } - - public void Apply(Operation operation, OperationFilterContext context) - { - if (operation.Parameters == null) - operation.Parameters = new List(); - var tokenAuthDict = new Dictionary> {{Name, new List()}}; - operation.Security = new IDictionary>[] { tokenAuthDict }; - } - } -}