2022-10-11 12:00:46 +02:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
struct Configuration {
|
|
|
|
|
|
|
|
let serverPort: Int
|
2022-10-11 12:09:43 +02:00
|
|
|
|
2022-11-24 21:31:03 +01:00
|
|
|
let mail: EMail?
|
2022-10-11 12:09:43 +02:00
|
|
|
|
2022-10-13 12:25:51 +02:00
|
|
|
/**
|
|
|
|
Use a database file and reduce logging.
|
|
|
|
|
|
|
|
If this property is set to `false`, then an in-memory database is used with increased logging.
|
|
|
|
*/
|
|
|
|
let production: Bool
|
|
|
|
|
2022-10-11 12:09:43 +02:00
|
|
|
struct EMail {
|
|
|
|
|
|
|
|
/// The url to the root of the server
|
|
|
|
let serverDomain: String
|
|
|
|
|
|
|
|
/// SMTP server address
|
|
|
|
let emailHostname: String
|
|
|
|
|
|
|
|
/// username to login
|
|
|
|
let email: String
|
|
|
|
|
|
|
|
/// password to login
|
|
|
|
let password: String
|
|
|
|
|
|
|
|
/// The number of minutes until a password reset token is no longer valid
|
|
|
|
let tokenExpiryDuration: Int
|
|
|
|
}
|
2023-01-31 22:16:44 +01:00
|
|
|
|
|
|
|
/// The authentication tokens to access the metrics
|
|
|
|
let monitoringTokens: Set<String>
|
2023-11-22 11:47:34 +01:00
|
|
|
|
|
|
|
/// The path to the folder where the metric logs are stored
|
|
|
|
///
|
|
|
|
/// If no path is provided, then a folder `logs` in the resources directory is created
|
|
|
|
/// If the path is relative, then it is assumed relative to the resources directory
|
|
|
|
let logPath: String?
|
|
|
|
|
|
|
|
func logURL(possiblyRelativeTo resourcesDirectory: URL) -> URL {
|
|
|
|
guard let logPath else {
|
|
|
|
return resourcesDirectory.appendingPathComponent("logs")
|
|
|
|
}
|
|
|
|
guard !logPath.hasPrefix("/") else {
|
|
|
|
return .init(fileURLWithPath: logPath)
|
|
|
|
}
|
|
|
|
return resourcesDirectory.appendingPathComponent(logPath)
|
|
|
|
}
|
2022-10-11 12:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Configuration {
|
|
|
|
|
|
|
|
init(loadFromUrl url: URL) throws {
|
|
|
|
let data: Data
|
|
|
|
do {
|
|
|
|
data = try Data(contentsOf: url)
|
|
|
|
} catch {
|
2023-02-06 22:03:02 +01:00
|
|
|
log("Failed to load configuration data from \(url.path): \(error)")
|
2022-10-11 12:00:46 +02:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
try self.init(data: data)
|
|
|
|
}
|
|
|
|
|
|
|
|
init(data: Data) throws {
|
|
|
|
do {
|
|
|
|
self = try JSONDecoder().decode(Configuration.self, from: data)
|
|
|
|
} catch {
|
2023-02-06 22:03:02 +01:00
|
|
|
log("Failed to decode configuration: \(error)")
|
2022-10-11 12:00:46 +02:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 12:09:43 +02:00
|
|
|
|
|
|
|
extension Configuration.EMail: Codable {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-11 12:00:46 +02:00
|
|
|
extension Configuration: Codable {
|
|
|
|
|
|
|
|
}
|