Fixed a few minor typos and some force unwraps (#259)

fix some typos, force unwraps and inconsistencies

Motivation:
Reviewing the code I found a few typos that hindered readability, some unnecessary force unwraps and inconsistencies in the codebase.

Modifications:

- The main file in NIOWebSocketServer now accepts command line arguments
and binds to a passed in host and port like the other server examples do.
- Minor typo fixes
- removed one force unwrap

Result:

- The result of my change will bring the NIOWebSocketServer more inline
with how the NIOHTTP1 and NIOChat servers examples are setup.
- less force unwrapping
- fewer typos

[message edited by @weissi]
This commit is contained in:
Matt Eaton authored and Johannes Weiß committed 2018-04-03 11:44:14 +01:00
1 parent 2a684fc1dc
commit 66e4fdfbba
15 files changed
+69 -19

No files matched your search

+44 -2
View File
@@ -224,9 +224,51 @@ defer {
try! group.syncShutdownGracefully()
}
// First argument is the program path
let arguments = CommandLine.arguments
let arg1 = arguments.dropFirst().first
let arg2 = arguments.dropFirst(2).first
let channel = try! bootstrap.bind(host: "localhost", port: 8888).wait()
print("Server started and listening on \(channel.localAddress!)")
let defaultHost = "localhost"
let defaultPort = 8888
enum BindTo {
case ip(host: String, port: Int)
case unixDomainSocket(path: String)
}
let bindTarget: BindTo
switch (arg1, arg1.flatMap(Int.init), arg2.flatMap(Int.init)) {
case (.some(let h), _ , .some(let p)):
/* we got two arguments, let's interpret that as host and port */
bindTarget = .ip(host: h, port: p)
case (let portString?, .none, _):
// Couldn't parse as number, expecting unix domain socket path.
bindTarget = .unixDomainSocket(path: portString)
case (_, let p?, _):
// Only one argument --> port.
bindTarget = .ip(host: defaultHost, port: p)
default:
bindTarget = .ip(host: defaultHost, port: defaultPort)
}
let channel = try { () -> Channel in
switch bindTarget {
case .ip(let host, let port):
return try bootstrap.bind(host: host, port: port).wait()
case .unixDomainSocket(let path):
return try bootstrap.bind(unixDomainSocketPath: path).wait()
}
}()
if let localAddress = channel.localAddress {
print("Server started and listening on \(localAddress)")
} else {
print("Server started but the address could not be determined.")
}
// This will never unblock as we don't close the ServerChannel
try channel.closeFuture.wait()