From d9ab803a11297871999f666a24c8313a258cc957 Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Tue, 3 Oct 2017 14:51:15 +0100 Subject: [PATCH] #1513 Added storage path --- src/Ombi.DependencyInjection/IocExtensions.cs | 7 +++-- src/Ombi.Helpers/StoragePathSingleton.cs | 13 +++++++++ src/Ombi.Store/Context/OmbiContext.cs | 8 ++--- src/Ombi/Program.cs | 4 +++ src/Ombi/Startup.cs | 29 +++++++++++++++---- 5 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 src/Ombi.Helpers/StoragePathSingleton.cs diff --git a/src/Ombi.DependencyInjection/IocExtensions.cs b/src/Ombi.DependencyInjection/IocExtensions.cs index 1f3458b85..02eae541b 100644 --- a/src/Ombi.DependencyInjection/IocExtensions.cs +++ b/src/Ombi.DependencyInjection/IocExtensions.cs @@ -1,9 +1,11 @@ using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Security.Principal; using Hangfire; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.DependencyInjection; @@ -99,10 +101,9 @@ namespace Ombi.DependencyInjection services.AddTransient(); } - public static void RegisterStore(this IServiceCollection services) - { + public static void RegisterStore(this IServiceCollection services) { services.AddEntityFrameworkSqlite().AddDbContext(); - + services.AddScoped(); // https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6 services.AddTransient(); services.AddTransient(); diff --git a/src/Ombi.Helpers/StoragePathSingleton.cs b/src/Ombi.Helpers/StoragePathSingleton.cs new file mode 100644 index 000000000..c502c4a54 --- /dev/null +++ b/src/Ombi.Helpers/StoragePathSingleton.cs @@ -0,0 +1,13 @@ +namespace Ombi.Helpers +{ + public class StoragePathSingleton + { + private static StoragePathSingleton instance; + + private StoragePathSingleton() { } + + public static StoragePathSingleton Instance => instance ?? (instance = new StoragePathSingleton()); + + public string StoragePath { get; set; } + } +} \ No newline at end of file diff --git a/src/Ombi.Store/Context/OmbiContext.cs b/src/Ombi.Store/Context/OmbiContext.cs index ebbab4b21..138d6806c 100644 --- a/src/Ombi.Store/Context/OmbiContext.cs +++ b/src/Ombi.Store/Context/OmbiContext.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; @@ -14,12 +15,10 @@ namespace Ombi.Store.Context public OmbiContext() { if (_created) return; - + _created = true; Database.Migrate(); - // Add the notifcation templates - } public DbSet NotificationTemplates { get; set; } @@ -44,7 +43,8 @@ namespace Ombi.Store.Context protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.UseSqlite("Data Source=Ombi.db"); + var i = StoragePathSingleton.Instance; + optionsBuilder.UseSqlite($"Data Source={Path.Combine(i.StoragePath,"Ombi.db")}"); } protected override void OnModelCreating(ModelBuilder builder) diff --git a/src/Ombi/Program.cs b/src/Ombi/Program.cs index 2567d4c78..f5e84e554 100644 --- a/src/Ombi/Program.cs +++ b/src/Ombi/Program.cs @@ -9,6 +9,8 @@ using Ombi.Store.Entities; using CommandLine; using CommandLine.Text; using Microsoft.AspNetCore; +using Microsoft.EntityFrameworkCore; +using Ombi.Helpers; namespace Ombi { @@ -33,6 +35,8 @@ namespace Ombi UrlArgs = host; var urlValue = string.Empty; + var instance = StoragePathSingleton.Instance; + instance.StoragePath = storagePath ?? string.Empty; using (var ctx = new OmbiContext()) { var config = ctx.ApplicationConfigurations.ToList(); diff --git a/src/Ombi/Startup.cs b/src/Ombi/Startup.cs index 0c04a23ec..72134c286 100644 --- a/src/Ombi/Startup.cs +++ b/src/Ombi/Startup.cs @@ -16,6 +16,7 @@ using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.SpaServices.Webpack; using Microsoft.AspNetCore.StaticFiles; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -36,6 +37,7 @@ namespace Ombi { public class Startup { + public static StoragePathSingleton StoragePath => StoragePathSingleton.Instance; public Startup(IHostingEnvironment env) { Console.WriteLine(env.ContentRootPath); @@ -48,11 +50,24 @@ namespace Ombi //if (env.IsDevelopment()) //{ - Log.Logger = new LoggerConfiguration() - .MinimumLevel.Debug() - .WriteTo.RollingFile(Path.Combine(env.ContentRootPath, "Logs", "log-{Date}.txt")) - .WriteTo.SQLite("Ombi.db", "Logs", LogEventLevel.Debug) - .CreateLogger(); + Serilog.ILogger config; + if (string.IsNullOrEmpty(StoragePath.StoragePath)) + { + config = new LoggerConfiguration() + .MinimumLevel.Debug() + .WriteTo.SQLite("Ombi.db", "Logs", LogEventLevel.Debug) + .CreateLogger(); + } + else + { + config = new LoggerConfiguration() + .MinimumLevel.Debug() + .WriteTo.SQLite(Path.Combine(StoragePath.StoragePath, "Ombi.db"), "Logs", LogEventLevel.Debug) + .CreateLogger(); + } + Log.Logger = config; + + //} //if (env.IsProduction()) //{ @@ -69,9 +84,10 @@ namespace Ombi // This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { + // Add framework services. services.AddDbContext(); - + services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders() @@ -113,6 +129,7 @@ namespace Ombi x.UseConsole(); }); + // Build the intermediate service provider return services.BuildServiceProvider(); }