Gravitalia / Autha

Autha is a fast and safe authorization delegation and account management API 🦦
https://account.gravitalia.com
Mozilla Public License 2.0
28 stars 3 forks source link

Use `hyper` and `tower` for metrics middleware #412

Open RealHinome opened 5 months ago

RealHinome commented 5 months ago

Describe what you want in a new way for Autha:

We should use hyper and tower-http to create a middleware to apply Prometheus metrics to every requests. Since there is NO example for v1 of Hyper, we should wait.

This is an example (not working):

    let listener = TcpListener::bind(SocketAddr::from(([0, 0, 0, 0], config.port))).await?;
    loop {
        let (stream, _) = listener.accept().await?;
        let io = TokioIo::new(stream);
        let mut warp_svc = warp::service(
            warp::any().map(|| "Hello From Warp!")
        );

        tokio::spawn(async move {
            // N.B. should use hyper service_fn here, since it's required to be implemented hyper Service trait!
            let svc = hyper::service::service_fn(move |req: Request<Incoming>| async move {
                let mut warp_req = warp::http::Request::builder()
                .method(req.method().to_string().as_str())
                .uri(req.uri().to_string())
                .body(req.body())
                .unwrap();

                println!("before request");
                let resp = warp_svc.call(warp_req);
                println!("after request");
                resp
            });

            if let Err(err) = http1::Builder::new().serve_connection(io, svc) {
                eprintln!("server error: {}", err);
            }
        });
    }

Which library/binary?

Autha

RealHinome commented 5 months ago

This could be also used to perfrom rate-limiting.