From e0e2a1cb067487640125bcf736594d9362cc307b Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Mon, 25 Dec 2023 20:12:43 +0100 Subject: [PATCH] Allow absolute key path --- Sources/App/Config.swift | 30 ++++++++++++++++++++++-------- Sources/App/configure.swift | 2 +- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Sources/App/Config.swift b/Sources/App/Config.swift index b78976c..9e60c50 100644 --- a/Sources/App/Config.swift +++ b/Sources/App/Config.swift @@ -5,7 +5,11 @@ 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 + /** + 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 /// 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 let authenticationTokens: Set - /// 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 + /** + 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 Config.url(logPath, possiblyRelativeTo: resourcesDirectory) + } + + 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(logPath) + return resourcesDirectory.appendingPathComponent(name) } } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 2dae169..1dbaf97 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -33,7 +33,7 @@ public func configure(_ app: Application) async throws { 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) deviceManager = DeviceManager(deviceKey: deviceKey, remoteKey: remoteKey, deviceTimeout: config.deviceTimeout, serverStatus: status)