-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Yisaer/configure_broker
feat: support config
- Loading branch information
Showing
6 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[broker] | ||
address = "127.0.0.1" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use once_cell::sync::OnceCell; | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct Config { | ||
pub broker: BrokerConfig, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct BrokerConfig { | ||
pub address: String, | ||
} | ||
|
||
pub fn load_config(path: &str) -> Config { | ||
let config_str = std::fs::read_to_string(path).expect("Unable to read config file"); | ||
toml::from_str(&config_str).expect("Unable to parse TOML") | ||
} | ||
|
||
static CONFIG: OnceCell<Config> = OnceCell::new(); | ||
|
||
pub fn initialize_config() { | ||
let config = load_config("./config.toml"); | ||
CONFIG.set(config).unwrap(); | ||
} | ||
|
||
pub fn get_config() -> &'static Config { | ||
CONFIG.get().expect("Config is not initialized") | ||
} |
This file contains 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
This file contains 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