51 lines
1.2 KiB
Swift
51 lines
1.2 KiB
Swift
import Foundation
|
|
|
|
struct Config: Codable {
|
|
|
|
/// The port where the server runs
|
|
let port: Int
|
|
|
|
/// The maximum size of the request body
|
|
let maxBodySize: String
|
|
|
|
/// The path to the folder where the metric logs are stored
|
|
let logPath: String
|
|
|
|
/// Serve files in the Public directory using Vapor
|
|
let serveFiles: Bool
|
|
|
|
/// Authentication tokens for remotes allowed to write
|
|
let writers: [String]
|
|
|
|
var logURL: URL {
|
|
.init(fileURLWithPath: logPath)
|
|
}
|
|
}
|
|
|
|
extension Config {
|
|
|
|
private static func file(in directory: URL) -> URL {
|
|
directory.appendingPathComponent("config.json")
|
|
}
|
|
|
|
init(loadFrom directory: URL) {
|
|
let configFileUrl = Config.file(in: directory)
|
|
|
|
guard FileManager.default.fileExists(atPath: configFileUrl.path) else {
|
|
print("No configuration found at \(configFileUrl.path)")
|
|
exit(-1)
|
|
}
|
|
self.init(loadAt: configFileUrl)
|
|
}
|
|
|
|
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)")
|
|
exit(-1)
|
|
}
|
|
}
|
|
}
|