44 lines
1.1 KiB
Swift
44 lines
1.1 KiB
Swift
import Foundation
|
|
|
|
struct Config {
|
|
|
|
/// The port where the server runs
|
|
let port: Int
|
|
|
|
/// The name of the file in the `Resources` folder containing the device authentication token
|
|
let keyFileName: String
|
|
|
|
/// The seconds to wait for a response from the device
|
|
let deviceTimeout: Int64
|
|
|
|
/// The authentication tokens to use for monitoring of the service
|
|
let authenticationTokens: Set<String>
|
|
}
|
|
|
|
extension Config: Codable {
|
|
|
|
}
|
|
|
|
extension Config {
|
|
|
|
init(loadFrom url: URL) throws {
|
|
guard FileManager.default.fileExists(atPath: url.path) else {
|
|
log("No configuration file found at \(url.path)")
|
|
fatalError("No configuration file found")
|
|
}
|
|
let data: Data
|
|
do {
|
|
data = try Data(contentsOf: url)
|
|
} catch {
|
|
log("Failed to read config data: \(error)")
|
|
throw error
|
|
}
|
|
do {
|
|
self = try JSONDecoder().decode(Config.self, from: data)
|
|
} catch {
|
|
log("Failed to decode config data: \(error)")
|
|
throw error
|
|
}
|
|
}
|
|
}
|