How do I pass variables into my Tokio service handler

I'm trying to pass a struct with my DB client pools into my service handler. I'm really noobie with rust but I do understand the concepts with async as I have watched a few Youtube lectures. It's the syntax that is killing me.

Could someone please write this out in a way I can pass in my own struct? Please be generous with your commenting. Thanks so much.

Below is the code I have right now.

// Start the Hyper server
    let addr = ([127, 0, 0, 1], 5000).into();
    let service = make_service_fn(|_, db| async { Ok::<_, hyper::Error>(service_fn(route)) });
    let httpserver = Server::bind(&addr).serve(service);

I originally had it like below because that's how I had seen it in lectures and it was easier to reason about. It seems like this way is actually handling the TCP segments manually though and I could never figure out how to get a Request type I need for the service handler.

async fn main() {
    let listener = TcpListener::bind("").await.unwrap();

    loop {
        let (socket, _) = listener.accept().await.unwrap();
        // A new task is spawned for each inbound socket. The socket is
        // moved to the new task and processed there.
        tokio::spawn(async move {

General tips would be appreciated too. Such as what is the idiomatic way to pass around database client variables. How can I set a max size on Request bodies, other config params, etc

Have you seen this example?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.