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 ?? "" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.