micromegas/servers/
log_uri_service.rs

1use micromegas_tracing::info;
2use std::task::Context;
3use std::task::Poll;
4use tower::Service;
5
6use super::http_utils::get_client_ip;
7
8/// A Tower service that logs the URI of incoming requests.
9#[derive(Clone)]
10pub struct LogUriService<S> {
11    /// The inner service to which requests will be forwarded.
12    pub service: S,
13}
14
15impl<S, Body> Service<http::Request<Body>> for LogUriService<S>
16where
17    S: Service<http::Request<Body>>,
18{
19    type Response = S::Response;
20    type Error = S::Error;
21    type Future = S::Future;
22
23    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
24        self.service.poll_ready(cx)
25    }
26
27    /// Logs the URI of the incoming request and then calls the inner service.
28    fn call(&mut self, request: http::Request<Body>) -> Self::Future {
29        let client_ip = get_client_ip(request.headers(), request.extensions());
30        info!("uri={:?} client_ip={client_ip}", request.uri());
31        self.service.call(request)
32    }
33}