Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Examples/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
disabled_rules:
- explicit_acl
9 changes: 5 additions & 4 deletions Examples/KituraExample/Package.swift
Original file line number Diff line number Diff line change
@@ -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"])
]
)
3 changes: 2 additions & 1 deletion Examples/KituraExample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
46 changes: 34 additions & 12 deletions Examples/KituraExample/Sources/KituraExample/main.swift
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions etc/build-examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down