diff --git a/src/config.rs b/src/config.rs index a62449c..6d0ec06 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,12 +1,16 @@ use std::net::SocketAddr; -use reqwest::Url; use serde::Deserialize; #[derive(Deserialize)] pub struct Config { pub addr: SocketAddr, - pub gitea_url: Url, - pub config_repo_name: String, - pub admin_token: String, + pub proxy: Option, + pub proxy_auth: Option, +} + +#[derive(Deserialize)] +pub struct ProxyAuth { + pub username: String, + pub password: String, } diff --git a/src/main.rs b/src/main.rs index 0656051..81fda44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,10 +6,10 @@ use axum::{ use config::Config; use error::Error; use model::{APIConfig, Request}; -use serde_json::Value; +use reqwest::Proxy; use structopt::StructOpt; -use crate::model::{AuthQuery, Response}; +use crate::model::Response; mod config; mod error; @@ -31,11 +31,18 @@ async fn main() -> Result<(), Error> { let opt = Opt::from_args(); let config = std::fs::read(&opt.config)?; let config = toml::from_slice::(&config)?; - let api_conf = APIConfig( - config.gitea_url, - config.config_repo_name, - config.admin_token, - ); + let mut builder = reqwest::ClientBuilder::new() + .user_agent("curl") + .timeout(Duration::from_secs(30)); + if let Some(px) = config.proxy { + let mut proxy = Proxy::all(px)?; + if let Some(auth) = config.proxy_auth { + proxy = proxy.basic_auth(auth.username.as_str(), auth.password.as_str()); + } + builder = builder.proxy(proxy); + } + let client = builder.build()?; + let api_conf = APIConfig(client); let app = Router::new() .route("/", post(on_request)) @@ -50,56 +57,13 @@ async fn main() -> Result<(), Error> { async fn on_request( Json(body): Json, - Extension(APIConfig(base_url, repo_name, token)): Extension, + Extension(APIConfig(client)): Extension, ) -> Result { - let client = reqwest::ClientBuilder::new() - .user_agent("curl") - .timeout(Duration::from_secs(30)) - .build()?; let conf = body.config(); if conf.starts_with("http://") || conf.starts_with("https://") { let drone_config = client.get(conf).send().await?.text().await?; let response = Response { data: drone_config }; return Ok(Json(response)); } - - let auth = AuthQuery { - access_token: token, - }; - let index_url = format!( - "{}/api/v1/repos/{}/{}/raw/index.json", - base_url.to_string(), - body.namespace(), - &repo_name - ); - let res: Value = client - .get(index_url) - .query(&auth) - .send() - .await? - .json() - .await?; - - if let Value::Object(obj) = res { - let v = obj.get(&body.name()).ok_or(Error::NoContent)?; - if let Value::String(path) = v { - let conf_url = format!( - "{}/api/v1/repos/{}/{}/raw/{}", - base_url.to_string(), - body.namespace(), - &repo_name, - path - ); - let drone_config = client - .get(conf_url) - .query(&auth) - .send() - .await? - .text() - .await?; - let response = Response { data: drone_config }; - return Ok(Json(response)); - } - } Err(Error::NoContent) } diff --git a/src/model.rs b/src/model.rs index faa1043..b1f0bd9 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,4 +1,4 @@ -use reqwest::Url; +use reqwest::Client; use serde::{Deserialize, Serialize}; #[derive(Deserialize)] @@ -8,8 +8,6 @@ pub struct Request { #[derive(Deserialize)] pub struct Repository { - pub name: String, - pub namespace: String, pub config_path: String, } @@ -18,23 +16,10 @@ pub struct Response { pub data: String, } -#[derive(Serialize)] -pub struct AuthQuery { - pub access_token: String, -} - #[derive(Clone)] -pub struct APIConfig(pub Url, pub String, pub String); +pub struct APIConfig(pub Client); impl Request { - pub fn namespace(&self) -> String { - self.repo.namespace.clone() - } - - pub fn name(&self) -> String { - self.repo.name.clone() - } - pub fn config(&self) -> String { self.repo.config_path.clone() }