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
73 changes: 44 additions & 29 deletions Sources/Vapor/HTTP/Server/HTTPServer.swift
Expand Up @@ -48,23 +48,34 @@ public final class HTTPServer: Server, Sendable {

/// Port the server will bind to.
public var port: Int {
get {
switch address {
case .hostname(_, let port):
return port ?? Self.defaultPort
default:
return Self.defaultPort
}
}
set {
switch address {
case .hostname(let hostname, _):
address = .hostname(hostname, port: newValue)
default:
address = .hostname(nil, port: newValue)
}
}
}
get {
switch address {
case .hostname(_, let port):
return port ?? Self.defaultPort
default:
return Self.defaultPort
}
}
set {
switch address {
case .hostname(let hostname, _):
address = .hostname(hostname, port: newValue)
default:
address = .hostname(nil, port: newValue)
}
}
}

/// A human-readable description of the configured address. Used in log messages when starting server.
public var addressDescription: String {
bisgardo marked this conversation as resolved.
Show resolved Hide resolved
let scheme = tlsConfiguration == nil ? "http" : "https"
switch address {
case .hostname(let hostname, let port):
return "\(scheme)://\(hostname ?? Self.defaultHostname):\(port ?? Self.defaultPort)"
case .unixDomainSocket(let socketPath):
return "\(scheme)+unix: \(socketPath)"
}
}

/// Listen backlog.
public var backlog: Int
Expand Down Expand Up @@ -307,19 +318,10 @@ public final class HTTPServer: Server, Sendable {
/// Override the socket path.
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

/// Log starting message for debugging before attempting to start the server.
configuration.logger.debug("Server starting on \(configuration.addressDescription)")

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

/// Overwrite configuration with actual address, if applicable.
/// They 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.address = .hostname(hostname, port: port)
} else if let pathname = localAddress.pathname {
configuration.address = .unixDomainSocket(path: pathname)
}
}

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

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