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..e0fccbce0 100644 --- a/Examples/KituraExample/Package.swift +++ b/Examples/KituraExample/Package.swift @@ -1,13 +1,14 @@ -// 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", .upToNextMajor(from: "1.0.0")), + .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/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..e8c800a28 100644 --- a/Examples/KituraExample/Sources/KituraExample/main.swift +++ b/Examples/KituraExample/Sources/KituraExample/main.swift @@ -1,30 +1,52 @@ import Kitura 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) +// Create a single EventLoopGroup for Kitura and the MongoClient to share. +let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 4) +let mongoClient = try MongoClient(using: eventLoopGroup) -private let router: Router = { +defer { + mongoClient.syncShutdown() + cleanupMongoSwift() + try? eventLoopGroup.syncShutdownGracefully() +} + +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 + let res = collection.find().flatMap { cursor in + cursor.toArray() + } + + res.whenSuccess { results in + response.send(results) + next() + } + + res.whenFailure { error in + response.error = error + response.send("Error: \(error)") + next() } - response.send(results) } return router }() -Kitura.addHTTPServer(onPort: 8080, with: 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() 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