Last active
October 1, 2019 13:42
-
-
Save vzsg/1bb6c215e5365d0ac7d18eeceacbbb46 to your computer and use it in GitHub Desktop.
Fetching remote configuration on app boot (Vapor 3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Dispatch | |
// Utility class for thread-safe access to an "app global" variable | |
final class DispatchBox<T>: Service { | |
private let queue = DispatchQueue(label: "DispatchBox_\(T.self)_Queue") | |
private var _value: T? | |
var value: T? { | |
get { return queue.sync { self._value }} | |
set { queue.async { self._value = newValue }} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// This sample class can be used to parse a JSON like: | |
// { "secret": "FOOBAR" } | |
struct SampleConfiguration: Decodable { | |
let secret: String | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vapor | |
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws { | |
// ... other configuration ... | |
// Creating a holder for SampleConfiguration | |
let holder = DispatchBox<SampleConfiguration>() | |
services.register(holder) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vapor | |
/// Called after your application has initialized. | |
public func boot(_ app: Application) throws { | |
let client = try app.make(Client.self) | |
let holder = try app.make(DispatchBox<SampleConfiguration>.self) | |
let config = try client.get("https://api.jsonbin.io/b/5b5f00c4f24d8943d0505d0a") | |
.flatMap { try $0.content.decode(SampleConfiguration.self) } | |
.wait() | |
holder.value = config | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Vapor | |
/// Register your application's routes here. | |
public func routes(_ router: Router) throws { | |
router.get("secret") { req -> String in | |
let holder = try req.make(DispatchBox<SampleConfiguration>.self) | |
return holder.value?.secret ?? "" | |
} | |
} |
It's an exception internal to URLSession, probably related to the jsonbin.io site's certificates.
I don't think we can do anything about it, nor that you should worry about it.
#0 0x00007fff4fc941f6 in __cxa_throw ()
#1 0x00007fff35eccf0f in Security::UnixError::throwMeNoLogging(int) ()
#2 0x00007fff35c78f18 in Security::safeCopyFile(char const*, unsigned int, char const*, unsigned short) ()
#3 0x00007fff35c77ce1 in Security::MDSSession::updateDataBases() ()
#4 0x00007fff35cc9ff5 in Security::MDSSession::DbOpen(char const*, cssm_net_address const*, unsigned int, Security::AccessCredentials const*, void const*, long&) ()
#5 0x00007fff35cc9e74 in mds_DbOpen(long, char const*, cssm_net_address const*, unsigned int, cssm_access_credentials const*, void const*, long*) ()
#6 0x00007fff35c77223 in Security::MDSClient::Directory::cdsa() const ()
#7 0x00007fff35d8a9a0 in Security::MDSClient::Directory::dlGetFirst(cssm_query const&, cssm_db_record_attribute_data&, cssm_data*, cssm_db_unique_record*&) ()
#8 0x00007fff35c76cec in Security::CssmClient::Table<Security::MDSClient::Common>::startQuery(Security::CssmQuery const&, bool) ()
#9 0x00007fff35c769e1 in Security::CssmClient::Table<Security::MDSClient::Common>::fetch(Security::CssmClient::Query const&, int) ()
#10 0x00007fff35c761bc in MdsComponent::MdsComponent(Security::Guid const&) ()
#11 0x00007fff35c75df8 in CssmManager::loadModule(Security::Guid const&, unsigned int, Security::ModuleCallback const&) ()
#12 0x00007fff35c75b97 in CSSM_ModuleLoad ()
#13 0x00007fff35c756ab in Security::CssmClient::ModuleImpl::activate() ()
#14 0x00007fff35c7545e in Security::CssmClient::AttachmentImpl::activate() ()
#15 0x00007fff35c75362 in Security::KeychainCore::Certificate::clHandle() ()
#16 0x00007fff35c9a74a in Security::KeychainCore::Certificate::copyFirstFieldValue(cssm_data const&) ()
#17 0x00007fff35c9a5e4 in Security::KeychainCore::Certificate::publicKey() ()
#18 0x00007fff35c9a4b2 in SecCertificateCopyPublicKey ()
#19 0x00007fff4fb5d2dd in boringssl_context_extract_certificates_from_x509_store ()
#20 0x00007fff4fb551a7 in _boringssl_context_SSL_ctx_verify_callback ()
#21 0x00007fff4fbcb301 in ssl_verify_cert_chain ()
#22 0x00007fff4fb6b521 in ssl3_connect ()
#23 0x00007fff4fbcdb4e in SSL_do_handshake ()
#24 0x00007fff4fb53f5c in boringssl_session_handshake_continue ()
#25 0x00007fff4fb53b0d in boringssl_context_handshake_negotiate ()
#26 0x00007fff4fb5ed51 in nw_protocol_boringssl_handshake_negotiate ()
#27 0x00000001019b9cae in _dispatch_call_block_and_release ()
#28 0x00000001019b1d8f in _dispatch_client_callout ()
#29 0x00000001019c7bbb in _dispatch_queue_serial_drain ()
#30 0x00000001019b9801 in _dispatch_queue_invoke ()
#31 0x00000001019c8fca in _dispatch_root_queue_drain_deferred_wlh ()
#32 0x00000001019cdea3 in _dispatch_workloop_worker_thread ()
#33 0x0000000101a2dfd6 in _pthread_wqthread ()
#34 0x0000000101a2dbed in start_wqthread ()
Thanks 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With
All exceptions
turned on I receive an exception with the code returning the config fromjsonbin.io
. If I turn exceptions off it all works as expected, just a little worried about what's happening there. Tried to put it into a do-catch but couldn't get it to hit that.