From 51116cc1c198e9853860d168d2e5f96ec0edd263 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 6 Sep 2022 22:01:14 -0500 Subject: add DataDirectory, minimize magic strings --- src/slskd/Program.cs | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 541f0f00..371f60bd 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -45,7 +45,6 @@ namespace slskd using Microsoft.Extensions.FileProviders.Physical; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; - using Prometheus; using Prometheus.DotNetRuntime; using Prometheus.SystemMetrics; using Serilog; @@ -160,6 +159,16 @@ namespace slskd [EnvironmentVariable("CONFIG")] public static string ConfigurationFile { get; private set; } = null; + /// + /// Gets the connection strings for application databases. + /// + public static ConnectionStrings ConnectionStrings { get; private set; } = null; + + /// + /// Gets the path where persistent data is saved. + /// + public static string DataDirectory { get; private set; } = null; + /// /// Gets the default fully qualified path to the configuration file. /// @@ -247,6 +256,15 @@ namespace slskd // and defaults that are dependent upon it AppDirectory ??= DefaultAppDirectory; + DataDirectory = Path.Combine(AppDirectory, "data"); + + ConnectionStrings = new() + { + Search = $"Data Source={Path.Combine(DataDirectory, "search.db")};Cache=shared;Pooling=True;", + Transfers = $"Data Source={Path.Combine(DataDirectory, "transfers.db")};Cache=shared;Pooling=True;", + Shares = $"Data Source={Path.Combine(DataDirectory, "shares.db")};Cache=shared", + }; + DefaultConfigurationFile = Path.Combine(AppDirectory, $"{AppName}.yml"); DefaultDownloadsDirectory = Path.Combine(AppDirectory, "downloads"); DefaultIncompleteDirectory = Path.Combine(AppDirectory, "incomplete"); @@ -260,7 +278,7 @@ namespace slskd try { VerifyDirectory(AppDirectory, createIfMissing: true, verifyWriteable: true); - VerifyDirectory(Path.Combine(AppDirectory, "data"), createIfMissing: true, verifyWriteable: true); + VerifyDirectory(DataDirectory, createIfMissing: true, verifyWriteable: true); VerifyDirectory(DefaultDownloadsDirectory, createIfMissing: true, verifyWriteable: true); VerifyDirectory(DefaultIncompleteDirectory, createIfMissing: true, verifyWriteable: true); } @@ -449,8 +467,8 @@ namespace slskd ConfigureSQLite(); - services.AddDbContext("search.db"); - services.AddDbContext("transfers.db"); + services.AddDbContext(ConnectionStrings.Search); + services.AddDbContext(ConnectionStrings.Transfers); services.AddSingleton(); services.AddSingleton(); @@ -512,7 +530,7 @@ namespace slskd using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting(); services.AddDataProtection() - .PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(AppDirectory, "data", ".DataProtection-Keys"))); + .PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(DataDirectory, ".DataProtection-Keys"))); var jwtSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(OptionsAtStartup.Web.Authentication.Jwt.Key)); @@ -838,7 +856,7 @@ namespace slskd commandLine: Environment.CommandLine); } - private static IServiceCollection AddDbContext(this IServiceCollection services, string filename) + private static IServiceCollection AddDbContext(this IServiceCollection services, string connectionString) where T : DbContext { Log.Debug("Initializing database context {Name}", typeof(T).Name); @@ -847,7 +865,7 @@ namespace slskd { services.AddDbContextFactory(options => { - options.UseSqlite($"Data Source={Path.Combine(AppDirectory, "data", filename)};Cache=shared;Pooling=True;"); + options.UseSqlite(connectionString); if (OptionsAtStartup.Debug && OptionsAtStartup.Flags.LogSQL) { -- cgit v1.2.3 From 8b1e4a50079afd5e7b37b264e7b2f72135cd9530 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 7 Sep 2022 22:21:41 -0500 Subject: use Filenames --- src/slskd/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 371f60bd..f4e59456 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -260,9 +260,9 @@ namespace slskd ConnectionStrings = new() { - Search = $"Data Source={Path.Combine(DataDirectory, "search.db")};Cache=shared;Pooling=True;", - Transfers = $"Data Source={Path.Combine(DataDirectory, "transfers.db")};Cache=shared;Pooling=True;", - Shares = $"Data Source={Path.Combine(DataDirectory, "shares.db")};Cache=shared", + Search = $"Data Source={Path.Combine(DataDirectory, Filenames.SearchDb)};Cache=shared;Pooling=True;", + Transfers = $"Data Source={Path.Combine(DataDirectory, Filenames.TransfersDb)};Cache=shared;Pooling=True;", + Shares = $"Data Source={Path.Combine(DataDirectory, Filenames.SharesDb)};Cache=shared", }; DefaultConfigurationFile = Path.Combine(AppDirectory, $"{AppName}.yml"); -- cgit v1.2.3 From 1cc28437711fdca0643f01191f84325e3b2359cb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 10 Sep 2022 17:41:06 -0500 Subject: hard code filenames, make SQLite init more prominent --- src/slskd/Program.cs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index f4e59456..5a5d9ce9 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -252,17 +252,31 @@ namespace slskd return; } + InitSQLiteOrFailFast(); + // the application isn't being run in command mode. derive the application directory value // and defaults that are dependent upon it AppDirectory ??= DefaultAppDirectory; - DataDirectory = Path.Combine(AppDirectory, "data"); + // configure connection strings and configure SQLite + string shareDbDataSource = default; + + if (OptionsAtStartup.Shares.Cache.StorageMode.ToEnum() == StorageMode.Disk) + { + shareDbDataSource = Path.Combine(DataDirectory, "shares.db"); + } + else + { + shareDbDataSource = "file:shares?mode=memory"; + } + ConnectionStrings = new() { - Search = $"Data Source={Path.Combine(DataDirectory, Filenames.SearchDb)};Cache=shared;Pooling=True;", - Transfers = $"Data Source={Path.Combine(DataDirectory, Filenames.TransfersDb)};Cache=shared;Pooling=True;", - Shares = $"Data Source={Path.Combine(DataDirectory, Filenames.SharesDb)};Cache=shared", + Search = $"Data Source={Path.Combine(DataDirectory, "search.db")};Cache=shared;Pooling=True;", + Transfers = $"Data Source={Path.Combine(DataDirectory, "transfers.db")};Cache=shared;Pooling=True;", + Shares = $"Data Source={shareDbDataSource};Cache=shared", + SharesBackup = $"Data Source={Path.Combine(DataDirectory, "shares.db.bak")};", }; DefaultConfigurationFile = Path.Combine(AppDirectory, $"{AppName}.yml"); @@ -465,8 +479,6 @@ namespace slskd services.AddSingleton(); - ConfigureSQLite(); - services.AddDbContext(ConnectionStrings.Search); services.AddDbContext(ConnectionStrings.Transfers); @@ -492,7 +504,7 @@ namespace slskd return services; } - private static void ConfigureSQLite() + private static void InitSQLiteOrFailFast() { // initialize // avoids: System.Exception: You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init(). -- cgit v1.2.3 From 2a0db7e44a0b25d2a505ae8472de214c6c68b5d2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 11 Sep 2022 12:02:17 -0500 Subject: move connection string setup --- src/slskd/Program.cs | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 5a5d9ce9..fe2bb626 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -252,33 +252,11 @@ namespace slskd return; } - InitSQLiteOrFailFast(); - // the application isn't being run in command mode. derive the application directory value // and defaults that are dependent upon it AppDirectory ??= DefaultAppDirectory; DataDirectory = Path.Combine(AppDirectory, "data"); - // configure connection strings and configure SQLite - string shareDbDataSource = default; - - if (OptionsAtStartup.Shares.Cache.StorageMode.ToEnum() == StorageMode.Disk) - { - shareDbDataSource = Path.Combine(DataDirectory, "shares.db"); - } - else - { - shareDbDataSource = "file:shares?mode=memory"; - } - - ConnectionStrings = new() - { - Search = $"Data Source={Path.Combine(DataDirectory, "search.db")};Cache=shared;Pooling=True;", - Transfers = $"Data Source={Path.Combine(DataDirectory, "transfers.db")};Cache=shared;Pooling=True;", - Shares = $"Data Source={shareDbDataSource};Cache=shared", - SharesBackup = $"Data Source={Path.Combine(DataDirectory, "shares.db.bak")};", - }; - DefaultConfigurationFile = Path.Combine(AppDirectory, $"{AppName}.yml"); DefaultDownloadsDirectory = Path.Combine(AppDirectory, "downloads"); DefaultIncompleteDirectory = Path.Combine(AppDirectory, "incomplete"); @@ -355,11 +333,38 @@ namespace slskd Log.Information("Process ID: {ProcessId}", ProcessId); Log.Information("Instance Name: {InstanceName}", OptionsAtStartup.InstanceName); + // SQLite must have specific capabilities to function properly. this shouldn't be a concern for shrinkwrapped + // binaries or in Docker, but if someone builds from source weird things can happen. + InitSQLiteOrFailFast(); + Log.Information("Using application directory {AppDirectory}", AppDirectory); Log.Information("Using configuration file {ConfigurationFile}", ConfigurationFile); + Log.Information("Storing application data in {DataDirectory}", DataDirectory); RecreateConfigurationFileIfMissing(ConfigurationFile); + // configure connection strings and configure SQLite + string shareDbDataSource = default; + + if (OptionsAtStartup.Shares.Cache.StorageMode.ToEnum() == StorageMode.Disk) + { + Log.Information("Using on-disk shared file database"); + shareDbDataSource = Path.Combine(DataDirectory, "shares.db"); + } + else + { + Log.Information("Using in-memory shared file database"); + shareDbDataSource = "file:shares?mode=memory"; + } + + ConnectionStrings = new() + { + Search = $"Data Source={Path.Combine(DataDirectory, "search.db")};Cache=shared;Pooling=True;", + Transfers = $"Data Source={Path.Combine(DataDirectory, "transfers.db")};Cache=shared;Pooling=True;", + Shares = $"Data Source={shareDbDataSource};Cache=shared", + SharesBackup = $"Data Source={Path.Combine(DataDirectory, "shares.db.bak")};", + }; + if (!string.IsNullOrEmpty(OptionsAtStartup.Logger.Loki)) { Log.Information("Forwarding logs to Grafana Loki instance at {LoggerLokiUrl}", OptionsAtStartup.Logger.Loki); -- cgit v1.2.3 From a596dfe252ef53e41b91ec6773aec93307c41f1e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 11 Sep 2022 18:09:58 -0500 Subject: tweak logging for consistency --- src/slskd/Application.cs | 2 +- src/slskd/Program.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 288ba07b..cab2b649 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1032,7 +1032,7 @@ namespace slskd { // the share transitioned into ready without completing a scan; it was loaded from disk State.SetValue(state => state with { Shares = state.Shares with { ScanPending = false } }); - Log.Information("Shares loaded from disk successfully. Sharing {Directories} directories and {Files} files", current.Directories, current.Files); + Log.Information("Share cache loaded from disk successfully. Sharing {Directories} directories and {Files} files", current.Directories, current.Files); _ = CacheBrowseResponse(); } else diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index fe2bb626..e828f242 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -348,12 +348,12 @@ namespace slskd if (OptionsAtStartup.Shares.Cache.StorageMode.ToEnum() == StorageMode.Disk) { - Log.Information("Using on-disk shared file database"); + Log.Information("Using on-disk shared file cache"); shareDbDataSource = Path.Combine(DataDirectory, "shares.db"); } else { - Log.Information("Using in-memory shared file database"); + Log.Information("Using in-memory shared file cache"); shareDbDataSource = "file:shares?mode=memory"; } -- cgit v1.2.3