|
| 1 | +// |
| 2 | +// GoogleCloudDatastoreAPI.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Andrew Edwards on 04/10/20. |
| 6 | +// |
| 7 | + |
| 8 | +import Vapor |
| 9 | +@_exported import Datastore |
| 10 | +@_exported import GoogleCloud |
| 11 | + |
| 12 | +extension Application.GoogleCloudPlatform { |
| 13 | + private struct CloudDatastoreAPIKey: StorageKey { |
| 14 | + typealias Value = GoogleCloudDatastoreAPI |
| 15 | + } |
| 16 | + |
| 17 | + private struct CloudDatastoreConfigurationKey: StorageKey { |
| 18 | + typealias Value = GoogleCloudDatastoreConfiguration |
| 19 | + } |
| 20 | + |
| 21 | + private struct CloudDatastoreHTTPClientKey: StorageKey, LockKey { |
| 22 | + typealias Value = HTTPClient |
| 23 | + } |
| 24 | + |
| 25 | + public var datastore: GoogleCloudDatastoreAPI { |
| 26 | + get { |
| 27 | + if let existing = self.application.storage[CloudDatastoreAPIKey.self] { |
| 28 | + return existing |
| 29 | + } else { |
| 30 | + return .init(application: self.application, eventLoop: self.application.eventLoopGroup.next()) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + nonmutating set { |
| 35 | + self.application.storage[CloudDatastoreAPIKey.self] = newValue |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public struct GoogleCloudDatastoreAPI { |
| 40 | + public let application: Application |
| 41 | + public let eventLoop: EventLoop |
| 42 | + |
| 43 | + /// A client used to interact with the `GoogleCloudDatastore` API. |
| 44 | + public var client: GoogleCloudDatastoreClient { |
| 45 | + do { |
| 46 | + let new = try GoogleCloudDatastoreClient(credentials: self.application.googleCloud.credentials, |
| 47 | + config: self.configuration, |
| 48 | + httpClient: self.http, |
| 49 | + eventLoop: self.eventLoop) |
| 50 | + return new |
| 51 | + } catch { |
| 52 | + fatalError("\(error.localizedDescription)") |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// The configuration for using `GoogleCloudDatastore` APIs. |
| 57 | + public var configuration: GoogleCloudDatastoreConfiguration { |
| 58 | + get { |
| 59 | + if let configuration = application.storage[CloudDatastoreConfigurationKey.self] { |
| 60 | + return configuration |
| 61 | + } else { |
| 62 | + fatalError("Cloud datastore configuration has not been set. Use app.googleCloud.datastore.configuration = ...") |
| 63 | + } |
| 64 | + } |
| 65 | + set { |
| 66 | + if application.storage[CloudDatastoreConfigurationKey.self] == nil { |
| 67 | + application.storage[CloudDatastoreConfigurationKey.self] = newValue |
| 68 | + } else { |
| 69 | + fatalError("Attempting to override credentials configuration after being set is not allowed.") |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// Custom `HTTPClient` that ignores unclean SSL shutdown. |
| 75 | + public var http: HTTPClient { |
| 76 | + if let existing = application.storage[CloudDatastoreHTTPClientKey.self] { |
| 77 | + return existing |
| 78 | + } else { |
| 79 | + let lock = application.locks.lock(for: CloudDatastoreHTTPClientKey.self) |
| 80 | + lock.lock() |
| 81 | + defer { lock.unlock() } |
| 82 | + if let existing = application.storage[CloudDatastoreHTTPClientKey.self] { |
| 83 | + return existing |
| 84 | + } |
| 85 | + let new = HTTPClient( |
| 86 | + eventLoopGroupProvider: .shared(application.eventLoopGroup), |
| 87 | + configuration: HTTPClient.Configuration(ignoreUncleanSSLShutdown: true) |
| 88 | + ) |
| 89 | + application.storage.set(CloudDatastoreHTTPClientKey.self, to: new) { |
| 90 | + try $0.syncShutdown() |
| 91 | + } |
| 92 | + return new |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +extension Request { |
| 99 | + private struct GoogleCloudDatastoreKey: StorageKey { |
| 100 | + typealias Value = GoogleCloudDatastoreClient |
| 101 | + } |
| 102 | + |
| 103 | + /// A client used to interact with the `GoogleCloudDatastore` API |
| 104 | + public var gcDatastore: GoogleCloudDatastoreClient { |
| 105 | + if let existing = application.storage[GoogleCloudDatastoreKey.self] { |
| 106 | + return existing.hopped(to: self.eventLoop) |
| 107 | + } else { |
| 108 | + let new = Application.GoogleCloudPlatform.GoogleCloudDatastoreAPI(application: self.application, eventLoop: self.eventLoop).client |
| 109 | + application.storage[GoogleCloudDatastoreKey.self] = new |
| 110 | + return new |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
0 commit comments