From 3241a5ed22388a2d1d7e33e264b32a4b35e7ea60 Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Wed, 5 Feb 2020 17:53:34 -0500 Subject: [PATCH 1/3] Perfect example WIP --- Examples/.swiftlint.yml | 2 ++ Examples/PerfectExample/Package.swift | 4 +-- .../Sources/PerfectExample/main.swift | 29 ++++++++++--------- 3 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 Examples/.swiftlint.yml 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..fe5f83fe7 100644 --- a/Examples/PerfectExample/Package.swift +++ b/Examples/PerfectExample/Package.swift @@ -1,11 +1,11 @@ -// 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", .branch("master")), ], 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..4146a2588 100644 --- a/Examples/PerfectExample/Sources/PerfectExample/main.swift +++ b/Examples/PerfectExample/Sources/PerfectExample/main.swift @@ -1,32 +1,35 @@ import Foundation -import MongoSwift +@testable 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 } +let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4) +let mongoClient = try MongoClient(using: elg) + /// 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.all() + }.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) From fcd4d2cbda2515bf7fa4e010dcfe625249a7bc5d Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Wed, 19 Feb 2020 23:31:12 -0500 Subject: [PATCH 2/3] update to new API --- Examples/PerfectExample/Package.swift | 1 + .../Sources/PerfectExample/main.swift | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Examples/PerfectExample/Package.swift b/Examples/PerfectExample/Package.swift index fe5f83fe7..53da82cea 100644 --- a/Examples/PerfectExample/Package.swift +++ b/Examples/PerfectExample/Package.swift @@ -6,6 +6,7 @@ let package = Package( dependencies: [ .package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"), .package(url: "https://github.com/mongodb/mongo-swift-driver", .branch("master")), + .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 4146a2588..d2e18f43d 100644 --- a/Examples/PerfectExample/Sources/PerfectExample/main.swift +++ b/Examples/PerfectExample/Sources/PerfectExample/main.swift @@ -1,5 +1,5 @@ import Foundation -@testable import MongoSwift +import MongoSwift import NIO import PerfectHTTP import PerfectHTTPServer @@ -10,9 +10,18 @@ struct Kitten: Codable { 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. let collection = mongoClient.db("home").collection("kittens", withType: Kitten.self) @@ -20,7 +29,7 @@ let collection = mongoClient.db("home").collection("kittens", withType: Kitten.s private var routes = Routes() routes.add(method: .get, uri: "/kittens") { _, response in collection.find().flatMap { cursor in - cursor.all() + cursor.toArray() }.flatMapThrowing { results in response.setHeader(.contentType, value: "application/json") let json = try JSONEncoder().encode(results) From 23b1b6f9aeb385cae39f6230291bf44659bce32e Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Fri, 21 Feb 2020 13:42:04 -0500 Subject: [PATCH 3/3] Update Package.swift --- Examples/PerfectExample/Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/PerfectExample/Package.swift b/Examples/PerfectExample/Package.swift index 53da82cea..c2cb2c693 100644 --- a/Examples/PerfectExample/Package.swift +++ b/Examples/PerfectExample/Package.swift @@ -5,7 +5,7 @@ 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", .branch("master")), + .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: [