Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch configuration and log actual port on startup #3160

Merged
merged 12 commits into from Apr 26, 2024
34 changes: 22 additions & 12 deletions Sources/Vapor/HTTP/Server/HTTPServer.swift
Expand Up @@ -308,18 +308,6 @@ public final class HTTPServer: Server, Sendable {
configuration.address = address!
}

/// Print starting message.
let scheme = configuration.tlsConfiguration == nil ? "http" : "https"
let addressDescription: String
switch configuration.address {
case .hostname(let hostname, let port):
addressDescription = "\(scheme)://\(hostname ?? configuration.hostname):\(port ?? configuration.port)"
case .unixDomainSocket(let socketPath):
addressDescription = "\(scheme)+unix: \(socketPath)"
}

self.configuration.logger.notice("Server starting on \(addressDescription)")
bisgardo marked this conversation as resolved.
Show resolved Hide resolved

/// Start the actual `HTTPServer`.
try self.connection.withLockedValue {
$0 = try HTTPServerConnection.start(
Expand All @@ -331,6 +319,28 @@ public final class HTTPServer: Server, Sendable {
).wait()
}

/// Override configuration with actual address.
/// This may differ from the provided configuation if port 0 was provided, for example.
if let localAddress = self.localAddress {
if let port = localAddress.port {
configuration.port = port
}
if let hostname = localAddress.hostname {
configuration.hostname = hostname
}
bisgardo marked this conversation as resolved.
Show resolved Hide resolved
}

/// Print starting message.
bisgardo marked this conversation as resolved.
Show resolved Hide resolved
let scheme = configuration.tlsConfiguration == nil ? "http" : "https"
let addressDescription: String
switch configuration.address {
case .hostname(let hostname, let port):
addressDescription = "\(scheme)://\(hostname ?? configuration.hostname):\(port ?? configuration.port)"
case .unixDomainSocket(let socketPath):
addressDescription = "\(scheme)+unix: \(socketPath)"
}
self.configuration.logger.notice("Server started on \(addressDescription)")

self.configuration = configuration
self.didStart.withLockedValue { $0 = true }
}
Expand Down