diff --git a/Examples/.swiftlint.yml b/Examples/.swiftlint.yml index 629b92772..6d8f25e1b 100644 --- a/Examples/.swiftlint.yml +++ b/Examples/.swiftlint.yml @@ -1,2 +1,3 @@ disabled_rules: - explicit_acl + - nesting diff --git a/Examples/VaporExample/Package.swift b/Examples/VaporExample/Package.swift index 7e3b089c3..dc34eae6f 100644 --- a/Examples/VaporExample/Package.swift +++ b/Examples/VaporExample/Package.swift @@ -1,11 +1,15 @@ -// swift-tools-version:4.2 +// swift-tools-version:5.0 import PackageDescription let package = Package( name: "VaporExample", + platforms: [ + .macOS(.v10_14) + ], dependencies: [ - .package(url: "https://github.com/vapor/vapor", .upToNextMajor(from: "3.3.0")), - .package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0")) + // The driver depends on SwiftNIO 2 and therefore is only compatible with Vapor 4. + .package(url: "https://github.com/vapor/vapor", .exact("4.0.0-beta.3.24")), + .package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0")) ], targets: [ .target(name: "VaporExample", dependencies: ["Vapor", "MongoSwift"]) diff --git a/Examples/VaporExample/README.md b/Examples/VaporExample/README.md index 30799f3dd..81a053a35 100644 --- a/Examples/VaporExample/README.md +++ b/Examples/VaporExample/README.md @@ -2,6 +2,8 @@ This is a minimal working example of using the driver in a Vapor application. +**Note**: Since the driver depends on SwiftNIO 2 as of the 1.0.0-rc0 release, it is only compatible with Vapor 4. + To test it out, do the following: 1. Run `mongod` to start MongoDB running on `localhost:27017`. 1. Navigate to the `Examples/` directory (one level up from this one.) diff --git a/Examples/VaporExample/Sources/VaporExample/configure.swift b/Examples/VaporExample/Sources/VaporExample/configure.swift new file mode 100644 index 000000000..d1ebc72d0 --- /dev/null +++ b/Examples/VaporExample/Sources/VaporExample/configure.swift @@ -0,0 +1,25 @@ +import MongoSwift +import Vapor + +extension Application { + /// A global `MongoClient` for use throughout the application. + var mongoClient: MongoClient { + get { + return self.storage[MongoClientKey.self]! + } + set { + self.storage[MongoClientKey.self] = newValue + } + } + + private struct MongoClientKey: StorageKey { + typealias Value = MongoClient + } +} + +func configure(_ app: Application) throws { + // Initialize a client using the application's EventLoopGroup. + let client = try MongoClient(using: app.eventLoopGroup) + app.mongoClient = client + try routes(app) +} diff --git a/Examples/VaporExample/Sources/VaporExample/main.swift b/Examples/VaporExample/Sources/VaporExample/main.swift index d078f9ec5..453625ae0 100644 --- a/Examples/VaporExample/Sources/VaporExample/main.swift +++ b/Examples/VaporExample/Sources/VaporExample/main.swift @@ -1,26 +1,17 @@ import MongoSwift import Vapor -/// A Codable type that matches the data in our home.kittens collection. -private struct Kitten: Content { - var name: String - var color: String -} - -private let app = try Application() -private let router = try app.make(Router.self) +var env = try Environment.detect() +try LoggingSystem.bootstrap(from: &env) -/// 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 app = Application(env) -router.get("kittens") { _ -> [Kitten] in - let cursor = try collection.find() - let results = Array(cursor) - if let error = cursor.error { - throw error - } - return results +defer { + // shut down the client and clean up the driver's global resources. + app.mongoClient.syncShutdown() + cleanupMongoSwift() + app.shutdown() } +try configure(app) try app.run() diff --git a/Examples/VaporExample/Sources/VaporExample/routes.swift b/Examples/VaporExample/Sources/VaporExample/routes.swift new file mode 100644 index 000000000..92d8dfc13 --- /dev/null +++ b/Examples/VaporExample/Sources/VaporExample/routes.swift @@ -0,0 +1,20 @@ +import MongoSwift +import Vapor + +/// A Codable type that matches the data in our home.kittens collection. +struct Kitten: Content { + var name: String + var color: String +} + +func routes(_ app: Application) throws { + /// A 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 = app.mongoClient.db("home").collection("kittens", withType: Kitten.self) + + app.get("kittens") { _ -> EventLoopFuture<[Kitten]> in + collection.find().flatMap { cursor in + cursor.toArray() + } + } +}