From 9ef64b5b82eaa7c62cd24a68f5f78da14d4289d6 Mon Sep 17 00:00:00 2001 From: Jonas_Jones Date: Sun, 30 Jun 2024 00:34:55 +0200 Subject: [PATCH] added more request logging code --- src/request_logger.rs | 17 ++++++++++------- src/server.rs | 21 +++++++++++++++++---- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/request_logger.rs b/src/request_logger.rs index e559389..e742d4a 100644 --- a/src/request_logger.rs +++ b/src/request_logger.rs @@ -1,10 +1,12 @@ use chrono::Utc; +use reqwest::Client; use reqwest::blocking::get; use serde::{Deserialize, Serialize}; use serde_json::Value; use sha2::{Digest, Sha256}; use std::fs::OpenOptions; use std::io::{BufReader, BufWriter}; +use warp::http::Method; use std::path::Path; #[derive(Serialize, Deserialize)] @@ -16,7 +18,7 @@ struct RequestLog { ip_hash: String, } -fn get_ip_country_code(ip: &str) -> Result> { +async fn get_ip_country_code(ip: &str) -> Result> { let url = format!("http://ip-api.com/json/{}", ip); let response: Value = get(&url)?.json()?; if let Some(country_code) = response["countryCode"].as_str() { @@ -26,20 +28,21 @@ fn get_ip_country_code(ip: &str) -> Result> { } } -fn hash_ip(ip: &str) -> String { +async fn hash_ip(ip: &str) -> String { let mut hasher = Sha256::new(); hasher.update(ip); format!("{:x}", hasher.finalize()) } -pub fn log_request(ip: &str, pathname: &str, method: &str, file_path: &str) /*-> Result<(), Box>*/ { +pub async fn log_request(client: &Client, ip: &str, pathname: &str, method: &Method, file_path: &str) -> Result<(), Box> { + let method_str = method.as_str(); let timestamp = Utc::now().to_rfc3339(); - let ip_country_code = get_ip_country_code(ip)?; - let ip_hash = hash_ip(ip); + let ip_country_code = get_ip_country_code(ip).await?; + let ip_hash = hash_ip(ip).await; let log_entry = RequestLog { timestamp, - method: method.to_string(), + method: method_str.to_string(), pathname: pathname.to_string(), ip_country_code, ip_hash, @@ -64,5 +67,5 @@ pub fn log_request(ip: &str, pathname: &str, method: &str, file_path: &str) /*-> let writer = BufWriter::new(&file); serde_json::to_writer_pretty(writer, &logs)?; - //Ok(()) + Ok(()) } \ No newline at end of file diff --git a/src/server.rs b/src/server.rs index 90bb641..51ec63b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -4,9 +4,12 @@ use std::env; use lastfm::reqwest::StatusCode; use warp::filters::path::FullPath; +use warp::http::Method; use warp::Filter; use warp::reply::Reply; +use reqwest::Client; + use crate::error_responses::{ErrorMessage, InternalServerError, BadRequestError, NotFoundError, NotImplementedError}; use crate::v1::get_v1_routes; use crate::{parse_ip, request_logger, Logger}; @@ -33,12 +36,22 @@ pub async fn serve() { .and(warp::path::full()) .and(warp::addr::remote()) .and(warp::header::optional::("x-forwarded-for")) - .map(|method, path: FullPath, addr: Option, fwd_for: Option| { + .map(|method: Method, path: FullPath, addr: Option, fwd_for: Option| { let client_ip = fwd_for.unwrap_or_else(|| addr.map(|a| a.ip().to_string()).unwrap_or_else(|| String::from("unknown"))); - let path_str = path.as_str(); + let path_str = path.as_str().to_string(); // Convert to owned String + let method_clone = method.clone(); + let method_str = method_clone.clone().as_str().to_string(); + let client_ip_clone = client_ip.clone(); // Clone for use outside the async block + let path_str_clone = path_str.clone(); - request_logger::log_request(&client_ip, path_str, method, "requests.json"); - Logger::info(&format!(" {} {} from {} ({})", method, path_str, ip_lookup(&client_ip), client_ip)); + /*tokio::spawn(async move { + let client = Client::new(); + if let Err(e) = request_logger::log_request(&client, &client_ip, &path_str, &method_clone, "requests.json").await { + eprintln!("Failed to log request: {:?}", e); + } + });*/ + + Logger::info(&format!("{} {} from {} ({})", method_str, path_str_clone, ip_lookup(&client_ip_clone), client_ip_clone)); }); // GET (any) => reply with return from handle_path