Allow custom data folder

This commit is contained in:
Christoph Hagen 2023-12-25 18:46:05 +01:00
parent 32055ecdec
commit 10194066db
3 changed files with 20 additions and 3 deletions

View File

@ -8,4 +8,5 @@
"tokenExpiryDuration": 15,
},
"monitoringTokens": [],
"dataDirectory" : "/data/schafkopf"
}

View File

@ -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<String>
@ -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 {

View File

@ -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)
}