-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Vapor for the Swift example (#3)
- Loading branch information
Showing
3 changed files
with
182 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,25 @@ | ||
// swift-tools-version:5.2 | ||
import Foundation | ||
import NIO | ||
import NIOHTTP1 | ||
|
||
struct Version: Codable { | ||
let version: String | ||
} | ||
|
||
func JSON<T: Codable>(_ value: T) -> Data { | ||
return (try? JSONEncoder().encode(value)) ?? Data() | ||
} | ||
|
||
final class HTTPHandler: ChannelInboundHandler { | ||
typealias InboundIn = HTTPServerRequestPart | ||
typealias OutboundOut = HTTPServerResponsePart | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
let req = self.unwrapInboundIn(data) | ||
import Vapor | ||
|
||
switch req { | ||
case .head(let requestHead): | ||
if requestHead.uri == "/version" && requestHead.method == .GET { | ||
let version = Version(version: "1.0.0") | ||
let response = JSON(version) | ||
|
||
var headers = HTTPHeaders() | ||
headers.add(name: "Content-Type", value: "application/json") | ||
headers.add(name: "Content-Length", value: "\(response.count)") | ||
|
||
let responseHead = HTTPResponseHead( | ||
version: requestHead.version, | ||
status: .ok, | ||
headers: headers) | ||
_ = context.write(self.wrapOutboundOut(.head(responseHead))) | ||
var buffer = context.channel.allocator.buffer(capacity: response.count) | ||
buffer.writeBytes(response) | ||
_ = context.write(self.wrapOutboundOut(.body(.byteBuffer(buffer)))) | ||
_ = context.writeAndFlush(self.wrapOutboundOut(.end(nil))) | ||
} else { | ||
let responseHead = HTTPResponseHead(version: requestHead.version, status: .notFound) | ||
_ = context.write(self.wrapOutboundOut(.head(responseHead))) | ||
_ = context.writeAndFlush(self.wrapOutboundOut(.end(nil))) | ||
} | ||
|
||
case .body: | ||
break | ||
|
||
case .end: | ||
break | ||
} | ||
} | ||
struct Version : Content { | ||
var version: String = "1.0.0" | ||
} | ||
|
||
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount) | ||
let bootstrap = ServerBootstrap(group: group) | ||
.childChannelInitializer { channel in | ||
channel.pipeline.configureHTTPServerPipeline().flatMap { | ||
channel.pipeline.addHandler(HTTPHandler()) | ||
} | ||
} | ||
struct Main { | ||
|
||
defer { | ||
try? group.syncShutdownGracefully() | ||
} | ||
static func main() async { | ||
do { | ||
let app = try await Application.make() | ||
app.get("version", use: { _ in Version() }) | ||
try await app.execute() | ||
try await app.asyncShutdown() | ||
} catch { | ||
print("got error: \(error)") | ||
} | ||
} | ||
|
||
let serverChannel = try bootstrap.bind(host: "127.0.0.1", port: 8000).wait() | ||
print("listening on http://127.0.0.1:8000") | ||
try serverChannel.closeFuture.wait() | ||
} | ||
_ = await Task{ await Main.main() }.value |