droneconf/src/main.rs

70 lines
1.8 KiB
Rust
Raw Normal View History

2022-01-30 22:18:09 +01:00
use std::{path::PathBuf, time::Duration};
use axum::{
extract::Extension, handler::post, response::IntoResponse, AddExtensionLayer, Json, Router,
};
use config::Config;
use error::Error;
use model::{APIConfig, Request};
2022-03-26 23:53:27 +01:00
use reqwest::Proxy;
2022-01-30 22:18:09 +01:00
use structopt::StructOpt;
2022-03-26 23:53:27 +01:00
use crate::model::Response;
2022-01-30 22:18:09 +01:00
mod config;
mod error;
mod model;
#[derive(StructOpt)]
struct Opt {
#[structopt(
short,
long,
help = "config file to use",
default_value = "./config.toml"
)]
config: PathBuf,
}
#[tokio::main]
async fn main() -> Result<(), Error> {
let opt = Opt::from_args();
let config = std::fs::read(&opt.config)?;
let config = toml::from_slice::<Config>(&config)?;
2022-03-26 23:53:27 +01:00
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);
2022-01-30 22:18:09 +01:00
let app = Router::new()
.route("/", post(on_request))
.layer(AddExtensionLayer::new(api_conf));
axum::Server::bind(&config.addr)
.serve(app.into_make_service())
.await?;
Ok(())
}
async fn on_request(
Json(body): Json<Request>,
2022-03-26 23:53:27 +01:00
Extension(APIConfig(client)): Extension<APIConfig>,
2022-01-30 22:18:09 +01:00
) -> Result<impl IntoResponse, Error> {
2023-07-19 21:51:47 +02:00
let conf = body.config().ok_or(Error::NoContent)?;
if conf.starts_with("http://") || conf.starts_with("https://") {
let drone_config = client.get(conf).send().await?.text().await?;
2023-07-19 22:36:24 +02:00
let response = Response::new(drone_config, body.is_woodpecker());
return Ok(Json(response));
}
2022-01-30 22:18:09 +01:00
Err(Error::NoContent)
}