diff --git a/judger/src/agent/http.rs b/judger/src/agent/http.rs index 27db9ce..4432cc2 100644 --- a/judger/src/agent/http.rs +++ b/judger/src/agent/http.rs @@ -6,8 +6,19 @@ pub struct HttpClient { } impl HttpClient { - pub fn new(base_url: String) -> Self { - let client = reqwest::Client::new(); + pub fn new(base_url: String, internal_token: String) -> Self { + let client = reqwest::Client::builder() + .default_headers({ + let mut headers = reqwest::header::HeaderMap::new(); + headers.insert( + reqwest::header::AUTHORIZATION, + reqwest::header::HeaderValue::from_str(&format!("Bearer {}", internal_token)) + .unwrap(), + ); + headers + }) + .build() + .unwrap(); Self { client, base_url } } diff --git a/judger/src/agent/platform/mod.rs b/judger/src/agent/platform/mod.rs index 00026f8..02d1b87 100644 --- a/judger/src/agent/platform/mod.rs +++ b/judger/src/agent/platform/mod.rs @@ -6,9 +6,9 @@ pub struct PlatformClient { } impl PlatformClient { - pub fn new(base_url: String) -> Self { + pub fn new(base_url: String, internal_token: String) -> Self { Self { - client: HttpClient::new(base_url), + client: HttpClient::new(base_url, internal_token), } } diff --git a/judger/src/main.rs b/judger/src/main.rs index cb10836..80553ac 100644 --- a/judger/src/main.rs +++ b/judger/src/main.rs @@ -36,6 +36,7 @@ async fn main() -> std::io::Result<()> { match opt.cmd { option::JudgerCommad::Serve { platform_uri, + internal_token, fetch_task_interval, port, } => { @@ -44,6 +45,7 @@ async fn main() -> std::io::Result<()> { opt.problem_package_bucket, opt.problem_package_dir, platform_uri.clone(), + internal_token, fetch_task_interval, port, ) @@ -72,10 +74,11 @@ async fn serve( problem_package_bucket: String, problem_package_dir: PathBuf, platform_uri: String, + internal_token: String, fetch_task_interval: u64, port: u16, ) -> std::io::Result<()> { - let platform_client = platform::PlatformClient::new(platform_uri.clone()); + let platform_client = platform::PlatformClient::new(platform_uri.clone(), internal_token); let worker = match JudgeWorker::new( Some(platform_client), diff --git a/judger/src/option.rs b/judger/src/option.rs index 179cb1c..d00f0fd 100644 --- a/judger/src/option.rs +++ b/judger/src/option.rs @@ -33,6 +33,8 @@ pub enum JudgerCommad { Serve { #[structopt(env = "PLATFORM_URI", default_value = "http://localhost:8080/")] platform_uri: String, + #[structopt(env = "INTERNAL_TOKEN", default_value = "internal_token")] + internal_token: String, /// Interval to fetch task in seconds #[structopt(env = "FETCH_TASK_INTERVAL", default_value = "10")] fetch_task_interval: u64,