Skip to content

Commit

Permalink
feat(junowen-server): add ip hash to log
Browse files Browse the repository at this point in the history
  • Loading branch information
progre committed Nov 19, 2023
1 parent cdb19c3 commit 3574f25
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions junowen-server/src/app.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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<impl IntoResponse> {
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))
}

0 comments on commit 3574f25

Please sign in to comment.