Skip to content

Commit

Permalink
Add dotenv for config
Browse files Browse the repository at this point in the history
  • Loading branch information
akamya997 committed Jan 18, 2024
1 parent 406b567 commit 90e55bf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions judger/src/client/environment/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BASE_URL="http://localhost:8080/api/v1/judge"
RUST_LOG=DEBUG
31 changes: 31 additions & 0 deletions judger/src/client/environment/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use structopt::StructOpt;

#[derive(StructOpt, Debug, Clone)]
#[structopt(name = "judge-client")]
pub struct JudgeClientOpt {
#[structopt(long)]
pub env_path: Option<String>,

/// Port to listen to
#[structopt(env = "BASE_URL", default_value = "http://localhost:8080/api/v1/judge")]
pub base_url: String,
}

pub fn load_option() -> JudgeClientOpt {
// First load env_path from Args
let opt = JudgeClientOpt::from_args();
if let Some(env_path) = opt.env_path {
dotenv::from_path(env_path).ok();
} else {
dotenv::dotenv().ok();
}

// Load opt again with ENV
let opt = JudgeClientOpt::from_args();
log::debug!("load opt: {:?}", opt);
opt
}

pub fn setup_logger() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug")).init();
}
9 changes: 6 additions & 3 deletions judger/src/client/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod client;
mod environment;
mod error;

use client::HttpClient;
use error::ClientError;
use judge_core::judge;
Expand Down Expand Up @@ -47,9 +49,10 @@ struct JudgeTask {

#[tokio::main]
async fn main() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug")).init();
let opt = environment::load_option();
environment::setup_logger();
let mut interval = interval(Duration::from_secs(10));
let base_url = "http://localhost:8080/api/v1/judge".to_string();
let base_url = opt.base_url;
let client = client::HttpClient::new(base_url);

loop {
Expand All @@ -66,7 +69,7 @@ async fn main() {
log::debug!("Report failed {:?}", report_response);
return;
}
log::debug!("Submission {:?} report success", submission_uid);
log::info!("Submission {:?} report success", submission_uid);
}
Err(e) => log::info!("Error judge task: {:?}", e),
}
Expand Down

0 comments on commit 90e55bf

Please sign in to comment.