pub struct Config { /* private fields */ }
Expand description
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§
source§impl Config
impl Config
sourcepub fn new(file: &str) -> Self
pub fn new(file: &str) -> Self
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.
sourcepub fn notifier(&self) -> ChangeNotifier
pub fn notifier(&self) -> ChangeNotifier
Obtains a change notifier which receives a message once the config changed.
sourcepub fn current(&self) -> Handle
pub fn current(&self) -> Handle
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.
sourcepub async fn load(&self) -> Result<()>
pub async fn load(&self) -> Result<()>
Forces the config to read the underlying file.
Note that this is normally called by the framework and should not be invoked manually.
sourcepub async fn store(&self, config: &str) -> Result<()>
pub async fn store(&self, config: &str) -> Result<()>
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("test_config.yml");
// Remove any left over file...
std::fs::remove_file("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);
sourcepub fn load_from_string(
&self,
data: &str,
last_modified: Option<SystemTime>
) -> Result<()>
pub fn load_from_string( &self, data: &str, last_modified: Option<SystemTime> ) -> Result<()>
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);
Auto Trait Implementations§
impl !Freeze for Config
impl !RefUnwindSafe for Config
impl Send for Config
impl Sync for Config
impl Unpin for Config
impl !UnwindSafe for Config
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more