From 95a75de736aa50b6b5d8c319028f12bbade50cbf Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Tue, 4 Feb 2020 15:07:10 -0500 Subject: [PATCH 1/5] vapor 4 example working --- Examples/.swiftlint.yml | 1 + Examples/VaporExample/Package.swift | 9 ++++-- .../Sources/VaporExample/configure.swift | 23 +++++++++++++++ .../Sources/VaporExample/main.swift | 29 +++++-------------- .../Sources/VaporExample/routes.swift | 20 +++++++++++++ 5 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 Examples/VaporExample/Sources/VaporExample/configure.swift create mode 100644 Examples/VaporExample/Sources/VaporExample/routes.swift 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..35ddadfb5 100644 --- a/Examples/VaporExample/Package.swift +++ b/Examples/VaporExample/Package.swift @@ -1,11 +1,14 @@ -// 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")) + .package(url: "https://github.com/vapor/vapor", .exact("4.0.0-beta.3.24")), + .package(url: "https://github.com/mongodb/mongo-swift-driver", .branch("master")) ], targets: [ .target(name: "VaporExample", dependencies: ["Vapor", "MongoSwift"]) diff --git a/Examples/VaporExample/Sources/VaporExample/configure.swift b/Examples/VaporExample/Sources/VaporExample/configure.swift new file mode 100644 index 000000000..caa5068fa --- /dev/null +++ b/Examples/VaporExample/Sources/VaporExample/configure.swift @@ -0,0 +1,23 @@ +import MongoSwift +import Vapor + +extension 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 { + 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..7786d6ccd 100644 --- a/Examples/VaporExample/Sources/VaporExample/main.swift +++ b/Examples/VaporExample/Sources/VaporExample/main.swift @@ -1,26 +1,11 @@ -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 +var env = try Environment.detect() +try LoggingSystem.bootstrap(from: &env) +let app = Application(env) +defer { + app.mongoClient.syncShutdown() + app.shutdown() } - -private let app = try Application() -private let router = try app.make(Router.self) - -/// 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) - -router.get("kittens") { _ -> [Kitten] in - let cursor = try collection.find() - let results = Array(cursor) - if let error = cursor.error { - throw error - } - return results -} - +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() + } + } +} From 3acc24bc9284b5ea8fb24176ceea53faa202ec7f Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Wed, 19 Feb 2020 23:06:19 -0500 Subject: [PATCH 2/5] comments, line breaks --- Examples/VaporExample/Sources/VaporExample/configure.swift | 2 ++ Examples/VaporExample/Sources/VaporExample/main.swift | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/Examples/VaporExample/Sources/VaporExample/configure.swift b/Examples/VaporExample/Sources/VaporExample/configure.swift index caa5068fa..d1ebc72d0 100644 --- a/Examples/VaporExample/Sources/VaporExample/configure.swift +++ b/Examples/VaporExample/Sources/VaporExample/configure.swift @@ -2,6 +2,7 @@ import MongoSwift import Vapor extension Application { + /// A global `MongoClient` for use throughout the application. var mongoClient: MongoClient { get { return self.storage[MongoClientKey.self]! @@ -17,6 +18,7 @@ extension Application { } 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 7786d6ccd..1afba133a 100644 --- a/Examples/VaporExample/Sources/VaporExample/main.swift +++ b/Examples/VaporExample/Sources/VaporExample/main.swift @@ -2,10 +2,15 @@ import Vapor var env = try Environment.detect() try LoggingSystem.bootstrap(from: &env) + let app = Application(env) + 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() From a604202d96a1d71de0baf27d7676f2157f9b8ba9 Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Wed, 19 Feb 2020 23:08:05 -0500 Subject: [PATCH 3/5] add import --- Examples/VaporExample/Sources/VaporExample/main.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Examples/VaporExample/Sources/VaporExample/main.swift b/Examples/VaporExample/Sources/VaporExample/main.swift index 1afba133a..453625ae0 100644 --- a/Examples/VaporExample/Sources/VaporExample/main.swift +++ b/Examples/VaporExample/Sources/VaporExample/main.swift @@ -1,3 +1,4 @@ +import MongoSwift import Vapor var env = try Environment.detect() From 6f1b49139630c517c2309b8c30de7c0a9d94d4f1 Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Wed, 19 Feb 2020 23:19:26 -0500 Subject: [PATCH 4/5] format --- Examples/VaporExample/Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/VaporExample/Package.swift b/Examples/VaporExample/Package.swift index 35ddadfb5..634a1bb24 100644 --- a/Examples/VaporExample/Package.swift +++ b/Examples/VaporExample/Package.swift @@ -4,7 +4,7 @@ import PackageDescription let package = Package( name: "VaporExample", platforms: [ - .macOS(.v10_14) + .macOS(.v10_14) ], dependencies: [ .package(url: "https://github.com/vapor/vapor", .exact("4.0.0-beta.3.24")), From 9e18aa1cc093d99a9859153c86f6f48b61d8a20f Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Fri, 21 Feb 2020 14:30:18 -0500 Subject: [PATCH 5/5] Add comments re: Vapor 4 support --- Examples/VaporExample/Package.swift | 3 ++- Examples/VaporExample/README.md | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Examples/VaporExample/Package.swift b/Examples/VaporExample/Package.swift index 634a1bb24..dc34eae6f 100644 --- a/Examples/VaporExample/Package.swift +++ b/Examples/VaporExample/Package.swift @@ -7,8 +7,9 @@ let package = Package( .macOS(.v10_14) ], dependencies: [ + // 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", .branch("master")) + .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.)