Extract config

This commit is contained in:
Christoph Hagen 2022-04-13 14:57:02 +02:00
parent bf755b4d50
commit e6fc0308ed
3 changed files with 16 additions and 6 deletions

13
Sources/App/Config.swift Normal file
View File

@ -0,0 +1,13 @@
import Foundation
struct Config {
/// The port where the server runs
static let port = 6003
/// The name of the file in the `Resources` folder containing the device authentication token
static let keyFileName = "deviceKey"
/// The seconds to wait for a response from the device
static let deviceTimeout: Int64 = 20
}

View File

@ -4,9 +4,6 @@ import Vapor
final class DeviceManager {
/// The seconds to wait for a response from the device
static let deviceTimeout: Int64 = 20
/// The connection to the device
private var connection: WebSocket?
@ -46,7 +43,7 @@ final class DeviceManager {
}
requestInProgress = eventLoop.makePromise(of: DeviceResponse.self)
socket.send(message.bytes, promise: nil)
eventLoop.scheduleTask(in: .seconds(Self.deviceTimeout)) { [weak self] in
eventLoop.scheduleTask(in: .seconds(Config.deviceTimeout)) { [weak self] in
guard let promise = self?.requestInProgress else {
return
}

View File

@ -4,10 +4,10 @@ var deviceManager: DeviceManager!
// configures your application
public func configure(_ app: Application) throws {
app.http.server.configuration.port = 6003
app.http.server.configuration.port = Config.port
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
let keyFile = storageFolder.appendingPathComponent("deviceKey")
let keyFile = storageFolder.appendingPathComponent(Config.keyFileName)
let deviceKey = try String(contentsOf: keyFile)
.trimmingCharacters(in: .whitespacesAndNewlines)
deviceManager = DeviceManager(deviceKey: deviceKey)