Specify port in config file

This commit is contained in:
Christoph Hagen 2022-05-03 12:05:03 +02:00
parent 721bf294b8
commit c366a6c1ce
2 changed files with 29 additions and 10 deletions

View File

@ -1,2 +1,3 @@
/data/logs/festival/server.log
150
6004

View File

@ -69,12 +69,18 @@ private func saveList(_ set: Set<String>, named name: String, to url: URL) {
}
private func loadList(from url: URL) throws -> Set<String> {
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)
} 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
}
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,14 +144,20 @@ 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")
@ -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)")
}