Skip to content

Commit

Permalink
Merge pull request #7 from Yisaer/configure_broker
Browse files Browse the repository at this point in the history
feat: support config
  • Loading branch information
Yisaer authored Aug 21, 2024
2 parents fbd117e + a65b2ff commit 708cc96
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pretty_env_logger = "0.4"
serde_json = "1.0"
prometheus = "0.13"
sysinfo = "0.26"
toml = "0.5"
once_cell = "1.8"

[dev-dependencies]
sqllogictest = "0.13.0"
2 changes: 2 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[broker]
address = "127.0.0.1"
28 changes: 28 additions & 0 deletions src/config/mod.rs
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")
}
4 changes: 3 additions & 1 deletion src/connector/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config;
use rumqttc::{AsyncClient, EventLoop, MqttOptions};

pub struct MqttClient {
Expand All @@ -7,7 +8,8 @@ pub struct MqttClient {

impl MqttClient {
pub fn new(client_id: &String) -> Self {
let mqtt_options = MqttOptions::new(client_id, "127.0.0.1", 1883);
let config = config::get_config();
let mqtt_options = MqttOptions::new(client_id, &config.broker.address, 1883);
let (client, event_loop) = AsyncClient::new(mqtt_options, 10);
MqttClient { client, event_loop }
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use util::SimpleLogger;
use crate::connector::MqttClient;

mod catalog;
mod config;
mod connector;
mod core;
mod sql;
Expand Down Expand Up @@ -155,6 +156,7 @@ async fn metrics_handler(extract::State(state): extract::State<Arc<Mutex<AppStat

#[tokio::main]
pub async fn main() {
config::initialize_config();
log::set_logger(&LOGGER)
.map(|()| log::set_max_level(LevelFilter::Info))
.unwrap();
Expand Down

0 comments on commit 708cc96

Please sign in to comment.