Skip to content

Commit

Permalink
Listen on specific address
Browse files Browse the repository at this point in the history
  • Loading branch information
djones6 committed May 17, 2019
1 parent c063e9f commit a5539cf
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions Sources/Kitura/Kitura.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Kitura {

// Socket types that we currently support
private enum ListenerType {
case inet(Int)
case inet(Int, String?)
case unix(String)
}

Expand All @@ -66,11 +66,12 @@ public class Kitura {
/// - Returns: The created `HTTPServer`.
@discardableResult
public class func addHTTPServer(onPort port: Int,
onAddress address: String? = nil,
with delegate: ServerDelegate,
withSSL sslConfig: SSLConfig?=nil,
keepAlive keepAliveState: KeepAliveState = .unlimited,
allowPortReuse: Bool = false) -> HTTPServer {
return Kitura._addHTTPServer(on: .inet(port), with: delegate, withSSL: sslConfig, keepAlive: keepAliveState, allowPortReuse: allowPortReuse)
return Kitura._addHTTPServer(on: .inet(port, address), with: delegate, withSSL: sslConfig, keepAlive: keepAliveState, allowPortReuse: allowPortReuse)
}

/// Add an HTTPServer on a Unix domain socket path with a delegate.
Expand Down Expand Up @@ -108,8 +109,8 @@ public class Kitura {
server.allowPortReuse = allowPortReuse
serverLock.lock()
switch listenType {
case .inet(let port):
httpServersAndPorts.append((server: server, port: port))
case .inet(let port, let address):
httpServersAndPorts.append((server: server, port: (port, address)))
case .unix(let socketPath):
httpServersAndUnixSocketPaths.append((server: server, socketPath: socketPath))
}
Expand All @@ -133,13 +134,14 @@ public class Kitura {
/// - Returns: The created `FastCGIServer`.
@discardableResult
public class func addFastCGIServer(onPort port: Int,
onAddress address: String? = nil,
with delegate: ServerDelegate,
allowPortReuse: Bool = false) -> FastCGIServer {
let server = FastCGI.createServer()
server.delegate = delegate
server.allowPortReuse = allowPortReuse
serverLock.lock()
fastCGIServersAndPorts.append((server: server, port: port))
fastCGIServersAndPorts.append((server: server, port: (port, address)))
serverLock.unlock()
return server
}
Expand Down Expand Up @@ -173,12 +175,12 @@ public class Kitura {
///```
public class func start() {
serverLock.lock()
for (server, port) in httpServersAndPorts {
Log.verbose("Starting an HTTP Server on port \(port)...")
for (server, (port, address)) in httpServersAndPorts {
Log.verbose("Starting an HTTP Server on address \(address ?? "*"):\(port)...")
do {
try server.listen(on: port)
try server.listen(on: port, address: address)
} catch {
Log.error("Error listening on port \(port): \(error). Use server.failed(callback:) to handle")
Log.error("Error listening on address \(address ?? "*"):\(port): \(error). Use server.failed(callback:) to handle")
}
}
for (server, path) in httpServersAndUnixSocketPaths {
Expand All @@ -189,12 +191,12 @@ public class Kitura {
Log.error("Error listening on path \(path): \(error). Use server.failed(callback:) to handle")
}
}
for (server, port) in fastCGIServersAndPorts {
Log.verbose("Starting a FastCGI Server on port \(port)...")
for (server, (port, address)) in fastCGIServersAndPorts {
Log.verbose("Starting a FastCGI Server on address \(address ?? "*"):\(port)...")
do {
try server.listen(on: port)
try server.listen(on: port, address: address)
} catch {
Log.error("Error listening on port \(port): \(error). Use server.failed(callback:) to handle")
Log.error("Error listening on address \(address ?? "*"):\(port): \(error). Use server.failed(callback:) to handle")
}
}
serverLock.unlock()
Expand Down Expand Up @@ -239,7 +241,7 @@ public class Kitura {
serverLock.unlock()
}

typealias Port = Int
typealias Port = (Int, String?)
internal static let serverLock = NSLock()
internal private(set) static var httpServersAndPorts = [(server: HTTPServer, port: Port)]()
internal private(set) static var httpServersAndUnixSocketPaths = [(server: HTTPServer, socketPath: String)]()
Expand Down

0 comments on commit a5539cf

Please sign in to comment.