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
5 changes: 3 additions & 2 deletions Examples/PerfectExample/Package.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// swift-tools-version:4.2
// swift-tools-version:5.0
import PackageDescription

let package = Package(
name: "PerfectExample",
dependencies: [
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"),
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0")),
.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: "PerfectExample", dependencies: ["PerfectHTTPServer", "MongoSwift"])
Expand Down
36 changes: 24 additions & 12 deletions Examples/PerfectExample/Sources/PerfectExample/main.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
import Foundation
import MongoSwift
import NIO
import PerfectHTTP
import PerfectHTTPServer

/// 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
}

// Create a SwiftNIO EventLoopGroup for the client to use.
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
let mongoClient = try MongoClient(using: elg)

defer {
// Close the client and clean up global driver resources.
mongoClient.syncShutdown()
cleanupMongoSwift()
// Shut down the EventLoopGroup.
try? elg.syncShutdownGracefully()
}

/// 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 collection = mongoClient.db("home").collection("kittens", withType: Kitten.self)

private var routes = Routes()
routes.add(method: .get, uri: "/kittens") { _, response in
response.setHeader(.contentType, value: "application/json")
do {
let cursor = try collection.find()
let json = try JSONEncoder().encode(Array(cursor))
if let error = cursor.error {
throw error
}
collection.find().flatMap { cursor in
cursor.toArray()
}.flatMapThrowing { results in
response.setHeader(.contentType, value: "application/json")
let json = try JSONEncoder().encode(results)
response.setBody(bytes: Array(json))
} catch {
print("error: \(error)")
response.completed()
}.whenFailure { error in
response.setBody(string: "Error: \(error)")
response.completed()
}
response.completed()
}

try HTTPServer.launch(name: "localhost", port: 8080, routes: routes)