Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(telemetry): add IP logging #16

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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