Struct apollo_framework::config::Config [−][src]
pub struct Config { /* fields omitted */ }
Provides access to the system configuration.
Most probably a config instance is installed by the Builder and
can be obtained via platform.require::<Config>()
. Note that it is highly recommended to
register a change listener by calling Config::notifier()
as we expect all components to pick
up config changes without restarting the application.
Implementations
impl Config
[src]
impl Config
[src]pub fn new(file: &str) -> Self
[src]
pub fn new(file: &str) -> Self
[src]Creates a new config reading the given file.
Note that this will not install a change listener. This is only done by the install function.
pub fn notifier(&self) -> ChangeNotifier
[src]
pub fn notifier(&self) -> ChangeNotifier
[src]Obtains a change notifier which receives a message once the config changed.
pub fn current(&self) -> Handle
[src]
pub fn current(&self) -> Handle
[src]Obtains a handle to the currently loaded configuration.
Note that this is a fairly efficient operation but still provides some overhead. Therefore this shouldn’t be placed in an inner loop.
pub async fn load(&self) -> Result<()>
[src]
pub async fn load(&self) -> Result<()>
[src]Forces the config to read the underlying file.
Note that this is normally called by the framework and should not be invoked manually.
pub async fn store(&self, config: &str) -> Result<()>
[src]
pub async fn store(&self, config: &str) -> Result<()>
[src]Loads a configuration from the given string instead of a file.
This is intended to be used in test environments where we cannot / do not want to load a config file from disk.
Example
let config = Config::new("apollo_test_config.yml"); // Remove any left over file... std::fs::remove_file("apollo_test_config.yml"); // Write a config file... assert_eq!(config.store(" server: port: 12345 ").await.is_ok(), true); // Load it back and verify its contents (in a fully running Apollo, this would // happen automatically via the config watcher...) assert_eq!(config.load().await.is_ok(), true); assert_eq!(config.current().config()["server"]["port"].as_i64().unwrap(), 12345); // Writing an invalid config file is prevented... assert_eq!(config.store("server: \"test").await.is_err(), true); // Therefore the original config is still present... assert_eq!(config.load().await.is_ok(), true); assert_eq!(config.current().config()["server"]["port"].as_i64().unwrap(), 12345);
pub fn load_from_string(
&self,
data: &str,
last_modified: Option<SystemTime>
) -> Result<()>
[src]
pub fn load_from_string(
&self,
data: &str,
last_modified: Option<SystemTime>
) -> Result<()>
[src]Loads a configuration from the given string instead of a file.
This is intended to be used in test environments where we cannot / do not want to load a config file from disk.
Example
use std::time::Instant; let config = Config::new("somefile.yml"); config.load_from_string(" server: port: 12345 ", None); assert_eq!(config.current().config()["server"]["port"].as_i64().unwrap(), 12345);