Skip to content

Commit ac76f89

Browse files
committed
Use Droplet.log for logging
1 parent babef6c commit ac76f89

7 files changed

+50
-44
lines changed

Sources/App/Collections/VanityCollection.swift

+6-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ private extension Field {
2323
final class VanityCollection: RouteCollection {
2424
typealias Wrapped = HTTP.Responder
2525

26+
let droplet: Droplet
2627
let apns: VaporAPNS
2728
let updatePassword: String?
28-
29-
init(apns: VaporAPNS, updatePassword: String?) {
30-
self.apns = apns
31-
self.updatePassword = updatePassword
29+
30+
init(droplet: Droplet) {
31+
self.droplet = droplet
32+
self.apns = try! droplet.vaporAPNS()
33+
self.updatePassword = try! drop.config.extract("app", "updatePassword") as String?
3234
}
3335

3436
func isAuthenticated(request: Request) -> Bool {

Sources/App/Collections/WalletCollection.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import Routing
44
import Storage
55
import Vapor
66

7-
final class WalletCollection: RouteCollection, EmptyInitializable {
7+
final class WalletCollection: RouteCollection {
88
typealias Wrapped = HTTP.Responder
99

10-
init() {
10+
let droplet: Droplet
11+
12+
init(droplet: Droplet) {
13+
self.droplet = droplet
1114
}
1215

1316
func findPass(passTypeIdentifier: String, serialNumber: String) throws -> Pass? {
@@ -86,7 +89,7 @@ final class WalletCollection: RouteCollection, EmptyInitializable {
8689

8790
func log(messages: [String]) {
8891
for message in messages {
89-
print(message)
92+
droplet.log.info(message)
9093
}
9194
}
9295

Sources/App/Database.swift

-15
This file was deleted.

Sources/App/Droplet+Database.swift

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import FluentPostgreSQL
2+
import Vapor
3+
4+
extension Droplet {
5+
func postgresDatabase() -> Database {
6+
let postgresConfig = config["database", "postgres"]
7+
8+
let host = postgresConfig?["host"]?.string ?? "localhost"
9+
let port = postgresConfig?["port"]?.int ?? 5432
10+
let dbname = postgresConfig?["dbname"]?.string ?? "passcards"
11+
let user = postgresConfig?["user"]?.string ?? ""
12+
let password = postgresConfig?["password"]?.string ?? ""
13+
14+
let postgres = PostgreSQLDriver(host: host, port: port, dbname: dbname, user: user, password: password)
15+
return Database(postgres)
16+
}
17+
}

Sources/App/Droplet+VaporAPNS.swift

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Vapor
2+
import VaporAPNS
3+
4+
extension Droplet {
5+
func vaporAPNS() throws -> VaporAPNS {
6+
let apns = try config.extract("app", "apns") as Config
7+
let topic = try apns.extract("topic") as String
8+
let teamID = try apns.extract("teamID") as String
9+
let keyID = try apns.extract("keyID") as String
10+
let rawPrivateKey = try apns.extract("privateKey") as String
11+
guard let (privateKey, publicKey) = ECKeys(from: rawPrivateKey) else {
12+
throw TokenError.invalidAuthKey
13+
}
14+
15+
let options = try Options(topic: topic, teamId: teamID, keyId: keyID, rawPrivKey: privateKey, rawPubKey: publicKey)
16+
return try VaporAPNS(options: options)
17+
}
18+
}

Sources/App/VaporAPNS.swift

-16
This file was deleted.

Sources/App/main.swift

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ import Storage
33
import Vapor
44

55
let drop = Droplet()
6-
drop.database = database(for: drop)
6+
drop.database = drop.postgresDatabase()
77
drop.preparations = [Pass.self, Registration.self]
88

99
try drop.addProvider(StorageProvider.self)
1010

11-
drop.collection(WalletCollection.self)
12-
13-
let apns = try vaporAPNS(for: drop)
14-
let updatePassword = try drop.config.extract("app", "updatePassword") as String?
15-
drop.collection(VanityCollection(apns: apns, updatePassword: updatePassword))
11+
drop.collection(WalletCollection(droplet: drop))
12+
drop.collection(VanityCollection(droplet: drop))
1613

1714
drop.get { req in
1815
return try drop.view.make("welcome")

0 commit comments

Comments
 (0)