Skip to content

Commit

Permalink
feat(telemetry): add IP logging (#16)
Browse files Browse the repository at this point in the history
* feat(telemetry): add IP logging
  • Loading branch information
storopoli authored Apr 7, 2024
1 parent 7bcb232 commit 4d0f582
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stoic-quotes"
version = "0.3.8"
version = "0.3.9"
edition = "2021"
authors = ["Jose Storopoli <[email protected]>"]
description = "Stoic quotes API backend"
Expand Down
9 changes: 7 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::pages::{plain_quote, quote, root};
use axum::{http::header::USER_AGENT, http::Request, response::IntoResponse, routing::get, Router};
use std::{env::current_dir, path::PathBuf};
use std::{env::current_dir, net::SocketAddr, path::PathBuf};
use tower_http::{services::ServeDir, trace::TraceLayer};
use tracing::info;

Expand All @@ -11,14 +11,19 @@ use tracing::info;
/// return a plain quote.
/// Otherwise, return the root page.
async fn handle_user_agent<T>(req: Request<T>) -> impl IntoResponse {
let peer_addr = req
.extensions()
.get::<SocketAddr>()
.map(ToString::to_string)
.unwrap_or_else(|| "unknown".to_string());
let header = Request::headers(&req);
let user_agent: String = if let Some(user_agent) = header.get(USER_AGENT) {
user_agent.clone().to_str().unwrap().to_string()
} else {
"blank".to_string()
};

info!("got user agent: {user_agent}");
info!("Request from {peer_addr} with User-Agent: {user_agent}");

if user_agent.contains("curl") || user_agent.contains("Wget") {
plain_quote().await.into_response()
Expand Down

0 comments on commit 4d0f582

Please sign in to comment.