From 10194066db31b736d60f1e156af3c2f25f5dce5a Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Mon, 25 Dec 2023 18:46:05 +0100 Subject: [PATCH] Allow custom data folder --- Resources/config_example.json | 1 + Sources/App/Management/Configuration.swift | 14 ++++++++++++++ Sources/App/configure.swift | 8 +++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Resources/config_example.json b/Resources/config_example.json index 337d9f3..51b6d13 100644 --- a/Resources/config_example.json +++ b/Resources/config_example.json @@ -8,4 +8,5 @@ "tokenExpiryDuration": 15, }, "monitoringTokens": [], + "dataDirectory" : "/data/schafkopf" } diff --git a/Sources/App/Management/Configuration.swift b/Sources/App/Management/Configuration.swift index fed8442..0bef2f8 100644 --- a/Sources/App/Management/Configuration.swift +++ b/Sources/App/Management/Configuration.swift @@ -23,6 +23,13 @@ struct Configuration { /// The number of minutes until a password reset token is no longer valid let tokenExpiryDuration: Int } + + /** + The folder where the data should be stored. + + If the folder is set to `nil`, then the `Resources` folder is used. + */ + let dataDirectory: String? /// The authentication tokens to access the metrics let monitoringTokens: Set @@ -42,6 +49,13 @@ struct Configuration { } return resourcesDirectory.appendingPathComponent(logPath) } + + func customDataDirectory(or publicDirectory: String) -> URL { + guard let dataDirectory else { + return URL(fileURLWithPath: publicDirectory) + } + return URL(fileURLWithPath: dataDirectory) + } } extension Configuration { diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 371fbcb..8817827 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -12,13 +12,14 @@ private let scheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2) private var configurationError: Error? = nil func configure(_ app: Application) async throws { - let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory) + let resourceFolder = URL(fileURLWithPath: app.directory.resourcesDirectory) + let publicDirectory = app.directory.publicDirectory let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory) .appendingPathComponent("config.json") let configuration = try Configuration(loadFromUrl: configPath) - let logFolder = configuration.logURL(possiblyRelativeTo: storageFolder) + let logFolder = configuration.logURL(possiblyRelativeTo: resourceFolder) let monitor = MetricObserver( logFileFolder: logFolder, logMetricId: "schafkopf.log") @@ -39,7 +40,8 @@ func configure(_ app: Application) async throws { app.databases.use(.sqlite(.memory), as: .sqlite) default: app.logger.logLevel = .notice - let dbFile = storageFolder.appendingPathComponent("db.sqlite").path + let dataDirectory = configuration.customDataDirectory(or: publicDirectory) + let dbFile = dataDirectory.appendingPathComponent("db.sqlite").path log("[PRODUCTION] Using database at \(dbFile)") app.databases.use(.sqlite(.file(dbFile)), as: .sqlite) }