Skip to content

Commit

Permalink
feat: add basic tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrix126 committed May 30, 2024
1 parent 4b9fdd7 commit 8e3d48d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ confy = "0.6"
url = {version="2.5.0", features=["serde"]}
serde = { version = "1", features = ["derive"]}
serde_json = "1"
tracing = "0.1"
27 changes: 19 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use axum::{
use reqwest::{Client, Url};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::{debug, info, warn};
#[derive(Serialize, Deserialize)]
struct Config {
listen_address: Ipv4Addr,
Expand All @@ -35,40 +36,50 @@ impl Default for Config {

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
info!("reading configuration file...");
let config: Config = confy::load_path("/etc/api-formatter/config.toml").expect("check that the directory /etc/api-formatter exist and that the program is ran with sufficient permission");
info!("creating client");
let state = AppState {
client: Client::new(),
};
let route = Router::new().fallback(handler).with_state(state);
let listener =
tokio::net::TcpListener::bind(format!("{}:{}", config.listen_address, config.listen_port))
.await?;
info!("Listening to {}", config.listen_address);
axum::serve(listener, route.into_make_service()).await?;
Ok(())
}

async fn handler(State(state): State<AppState>, request: Request) -> impl IntoResponse {
match state
info!("new request ! ");
debug!("The request received {:?}", request);
let req = state
.client
.request(request.method().to_owned(), request.uri().to_string())
.headers(request.headers().to_owned())
.body(to_bytes(request.into_body(), usize::MAX).await.unwrap())
.send()
.await
{
.await;
debug!("The request that will be make by the proxy {:?}", req);
match req {
Ok(rep) => format_resp(rep).await.into_response(),
Err(err) => (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
err.to_string(),
)
.into_response(),
Err(err) => {
warn!("request had an error: {}", err);
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
err.to_string(),
)
.into_response()
}
}
}

async fn format_resp(rep: reqwest::Response) -> impl IntoResponse {
let headers = rep.headers().to_owned();
let status = rep.status();
if let Ok(mut body) = rep.json::<Value>().await {
debug!("pretty formatting the response: {}", body);
format_json_fields_into_readable_output(&mut body);
(status, headers, body.to_string()).into_response()
} else {
Expand Down

0 comments on commit 8e3d48d

Please sign in to comment.