diff --git a/Examples/.swiftlint.yml b/Examples/.swiftlint.yml new file mode 100644 index 000000000..629b92772 --- /dev/null +++ b/Examples/.swiftlint.yml @@ -0,0 +1,2 @@ +disabled_rules: + - explicit_acl diff --git a/Examples/PerfectExample/Package.swift b/Examples/PerfectExample/Package.swift index de453934d..c2cb2c693 100644 --- a/Examples/PerfectExample/Package.swift +++ b/Examples/PerfectExample/Package.swift @@ -1,11 +1,12 @@ -// swift-tools-version:4.2 +// swift-tools-version:5.0 import PackageDescription let package = Package( name: "PerfectExample", dependencies: [ .package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"), - .package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0")), + .package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0")), + .package(url: "https://github.com/apple/swift-nio", .upToNextMajor(from: "2.14.0")) ], targets: [ .target(name: "PerfectExample", dependencies: ["PerfectHTTPServer", "MongoSwift"]) diff --git a/Examples/PerfectExample/Sources/PerfectExample/main.swift b/Examples/PerfectExample/Sources/PerfectExample/main.swift index 038e76d11..d2e18f43d 100644 --- a/Examples/PerfectExample/Sources/PerfectExample/main.swift +++ b/Examples/PerfectExample/Sources/PerfectExample/main.swift @@ -1,32 +1,44 @@ import Foundation import MongoSwift +import NIO import PerfectHTTP import PerfectHTTPServer /// A Codable type that matches the data in our home.kittens collection. -private struct Kitten: Codable { +struct Kitten: Codable { var name: String var color: String } +// Create a SwiftNIO EventLoopGroup for the client to use. +let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4) +let mongoClient = try MongoClient(using: elg) + +defer { + // Close the client and clean up global driver resources. + mongoClient.syncShutdown() + cleanupMongoSwift() + // Shut down the EventLoopGroup. + try? elg.syncShutdownGracefully() +} + /// A single collection with type `Kitten`. This allows us to directly retrieve instances of /// `Kitten` from the collection. `MongoCollection` is safe to share across threads. -private let collection = try MongoClient().db("home").collection("kittens", withType: Kitten.self) +let collection = mongoClient.db("home").collection("kittens", withType: Kitten.self) private var routes = Routes() routes.add(method: .get, uri: "/kittens") { _, response in - response.setHeader(.contentType, value: "application/json") - do { - let cursor = try collection.find() - let json = try JSONEncoder().encode(Array(cursor)) - if let error = cursor.error { - throw error - } + collection.find().flatMap { cursor in + cursor.toArray() + }.flatMapThrowing { results in + response.setHeader(.contentType, value: "application/json") + let json = try JSONEncoder().encode(results) response.setBody(bytes: Array(json)) - } catch { - print("error: \(error)") + response.completed() + }.whenFailure { error in + response.setBody(string: "Error: \(error)") + response.completed() } - response.completed() } try HTTPServer.launch(name: "localhost", port: 8080, routes: routes)