Skip to content

Commit fcf843b

Browse files
committed
SWIFT-639 Update Kitura example (#399)
1 parent 1518297 commit fcf843b

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
// swift-tools-version:4.2
1+
// swift-tools-version:5.0
22
import PackageDescription
33

44
let package = Package(
55
name: "KituraExample",
66
dependencies: [
7-
.package(url: "https://github.com/IBM-Swift/Kitura", .upToNextMajor(from: "2.6.3")),
8-
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0"))
7+
.package(url: "https://github.com/IBM-Swift/Kitura", .upToNextMajor(from: "2.9.1")),
8+
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0")),
9+
.package(url: "https://github.com/apple/swift-nio", .upToNextMajor(from: "2.14.0"))
910
],
1011
targets: [
11-
.target(name: "KituraExample", dependencies: ["Kitura", "MongoSwift"])
12+
.target(name: "KituraExample", dependencies: ["Kitura", "MongoSwift", "NIO"])
1213
]
1314
)

Examples/KituraExample/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ To test it out, do the following:
77
1. Navigate to the `Examples/` directory (one level up from this one.)
88
1. Run `../loadExampleData.sh` to load sample data into the database.
99
1. Navigate to the root directory of this example.
10-
1. Run `swift run`.
10+
1. Build with `export KITURA_NIO=1 && swift build` to enable using SwiftNIO for the networking layer.
11+
1. Start the server with `swift run`.
1112
1. Navigate to `localhost:8080/kittens` to see the example data loaded on the web page.
Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,52 @@
11
import Kitura
22
import MongoSwift
3+
import NIO
34

45
/// A Codable type that matches the data in our home.kittens collection.
5-
private struct Kitten: Codable {
6+
struct Kitten: Codable {
67
var name: String
78
var color: String
89
}
910

10-
/// A single collection with type `Kitten`. This allows us to directly retrieve instances of
11-
/// `Kitten` from the collection. `MongoCollection` is safe to share across threads.
12-
private let collection = try MongoClient().db("home").collection("kittens", withType: Kitten.self)
11+
// Create a single EventLoopGroup for Kitura and the MongoClient to share.
12+
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 4)
13+
let mongoClient = try MongoClient(using: eventLoopGroup)
1314

14-
private let router: Router = {
15+
defer {
16+
mongoClient.syncShutdown()
17+
cleanupMongoSwift()
18+
try? eventLoopGroup.syncShutdownGracefully()
19+
}
20+
21+
let router: Router = {
1522
let router = Router()
1623

17-
router.get("kittens") { _, response, _ in
18-
let cursor = try collection.find()
19-
let results = Array(cursor)
20-
if let error = cursor.error {
21-
throw error
24+
/// A single collection with type `Kitten`. This allows us to directly retrieve instances of
25+
/// `Kitten` from the collection. `MongoCollection` is safe to share across threads.
26+
let collection = mongoClient.db("home").collection("kittens", withType: Kitten.self)
27+
28+
router.get("kittens") { _, response, next -> Void in
29+
let res = collection.find().flatMap { cursor in
30+
cursor.toArray()
31+
}
32+
33+
res.whenSuccess { results in
34+
response.send(results)
35+
next()
36+
}
37+
38+
res.whenFailure { error in
39+
response.error = error
40+
response.send("Error: \(error)")
41+
next()
2242
}
23-
response.send(results)
2443
}
2544

2645
return router
2746
}()
2847

29-
Kitura.addHTTPServer(onPort: 8080, with: router)
48+
let server = Kitura.addHTTPServer(onPort: 8080, with: router)
49+
// Use the EventLoopGroup created above for the Kitura server. To call this method we must build with
50+
// `export KITURA_NIO=1 && swift build`.
51+
try server.setEventLoopGroup(eventLoopGroup)
3052
Kitura.run()

etc/build-examples.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ for example_project in ${examples[@]}; do
1515

1616
# don't exit on failure
1717
set +e
18+
if [ ${example_project} == "KituraExample" ]; then
19+
export KITURA_NIO=1
20+
fi
1821
swift build
1922
build_success=$?
2023
set -e

0 commit comments

Comments
 (0)