Skip to content

Commit

Permalink
Use onComplete callback instead of EventLoopFuture (#59)
Browse files Browse the repository at this point in the history
* Use onComplete callback instead of Future

* Pass on backlog config

* Swift format

* Use hummingbird-core 0.6.0
  • Loading branch information
adam-fowler authored Mar 14, 2021
1 parent 88c127b commit 438ab21
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.4.0"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.2.0"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "1.0.0-alpha.6"),
.package(url: "https://github.com/hummingbird-project/hummingbird-core.git", from: "0.5.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird-core.git", .upToNextMinor(from: "0.6.0")),
],
targets: [
.target(name: "Hummingbird", dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion Sources/Hummingbird/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extension HBApplication {
serverName: self.serverName,
maxUploadSize: self.maxUploadSize,
maxStreamingBufferSize: self.maxStreamingBufferSize,
// backlog: self.backlog,
backlog: self.backlog,
reuseAddress: self.reuseAddress,
tcpNoDelay: self.tcpNoDelay,
withPipeliningAssistance: self.enableHttpPipelining
Expand Down
16 changes: 9 additions & 7 deletions Sources/Hummingbird/Server/Application+HTTPResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension HBApplication {
/// - request: request
/// - context: context from ChannelHandler
/// - Returns: response
public func respond(to request: HBHTTPRequest, context: ChannelHandlerContext) -> EventLoopFuture<HBHTTPResponse> {
public func respond(to request: HBHTTPRequest, context: ChannelHandlerContext, onComplete: @escaping (Result<HBHTTPResponse, Error>) -> Void) {
let request = HBRequest(
head: request.head,
body: request.body,
Expand All @@ -39,13 +39,14 @@ extension HBApplication {
)

// respond to request
return self.responder.respond(to: request)
.map { response in
self.responder.respond(to: request).whenComplete { result in
switch result {
case .success(let response):
response.headers.add(name: "Date", value: HBDateCache.currentDate)
let responseHead = HTTPResponseHead(version: request.version, status: response.status, headers: response.headers)
return HBHTTPResponse(head: responseHead, body: response.body)
}
.flatMapError { error in
onComplete(.success(HBHTTPResponse(head: responseHead, body: response.body)))

case .failure(let error):
// then convert to valid response so this isn't treated as an error further down
let response: HBHTTPResponse
if let error = error as? HBHTTPResponseError {
Expand All @@ -59,8 +60,9 @@ extension HBApplication {
body: .empty
)
}
return request.success(response)
return onComplete(.success(response))
}
}
}
}
}

0 comments on commit 438ab21

Please sign in to comment.