From c366a6c1ce67b6bfe3ed7b486ed00d69b7eedcda Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Tue, 3 May 2022 12:05:03 +0200 Subject: [PATCH] Specify port in config file --- Resources/{paths.conf => config.conf} | 1 + Sources/App/configure.swift | 38 ++++++++++++++++++++------- 2 files changed, 29 insertions(+), 10 deletions(-) rename Resources/{paths.conf => config.conf} (87%) diff --git a/Resources/paths.conf b/Resources/config.conf similarity index 87% rename from Resources/paths.conf rename to Resources/config.conf index 60af1bf..7a9987f 100755 --- a/Resources/paths.conf +++ b/Resources/config.conf @@ -1,2 +1,3 @@ /data/logs/festival/server.log 150 +6004 diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index af565bb..9aca031 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -69,12 +69,18 @@ private func saveList(_ set: Set, named name: String, to url: URL) { } private func loadList(from url: URL) throws -> Set { + do { let users = try String(contentsOf: url) .split(separator: "\n") .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } .filter { !$0.isEmpty } - log("Loaded list \(url.path) (\(users.count) entries)") - return .init(users) + log("Loaded list \(url.path) (\(users.count) entries)") + return .init(users) + } catch { + log("Failed to load \(url.path): \(error)") + throw error + } + } private func log(event: String) -> String { @@ -104,7 +110,12 @@ private func createFileIfNeeded(at path: URL) throws { guard !FileManager.default.fileExists(atPath: path.path) else { return } - try Data().write(to: path) + do { + try Data().write(to: path) + } catch { + log("Failed to create \(path.path): \(error)") + throw error + } } private func readConfig(at path: URL) throws -> String { @@ -118,12 +129,12 @@ private func readConfig(at path: URL) throws -> String { } } -private func configureFromFile(at configPath: URL) throws { +private func configureFromFile(at configPath: URL, app: Application) throws { let config = try readConfig(at: configPath) .components(separatedBy: "\n") .map { $0.trimmingCharacters(in: .whitespaces) } .filter { !$0.isEmpty } - guard config.count == 2 else { + guard config.count == 3 else { log("Invalid configuration file at \(configPath.path)") throw FestivalError.invalidConfiguration } @@ -133,15 +144,21 @@ private func configureFromFile(at configPath: URL) throws { return } maximumGuestCount = count + guard let port = Int(config[2]) else { + log("Invalid port '\(config[2])', using default") + return + } + app.http.server.configuration.port = port + } public func configure(_ app: Application) throws { - app.http.server.configuration.port = 9001 let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory) - .appendingPathComponent("paths.conf") - try configureFromFile(at: configPath) - + .appendingPathComponent("config.conf") + try configureFromFile(at: configPath, app: app) + + let listDirectory = URL(fileURLWithPath: app.directory.publicDirectory) .appendingPathComponent("lists") let eventLog = listDirectory.appendingPathComponent("events.txt") @@ -170,6 +187,7 @@ public func configure(_ app: Application) throws { let date = Date() let dateString = df.string(from: date) - log("[\(dateString)] Server started: \(registeredGuests.count) registered, \(declinedGuests.count) declined, event log open: \(eventLogHandle != nil)") + + log("[\(dateString)] Server started on port \(app.http.server.configuration.port): \(registeredGuests.count) registered, \(declinedGuests.count) declined, event log open: \(eventLogHandle != nil)") }