Skip to content

Commit

Permalink
Merge pull request #33 from Goboolean/feat/envload
Browse files Browse the repository at this point in the history
dotenv -> env! 방식 변경
  • Loading branch information
mulmuri authored Jun 14, 2024
2 parents f92b735 + 3e61a6a commit 070b05e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 32 deletions.
49 changes: 33 additions & 16 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
//! A connector that fetches data from the HTS service and dumps it into `InfluxDB`.
fn main() {
use std::env;

let influxdb_url = env::var("INFLUXDB_URL");
let influxdb_bucket = env::var("INFLUXDB_BUCKET");
let influxdb_token = env::var("INFLUXDB_TOKEN");
let influxdb_org = env::var("INFLUXDB_ORG");
let text_file_path = env::var("TEXT_FILE_PATH");

#[cfg(not(debug_assertions))]
{
use dotenv::dotenv;
use std::env;

dotenv().expect("Failed to read .env file");

let influxdb_url = env::var("INFLUXDB_URL").expect("INFLUX_URL must be set");
let influxdb_bucket = env::var("INFLUXDB_BUCKET").expect("INFLUX_BUCKET must be set");
let influxdb_token = env::var("INFLUXDB_TOKEN").expect("INFLUX_TOKEN must be set");
let influxdb_org = env::var("INFLUXDB_ORG").expect("INFLUX_ORG must be set");
let text_file_path = env::var("TEXT_FILE_PATH").expect("TEXT_FILE_PATH must be set");

println!("cargo:rustc-env=INFLUXDB_URL={}", influxdb_url);
println!("cargo:rustc-env=INFLUXDB_BUCKET={}", influxdb_bucket);
println!("cargo:rustc-env=INFLUXDB_TOKEN={}", influxdb_token);
println!("cargo:rustc-env=INFLUXDB_ORG={}", influxdb_org);
println!("cargo:rustc-env=TEXT_FILE_PATH={}", text_file_path);
if influxdb_url.is_err() {
panic!("INFLUXDB_URL must be set");
}

if influxdb_bucket.is_err() {
panic!("INFLUXDB_BUCKET must be set");
}

if influxdb_token.is_err() {
panic!("INFLUXDB_TOKEN must be set");
}

if influxdb_org.is_err() {
panic!("INFLUXDB_ORG must be set");
}

if text_file_path.is_err() {
panic!("TEXT_FILE_PATH must be set");
}
}

println!("cargo:rustc-env=INFLUXDB_URL={}", influxdb_url.unwrap_or_default());
println!("cargo:rustc-env=INFLUXDB_BUCKET={}", influxdb_bucket.unwrap_or_default());
println!("cargo:rustc-env=INFLUXDB_TOKEN={}", influxdb_token.unwrap_or_default());
println!("cargo:rustc-env=INFLUXDB_ORG={}", influxdb_org.unwrap_or_default());
println!("cargo:rustc-env=TEXT_FILE_PATH={}", text_file_path.unwrap_or_default());
}
33 changes: 25 additions & 8 deletions src/influx/config.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
use dotenv::dotenv;
use envconfig::Envconfig;
use serde::Deserialize;

use std::error::Error;
use std::env;

#[derive(Debug, Deserialize, Envconfig)]
pub struct Config {
#[envconfig(from = "INFLUXDB_URL")]
pub url: String,
#[envconfig(from = "INFLUXDB_TOKEN")]
pub token: String,
#[envconfig(from = "INFLUXDB_ORG")]
pub org: String,
#[envconfig(from = "INFLUXDB_BUCKET")]
pub bucket: String,
}

impl Config {
pub fn new() -> Result<Self, envconfig::Error> {
dotenv().ok();
Self::init_from_env()
fn retrieve_env_var(key: &str) -> Result<String, Box<dyn Error>> {
env::var(key).map_err(|e| {
Box::new(e) as Box<dyn Error>
})
}

pub fn new() -> Result<Self, Box<dyn Error>> {
Ok(Self {
url: Self::retrieve_env_var("INFLUXDB_URL")?,
token: Self::retrieve_env_var("INFLUXDB_TOKEN")?,
org: Self::retrieve_env_var("INFLUXDB_ORG")?,
bucket: Self::retrieve_env_var("INFLUXDB_BUCKET")?,
})
}

pub fn init() -> Result<Self, Box<dyn Error>> {
Ok(Self {
url: env!("INFLUXDB_URL").to_string(),
token: env!("INFLUXDB_TOKEN").to_string(),
org: env!("INFLUXDB_ORG").to_string(),
bucket: env!("INFLUXDB_BUCKET").to_string(),
})
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ use std::time::Duration;
use tokio::runtime::Runtime;

fn main() {
let path: &'static str = env!("INFLUXDB_URL");
println!("the $PATH variable at the time of compiling was: {path}");

let runtime = Runtime::new().expect("Failed to create runtime");

let client = runtime.block_on(async {
let config = InfluxConfig::new().expect("Failed to create config");
let config = InfluxConfig::init().expect("Failed to create config");
InfluxClient::new(config)
.await
.expect("Failed to create client")
});
let adapter = InfluxHandler::new(client);

let config = TextConfig::new().expect("Failed to create config");
let config = TextConfig::init().expect("Failed to create config");
let reader = TextReader::new(config, Box::new(adapter)).expect("Failed to create reader");

let one_day = Duration::from_secs(24 * 60 * 60);
Expand Down
26 changes: 20 additions & 6 deletions src/text/config.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
use dotenv::dotenv;
use envconfig::Envconfig;
use serde::Deserialize;
use std::error::Error;
use std::env;


#[derive(Debug, Deserialize, Envconfig)]
pub struct Config {
#[envconfig(from = "TEXT_FILE_PATH")]
pub path: String,
}

impl Config {
pub fn new() -> Result<Self, envconfig::Error> {
dotenv().ok();
Self::init_from_env()
fn retrieve_env_var(key: &str) -> Result<String, Box<dyn Error>> {
env::var(key).map_err(|e| {
Box::new(e) as Box<dyn Error>
})
}

pub fn new() -> Result<Self, Box<dyn Error>> {
Ok(Self {
path: Self::retrieve_env_var("TEXT_FILE_PATH")?,
})
}

pub fn init() -> Result<Self, Box<dyn Error>> {
Ok(Self {
path: env!("TEXT_FILE_PATH").to_string(),
})
}
}

Expand All @@ -27,7 +41,7 @@ mod tests {
env::remove_var("TEXT_FILE_PATH");

// Act
let config: Result<Config, envconfig::Error> = Config::new();
let config = Config::new();

// Assert
assert!(config.is_err());
Expand Down
3 changes: 3 additions & 0 deletions src/text/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ impl Reader {
} {
match (parse_candle(line.clone()), parse_indicator(line.clone())) {
(Ok(candle), _) => {
println!("{:?}", candle);
self.handler.handle_candle(candle)?;
}
(_, Ok(indicator)) => {
println!("{:?}", indicator);
self.handler.handle_indicator(indicator)?;
}
(Err(_), Err(_)) => {
println!("Failed to parse line: {}", line.trim_end());
}
}
}
Expand Down

0 comments on commit 070b05e

Please sign in to comment.