diff --git a/junowen-server/src/app.rs b/junowen-server/src/app.rs index 5b65a7d..bf487d6 100644 --- a/junowen-server/src/app.rs +++ b/junowen-server/src/app.rs @@ -1,12 +1,14 @@ mod custom; +use std::hash::{self, DefaultHasher, Hash, Hasher}; + use anyhow::{bail, Result}; use lambda_http::{ http::{header::RETRY_AFTER, StatusCode}, Body, IntoResponse, Request, Response, }; use serde::Deserialize; -use tracing::trace; +use tracing::{info_span, trace, Instrument}; use crate::{app::custom::custom, database::Database}; @@ -32,10 +34,24 @@ fn to_response( builder.body(body.into()).unwrap() } +fn ip_hash(req: &Request) -> u64 { + let ip = req + .headers() + .get("x-forwarded-for") + .and_then(|x| x.to_str().ok()) + .unwrap_or_default(); + let mut s = DefaultHasher::new(); + ip.hash(&mut s); + s.finish() +} + pub async fn app(req: &Request, db: &impl Database) -> Result { trace!("{:?}", req); + if let Some(relative_uri) = req.uri().path().strip_prefix("/custom/") { - return custom(relative_uri, req, db).await; + return custom(relative_uri, req, db) + .instrument(info_span!("ip_hash", "{:x}", ip_hash(req))) + .await; } Ok(to_response(StatusCode::NOT_FOUND, None, Body::Empty)) }