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

Make HBBindAddress a struct #361

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Hummingbird/Exports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//

@_exported import enum HummingbirdCore.HBBindAddress
@_exported import struct HummingbirdCore.HBBindAddress
@_exported import struct HummingbirdCore.HBHTTPError
@_exported import protocol HummingbirdCore.HBHTTPResponseError
@_exported import struct HummingbirdCore.HBRequest
Expand Down
30 changes: 11 additions & 19 deletions Sources/HummingbirdCore/Server/BindAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,19 @@
//===----------------------------------------------------------------------===//

/// Address to bind server to
public enum HBBindAddress: Sendable {
/// bind address define by host and port
case hostname(_ host: String = "127.0.0.1", port: Int = 8080)
/// bind address defined by unxi domain socket
case unixDomainSocket(path: String)

/// if address is hostname and port return port
public var port: Int? {
guard case .hostname(_, let port) = self else { return nil }
return port
public struct HBBindAddress: Sendable, Equatable {
enum _Internal: Equatable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always a fan of this pattern.

case hostname(_ host: String = "127.0.0.1", port: Int = 8080)
case unixDomainSocket(path: String)
}

/// if address is hostname and port return hostname
public var host: String? {
guard case .hostname(let host, _) = self else { return nil }
return host
let value: _Internal
init(_ value: _Internal) {
self.value = value
}

/// if address is unix domain socket return unix domain socket path
public var unixDomainSocketPath: String? {
guard case .unixDomainSocket(let path) = self else { return nil }
return path
}
// Address define by host and port
public static func hostname(_ host: String = "127.0.0.1", port: Int = 8080) -> Self { .init(.hostname(host, port: port)) }
// Address defined by unxi domain socket
public static func unixDomainSocket(path: String) -> Self { .init(.unixDomainSocket(path: path)) }

Check warning on line 30 in Sources/HummingbirdCore/Server/BindAddress.swift

View check run for this annotation

Codecov / codecov/patch

Sources/HummingbirdCore/Server/BindAddress.swift#L30

Added line #L30 was not covered by tests
}
2 changes: 1 addition & 1 deletion Sources/HummingbirdCore/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public actor HBServer<ChildChannel: HBChildChannel>: Service {

do {
let asyncChannel: AsyncServerChannel
switch configuration.address {
switch configuration.address.value {
case .hostname(let host, let port):
asyncChannel = try await bootstrap.bind(
host: host,
Expand Down
Loading