Skip to content

Commit

Permalink
Use embedded defaults when iggy-server config file is absent (#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcio authored Nov 27, 2024
1 parent 5179a14 commit 7b27a45
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ The official images can be found [here](https://hub.docker.com/r/iggyrs/iggy), s

## Configuration

The default configuration can be found in `server.toml` (the default one) or `server.json` file in `configs` directory.
The default configuration can be found in `server.toml` file in `configs` directory.

The configuration file is loaded from the current working directory, but you can specify the path to the configuration file by setting `IGGY_CONFIG_PATH` environment variable, for example `export IGGY_CONFIG_PATH=configs/server.json` (or other command depending on OS).
The configuration file is loaded from the current working directory, but you can specify the path to the configuration file by setting `IGGY_CONFIG_PATH` environment variable, for example `export IGGY_CONFIG_PATH=configs/server.toml` (or other command depending on OS).

When config file is not found, the default values from embedded server.toml file are used.

For the detailed documentation of the configuration file, please refer to the [configuration](https://docs.iggy.rs/server/configuration) section.

Expand Down Expand Up @@ -226,6 +228,7 @@ Iggy comes with the Rust SDK, which is available on [crates.io](https://crates.i
The SDK provides both, low-level client for the specific transport, which includes the message sending and polling along with all the administrative actions such as managing the streams, topics, users etc., as well as the high-level client, which abstracts the low-level details and provides the easy-to-use API for both, message producers and consumers.

You can find the more examples, including the multi-tenant one under the `examples` directory.

```rust
// Create the Iggy client
let client = IggyClient::from_connection_string("iggy://user:secret@localhost:8090")?;
Expand All @@ -246,17 +249,17 @@ producer.send(messages).await?;

// Create a consumer for the given stream and one of its topics
let mut consumer = client
.consumer_group("my_app", "dev01", "events")?
.auto_commit(AutoCommit::IntervalOrWhen(
IggyDuration::from_str("1s")?,
AutoCommitWhen::ConsumingAllMessages,
))
.create_consumer_group_if_not_exists()
.auto_join_consumer_group()
.polling_strategy(PollingStrategy::next())
.poll_interval(IggyDuration::from_str("1ms")?)
.batch_size(1000)
.build();
.consumer_group("my_app", "dev01", "events")?
.auto_commit(AutoCommit::IntervalOrWhen(
IggyDuration::from_str("1s")?,
AutoCommitWhen::ConsumingAllMessages,
))
.create_consumer_group_if_not_exists()
.auto_join_consumer_group()
.polling_strategy(PollingStrategy::next())
.poll_interval(IggyDuration::from_str("1ms")?)
.batch_size(1000)
.build();

consumer.init().await?;

Expand Down
2 changes: 1 addition & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "server"
version = "0.4.81"
version = "0.4.82"
edition = "2021"
build = "src/build.rs"

Expand Down
31 changes: 21 additions & 10 deletions server/src/configs/config_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,21 +257,32 @@ impl ConfigProvider for FileConfigProvider {
async fn load_config(&self) -> Result<ServerConfig, ServerError> {
println!("Loading config from path: '{}'...", self.path);

if !file_exists(&self.path) {
return Err(ServerError::CannotLoadConfiguration(format!(
"Cannot find configuration file at path: '{}'.",
self.path,
)));
// Include the default configuration from server.toml
let embedded_default_config = Toml::string(include_str!("../../../configs/server.toml"));

// Start with the default configuration
let mut config_builder = Figment::new().merge(embedded_default_config);

// If the server.toml file exists, merge it into the configuration
if file_exists(&self.path) {
println!("Found configuration file at path: '{}'.", self.path);
config_builder = config_builder.merge(Toml::file(&self.path));
} else {
println!(
"Configuration file not found at path: '{}'. Using default configuration from embedded server.toml.",
self.path
);
}

let config_builder = Figment::new().merge(Toml::file(&self.path));
let custom_env_provider = CustomEnvProvider::new("IGGY_");
let config_result: Result<ServerConfig, figment::Error> =
config_builder.merge(custom_env_provider).extract();
// Merge environment variables into the configuration
config_builder = config_builder.merge(CustomEnvProvider::new("IGGY_"));

// Finally, attempt to extract the final configuration
let config_result: Result<ServerConfig, figment::Error> = config_builder.extract();

match config_result {
Ok(config) => {
println!("Config loaded from path: '{}'", self.path);
println!("Config loaded successfully.");
println!("Using Config: {config}");
Ok(config)
}
Expand Down

0 comments on commit 7b27a45

Please sign in to comment.