Allow absolute key path

This commit is contained in:
Christoph Hagen 2023-12-25 20:12:43 +01:00
parent c9d13bb150
commit e0e2a1cb06
2 changed files with 23 additions and 9 deletions

View File

@ -5,7 +5,11 @@ struct Config {
/// The port where the server runs /// The port where the server runs
let port: Int let port: Int
/// The name of the file in the `Resources` folder containing the device authentication token /**
The path to the file containing the containing the device authentication token.
If the path is relative, then it is relative to the `Resources` folder.
*/
let keyFileName: String let keyFileName: String
/// The seconds to wait for a response from the device /// The seconds to wait for a response from the device
@ -14,20 +18,30 @@ struct Config {
/// The authentication tokens to use for monitoring of the service /// The authentication tokens to use for monitoring of the service
let authenticationTokens: Set<String> let authenticationTokens: Set<String>
/// The path to the folder where the metric logs are stored /**
/// 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 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? let logPath: String?
func logURL(possiblyRelativeTo resourcesDirectory: URL) -> URL { func logURL(possiblyRelativeTo resourcesDirectory: URL) -> URL {
guard let logPath else { guard let logPath else {
return resourcesDirectory.appendingPathComponent("logs") return resourcesDirectory.appendingPathComponent("logs")
} }
guard !logPath.hasPrefix("/") else { return Config.url(logPath, possiblyRelativeTo: resourcesDirectory)
return .init(fileURLWithPath: logPath)
} }
return resourcesDirectory.appendingPathComponent(logPath)
func keyURL(possiblyRelativeTo resourcesDirectory: URL) -> URL {
Config.url(keyFileName, possiblyRelativeTo: resourcesDirectory)
}
private static func url(_ name: String, possiblyRelativeTo resourcesDirectory: URL) -> URL {
guard !name.hasPrefix("/") else {
return .init(fileURLWithPath: name)
}
return resourcesDirectory.appendingPathComponent(name)
} }
} }

View File

@ -33,7 +33,7 @@ public func configure(_ app: Application) async throws {
app.http.server.configuration.port = config.port app.http.server.configuration.port = config.port
let keyFile = storageFolder.appendingPathComponent(config.keyFileName) let keyFile = config.keyURL(possiblyRelativeTo: storageFolder)
let (deviceKey, remoteKey) = try loadKeys(at: keyFile) let (deviceKey, remoteKey) = try loadKeys(at: keyFile)
deviceManager = DeviceManager(deviceKey: deviceKey, remoteKey: remoteKey, deviceTimeout: config.deviceTimeout, serverStatus: status) deviceManager = DeviceManager(deviceKey: deviceKey, remoteKey: remoteKey, deviceTimeout: config.deviceTimeout, serverStatus: status)