Caps-Server/Sources/App/Config.swift

51 lines
1.2 KiB
Swift
Raw Normal View History

import Foundation
struct Config: Codable {
2023-01-11 18:26:53 +01:00
/// The port where the server runs
let port: Int
/// The maximum size of the request body
let maxBodySize: String
2023-02-06 21:31:12 +01:00
/// The path to the folder where the metric logs are stored
let logPath: String
2022-05-27 09:25:41 +02:00
/// Serve files in the Public directory using Vapor
let serveFiles: Bool
2022-05-27 09:25:41 +02:00
/// Authentication tokens for remotes allowed to write
let writers: [String]
var logURL: URL {
.init(fileURLWithPath: logPath)
}
2023-01-11 18:26:53 +01:00
}
extension Config {
private static func file(in directory: URL) -> URL {
directory.appendingPathComponent("config.json")
}
init(loadFrom directory: URL) {
let configFileUrl = Config.file(in: directory)
2023-05-07 21:07:30 +02:00
guard FileManager.default.fileExists(atPath: configFileUrl.path) else {
print("No configuration found at \(configFileUrl.path)")
exit(-1)
2023-01-11 18:26:53 +01:00
}
2023-05-07 21:07:30 +02:00
self.init(loadAt: configFileUrl)
2023-01-11 18:26:53 +01:00
}
private init(loadAt url: URL) {
do {
let configData = try Data(contentsOf: url)
self = try JSONDecoder().decode(Config.self, from: configData)
} catch {
print("Failed to load configuration from \(url.path): \(error)")
2023-05-07 21:07:30 +02:00
exit(-1)
2023-01-11 18:26:53 +01:00
}
}
}