From 1ecf61928f9d7a124662600349dc9bce6e4ff529 Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Tue, 4 Feb 2020 17:36:22 -0500 Subject: [PATCH 1/6] Kitura example WIP --- Examples/.swiftlint.yml | 2 ++ Examples/KituraExample/Package.swift | 6 ++-- Examples/KituraExample/README.md | 3 +- .../Sources/KituraExample/main.swift | 35 ++++++++++++------- 4 files changed, 29 insertions(+), 17 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/KituraExample/Package.swift b/Examples/KituraExample/Package.swift index 7c732ea41..a17d0801e 100644 --- a/Examples/KituraExample/Package.swift +++ b/Examples/KituraExample/Package.swift @@ -1,11 +1,11 @@ -// swift-tools-version:4.2 +// swift-tools-version:5.0 import PackageDescription let package = Package( name: "KituraExample", dependencies: [ - .package(url: "https://github.com/IBM-Swift/Kitura", .upToNextMajor(from: "2.6.3")), - .package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0")) + .package(url: "https://github.com/IBM-Swift/Kitura", .upToNextMajor(from: "2.9.1")), + .package(url: "https://github.com/mongodb/mongo-swift-driver", .branch("master")) ], targets: [ .target(name: "KituraExample", dependencies: ["Kitura", "MongoSwift"]) diff --git a/Examples/KituraExample/README.md b/Examples/KituraExample/README.md index 23b5cee38..a2dd40d52 100644 --- a/Examples/KituraExample/README.md +++ b/Examples/KituraExample/README.md @@ -7,5 +7,6 @@ To test it out, do the following: 1. Navigate to the `Examples/` directory (one level up from this one.) 1. Run `../loadExampleData.sh` to load sample data into the database. 1. Navigate to the root directory of this example. -1. Run `swift run`. +1. Build with `export KITURA_NIO=1 && swift build` to enable using SwiftNIO for the networking layer. +1. Start the server with `swift run`. 1. Navigate to `localhost:8080/kittens` to see the example data loaded on the web page. diff --git a/Examples/KituraExample/Sources/KituraExample/main.swift b/Examples/KituraExample/Sources/KituraExample/main.swift index 792baf9c8..947b984c3 100644 --- a/Examples/KituraExample/Sources/KituraExample/main.swift +++ b/Examples/KituraExample/Sources/KituraExample/main.swift @@ -1,30 +1,39 @@ import Kitura -import MongoSwift +@testable import MongoSwift +import NIO /// 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 } -/// 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 eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 4) +let mongoClient = try MongoClient(using: eventLoopGroup) +defer { + try? mongoClient.close().wait() + try? eventLoopGroup.syncShutdownGracefully() +} -private let router: Router = { +let router: Router = { let router = Router() - router.get("kittens") { _, response, _ in - let cursor = try collection.find() - let results = Array(cursor) - if let error = cursor.error { - throw error + /// 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) + + router.get("kittens") { _, response, next -> Void in + collection.find().flatMap { cursor in + cursor.all() + }.whenSuccess { results in + response.send(results) + next() } - response.send(results) } return router }() -Kitura.addHTTPServer(onPort: 8080, with: router) +let server = Kitura.addHTTPServer(onPort: 8080, with: router) +try server.setEventLoopGroup(eventLoopGroup) Kitura.run() From ca7d4f6a74a9922110574958d3a120881e3cabfc Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Wed, 5 Feb 2020 18:26:01 -0500 Subject: [PATCH 2/6] handle error --- .../KituraExample/Sources/KituraExample/main.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Examples/KituraExample/Sources/KituraExample/main.swift b/Examples/KituraExample/Sources/KituraExample/main.swift index 947b984c3..34fd06b70 100644 --- a/Examples/KituraExample/Sources/KituraExample/main.swift +++ b/Examples/KituraExample/Sources/KituraExample/main.swift @@ -23,12 +23,20 @@ let router: Router = { let collection = mongoClient.db("home").collection("kittens", withType: Kitten.self) router.get("kittens") { _, response, next -> Void in - collection.find().flatMap { cursor in + let res = collection.find().flatMap { cursor in cursor.all() - }.whenSuccess { results in + } + + res.whenSuccess { results in response.send(results) next() } + + res.whenFailure { error in + response.error = error + response.send("Error: \(error)") + next() + } } return router From 5b51be195433d5f1ca878c2206f4c4f9b67b3d15 Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Wed, 19 Feb 2020 22:13:48 -0500 Subject: [PATCH 3/6] use new APIs --- Examples/KituraExample/Package.resolved | 124 ++++++++++++++++++ .../Sources/KituraExample/main.swift | 6 +- 2 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 Examples/KituraExample/Package.resolved diff --git a/Examples/KituraExample/Package.resolved b/Examples/KituraExample/Package.resolved new file mode 100644 index 000000000..8babef2a1 --- /dev/null +++ b/Examples/KituraExample/Package.resolved @@ -0,0 +1,124 @@ +{ + "object": { + "pins": [ + { + "package": "Socket", + "repositoryURL": "https://github.com/IBM-Swift/BlueSocket.git", + "state": { + "branch": null, + "revision": "c46a3d41f5b2401d18bcb46d0101cdc5cd13e307", + "version": "1.0.52" + } + }, + { + "package": "SSLService", + "repositoryURL": "https://github.com/IBM-Swift/BlueSSLService.git", + "state": { + "branch": null, + "revision": "ab2c2aa2fe574969f391c80ee87746be431dee0e", + "version": "1.0.52" + } + }, + { + "package": "Kitura", + "repositoryURL": "https://github.com/IBM-Swift/Kitura", + "state": { + "branch": null, + "revision": "bbb7462ac3d422cad4480a57ddbd33263c381a7b", + "version": "2.9.1" + } + }, + { + "package": "Kitura-NIO", + "repositoryURL": "https://github.com/IBM-Swift/Kitura-NIO.git", + "state": { + "branch": null, + "revision": "69bd82be54207512bee5baf374f461fe83190b2e", + "version": "2.4.0" + } + }, + { + "package": "Kitura-TemplateEngine", + "repositoryURL": "https://github.com/IBM-Swift/Kitura-TemplateEngine.git", + "state": { + "branch": null, + "revision": "d62d74bca48c6fb76f9fc1f48eeb2d7af415e80b", + "version": "2.0.1" + } + }, + { + "package": "KituraContracts", + "repositoryURL": "https://github.com/IBM-Swift/KituraContracts.git", + "state": { + "branch": null, + "revision": "a30e2fb79e926672776a05ec6b919c239870a221", + "version": "1.2.1" + } + }, + { + "package": "LoggerAPI", + "repositoryURL": "https://github.com/IBM-Swift/LoggerAPI.git", + "state": { + "branch": null, + "revision": "3357dd9526cdf9436fa63bb792b669e6efdc43da", + "version": "1.9.0" + } + }, + { + "package": "MongoSwift", + "repositoryURL": "https://github.com/mongodb/mongo-swift-driver", + "state": { + "branch": "master", + "revision": "1c72db375d9762244974a2bf64509eb514062fa9", + "version": null + } + }, + { + "package": "Nimble", + "repositoryURL": "https://github.com/Quick/Nimble.git", + "state": { + "branch": null, + "revision": "f8657642dfdec9973efc79cc68bcef43a653a2bc", + "version": "8.0.2" + } + }, + { + "package": "swift-log", + "repositoryURL": "https://github.com/apple/swift-log.git", + "state": { + "branch": null, + "revision": "74d7b91ceebc85daf387ebb206003f78813f71aa", + "version": "1.2.0" + } + }, + { + "package": "swift-nio-extras", + "repositoryURL": "https://github.com/apple/swift-nio-extras.git", + "state": { + "branch": null, + "revision": "b4dbfacff47fb8d0f9e0a422d8d37935a9f10570", + "version": "1.4.0" + } + }, + { + "package": "swift-nio-ssl", + "repositoryURL": "https://github.com/apple/swift-nio-ssl.git", + "state": { + "branch": null, + "revision": "af46d9b58fafbb76f9b01177568d435a1b024f99", + "version": "2.6.2" + } + }, + { + "package": "TypeDecoder", + "repositoryURL": "https://github.com/IBM-Swift/TypeDecoder.git", + "state": { + "branch": null, + "revision": "a5978582981f7151594bdaf5fe0d3cd9b1d2d0c0", + "version": "1.3.4" + } + } + ] + }, + "version": 1 +} diff --git a/Examples/KituraExample/Sources/KituraExample/main.swift b/Examples/KituraExample/Sources/KituraExample/main.swift index 34fd06b70..2ed555b7b 100644 --- a/Examples/KituraExample/Sources/KituraExample/main.swift +++ b/Examples/KituraExample/Sources/KituraExample/main.swift @@ -1,5 +1,5 @@ import Kitura -@testable import MongoSwift +import MongoSwift import NIO /// A Codable type that matches the data in our home.kittens collection. @@ -11,7 +11,7 @@ struct Kitten: Codable { let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 4) let mongoClient = try MongoClient(using: eventLoopGroup) defer { - try? mongoClient.close().wait() + mongoClient.syncShutdown() try? eventLoopGroup.syncShutdownGracefully() } @@ -24,7 +24,7 @@ let router: Router = { router.get("kittens") { _, response, next -> Void in let res = collection.find().flatMap { cursor in - cursor.all() + cursor.toArray() } res.whenSuccess { results in From 84961d5079d292ec7b016b08309062b2344e7aa7 Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Wed, 19 Feb 2020 22:42:40 -0500 Subject: [PATCH 4/6] rm Package.resolved, update examples script --- Examples/KituraExample/Package.resolved | 124 ------------------------ etc/build-examples.sh | 3 + 2 files changed, 3 insertions(+), 124 deletions(-) delete mode 100644 Examples/KituraExample/Package.resolved diff --git a/Examples/KituraExample/Package.resolved b/Examples/KituraExample/Package.resolved deleted file mode 100644 index 8babef2a1..000000000 --- a/Examples/KituraExample/Package.resolved +++ /dev/null @@ -1,124 +0,0 @@ -{ - "object": { - "pins": [ - { - "package": "Socket", - "repositoryURL": "https://github.com/IBM-Swift/BlueSocket.git", - "state": { - "branch": null, - "revision": "c46a3d41f5b2401d18bcb46d0101cdc5cd13e307", - "version": "1.0.52" - } - }, - { - "package": "SSLService", - "repositoryURL": "https://github.com/IBM-Swift/BlueSSLService.git", - "state": { - "branch": null, - "revision": "ab2c2aa2fe574969f391c80ee87746be431dee0e", - "version": "1.0.52" - } - }, - { - "package": "Kitura", - "repositoryURL": "https://github.com/IBM-Swift/Kitura", - "state": { - "branch": null, - "revision": "bbb7462ac3d422cad4480a57ddbd33263c381a7b", - "version": "2.9.1" - } - }, - { - "package": "Kitura-NIO", - "repositoryURL": "https://github.com/IBM-Swift/Kitura-NIO.git", - "state": { - "branch": null, - "revision": "69bd82be54207512bee5baf374f461fe83190b2e", - "version": "2.4.0" - } - }, - { - "package": "Kitura-TemplateEngine", - "repositoryURL": "https://github.com/IBM-Swift/Kitura-TemplateEngine.git", - "state": { - "branch": null, - "revision": "d62d74bca48c6fb76f9fc1f48eeb2d7af415e80b", - "version": "2.0.1" - } - }, - { - "package": "KituraContracts", - "repositoryURL": "https://github.com/IBM-Swift/KituraContracts.git", - "state": { - "branch": null, - "revision": "a30e2fb79e926672776a05ec6b919c239870a221", - "version": "1.2.1" - } - }, - { - "package": "LoggerAPI", - "repositoryURL": "https://github.com/IBM-Swift/LoggerAPI.git", - "state": { - "branch": null, - "revision": "3357dd9526cdf9436fa63bb792b669e6efdc43da", - "version": "1.9.0" - } - }, - { - "package": "MongoSwift", - "repositoryURL": "https://github.com/mongodb/mongo-swift-driver", - "state": { - "branch": "master", - "revision": "1c72db375d9762244974a2bf64509eb514062fa9", - "version": null - } - }, - { - "package": "Nimble", - "repositoryURL": "https://github.com/Quick/Nimble.git", - "state": { - "branch": null, - "revision": "f8657642dfdec9973efc79cc68bcef43a653a2bc", - "version": "8.0.2" - } - }, - { - "package": "swift-log", - "repositoryURL": "https://github.com/apple/swift-log.git", - "state": { - "branch": null, - "revision": "74d7b91ceebc85daf387ebb206003f78813f71aa", - "version": "1.2.0" - } - }, - { - "package": "swift-nio-extras", - "repositoryURL": "https://github.com/apple/swift-nio-extras.git", - "state": { - "branch": null, - "revision": "b4dbfacff47fb8d0f9e0a422d8d37935a9f10570", - "version": "1.4.0" - } - }, - { - "package": "swift-nio-ssl", - "repositoryURL": "https://github.com/apple/swift-nio-ssl.git", - "state": { - "branch": null, - "revision": "af46d9b58fafbb76f9b01177568d435a1b024f99", - "version": "2.6.2" - } - }, - { - "package": "TypeDecoder", - "repositoryURL": "https://github.com/IBM-Swift/TypeDecoder.git", - "state": { - "branch": null, - "revision": "a5978582981f7151594bdaf5fe0d3cd9b1d2d0c0", - "version": "1.3.4" - } - } - ] - }, - "version": 1 -} diff --git a/etc/build-examples.sh b/etc/build-examples.sh index 4fabebf78..9cfc803d6 100755 --- a/etc/build-examples.sh +++ b/etc/build-examples.sh @@ -15,6 +15,9 @@ for example_project in ${examples[@]}; do # don't exit on failure set +e + if [ ${example_project} == "KituraExample" ]; then + export KITURA_NIO=1 + fi swift build build_success=$? set -e From 25334511a0011915dc3045c75eaec7f81ae20b05 Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Wed, 19 Feb 2020 23:18:35 -0500 Subject: [PATCH 5/6] explicitly add NIO dependency, add comments --- Examples/KituraExample/Package.swift | 5 +++-- Examples/KituraExample/Sources/KituraExample/main.swift | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Examples/KituraExample/Package.swift b/Examples/KituraExample/Package.swift index a17d0801e..e5885a662 100644 --- a/Examples/KituraExample/Package.swift +++ b/Examples/KituraExample/Package.swift @@ -5,9 +5,10 @@ let package = Package( name: "KituraExample", dependencies: [ .package(url: "https://github.com/IBM-Swift/Kitura", .upToNextMajor(from: "2.9.1")), - .package(url: "https://github.com/mongodb/mongo-swift-driver", .branch("master")) + .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: "KituraExample", dependencies: ["Kitura", "MongoSwift"]) + .target(name: "KituraExample", dependencies: ["Kitura", "MongoSwift", "NIO"]) ] ) diff --git a/Examples/KituraExample/Sources/KituraExample/main.swift b/Examples/KituraExample/Sources/KituraExample/main.swift index 2ed555b7b..e8c800a28 100644 --- a/Examples/KituraExample/Sources/KituraExample/main.swift +++ b/Examples/KituraExample/Sources/KituraExample/main.swift @@ -8,10 +8,13 @@ struct Kitten: Codable { var color: String } +// Create a single EventLoopGroup for Kitura and the MongoClient to share. let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 4) let mongoClient = try MongoClient(using: eventLoopGroup) + defer { mongoClient.syncShutdown() + cleanupMongoSwift() try? eventLoopGroup.syncShutdownGracefully() } @@ -43,5 +46,7 @@ let router: Router = { }() let server = Kitura.addHTTPServer(onPort: 8080, with: router) +// Use the EventLoopGroup created above for the Kitura server. To call this method we must build with +// `export KITURA_NIO=1 && swift build`. try server.setEventLoopGroup(eventLoopGroup) Kitura.run() From 0a775631ee80fe382365006e1b8221b83cdcfea3 Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Fri, 21 Feb 2020 14:21:24 -0500 Subject: [PATCH 6/6] Update Package.swift --- Examples/KituraExample/Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/KituraExample/Package.swift b/Examples/KituraExample/Package.swift index e5885a662..e0fccbce0 100644 --- a/Examples/KituraExample/Package.swift +++ b/Examples/KituraExample/Package.swift @@ -5,7 +5,7 @@ let package = Package( name: "KituraExample", dependencies: [ .package(url: "https://github.com/IBM-Swift/Kitura", .upToNextMajor(from: "2.9.1")), - .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: [