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
24 changes: 18 additions & 6 deletions Sources/Vapor/HTTP/Server/HTTPServer.swift
Expand Up @@ -307,19 +307,18 @@ public final class HTTPServer: Server, Sendable {
/// Override the socket path.
configuration.address = address!
}
/// Print starting message.

/// Log starting message for debugging before attempting to start the server.
let scheme = configuration.tlsConfiguration == nil ? "http" : "https"
let addressDescription: String
var addressDescription: String
bisgardo marked this conversation as resolved.
Show resolved Hide resolved
switch configuration.address {
case .hostname(let hostname, let port):
addressDescription = "\(scheme)://\(hostname ?? configuration.hostname):\(port ?? configuration.port)"
addressDescription = "\(scheme)://\(hostname ?? Configuration.defaultHostname):\(port ?? Configuration.defaultPort)"
bisgardo marked this conversation as resolved.
Show resolved Hide resolved
case .unixDomainSocket(let socketPath):
addressDescription = "\(scheme)+unix: \(socketPath)"
}
self.configuration.logger.debug("Server starting on \(addressDescription)")

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 +330,19 @@ 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 hostname = localAddress.hostname, let port = localAddress.port {
bisgardo marked this conversation as resolved.
Show resolved Hide resolved
configuration.port = port
configuration.hostname = hostname
addressDescription = "\(scheme)://\(hostname):\(port)"
bisgardo marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Log started message with the actual configuration.
self.configuration.logger.notice("Server started on \(addressDescription)")

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