Converting request body to axum::body::Body #513
-
I think my question related more to my inability to fully understand Services and how we can merge/connect with one another. My problem is:
Then, I have a set of dynamic routes like:
With
Just with the console, everything works fine but the worker is also have routing (not to mention that after 0.3.x update none of it would work, but worked before):
Back to the problem Though I think the bigger problem is that documentation focuses on convenience using Services and minimalistic @davidpdrsn is always helping out but I think there are some foundational docs missing. How to connect services assuming that they are all generics and end up having different Request/Response types. For example, I have been using BoxBody from
Which helped but would've been better if I wouldn't have to scroll through sources to understand. And I will be happy to help with documentation (though my English grammar sucks) but for that I need to have clear understanding myself. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
If you're in a spot that requires converting a generic request body into a Is there a reason you're writing tower services directly instead of async handler functions? If that is possible I would highly recommend doing that instead as its way easier. You also don't have to use a generic parameter for the request body. Doing this should just work: impl<S> Service<Request<Body>> for Foo<S>
where
S: Service<Request<Body>>, Similar to whats shown here. This wont work if you have middleware the change the request body but such middleware are very rare. For example there are currently no middleware in tower-http that change the request body, though we're thinking about adding one. In terms of documentation I think you're right. Tower has a reputation of being hard to understand and while I think we're in a better spot now than a year ago, we still have work to do. However I don't feel tower needs more API documentation (in the docs.rs sense). We already have:
Its quite hard to write good "how it all fits together" docs in generated API docs, especially when your API is split across a few crates (tower, http, tower-http, http-body, hyper, axum). Depending on the level of abstraction you're at you might also run into more generics than you used to. I don't think the generics are inherently bad but unless you come from haskell or scala there might be some learning curve to it. Personally I would like to write more blog posts (or streams) about solving concrete problems with tower. The hope being that after people have read a bunch of them they would have a deeper understanding and know which tools to apply where. But figuring out how to explain this stuff in a way thats easy to understand, is hard. Its not something I have bandwidth for myself at the moment unfortunately. |
Beta Was this translation helpful? Give feedback.
If you're in a spot that requires converting a generic request body into a
axum::body::Body
then I'd say your approach is wrong. Its not something you should have to do. So this feels like an XY question.Is there a reason you're writing tower services directly instead of async handler functions? If that is possible I would highly recommend doing that instead as its way easier.
You also don't have to use a generic parameter for the request body. Doing this should just work:
Similar to whats shown here.
This wont work if you have middleware the change the request body but such middleware are very rare. For examp…