by 0x457 · 4y ago · 0 comments
1. This one is annoying, but easily solvable by making your own return type that implements IntoResponse
2. I'm not sure what you're talking about? You just add `Cookies` extractor to your handler and use that. You don't need to add it to the return, middleware takes care of it.
Middleware in axum split into two: Tower middleware and Axum middleware. Tower middleware is...hard to write, but it's faster, axum middleware is easier to write, but it's slower.
4. axum doesn't force using closures, and state is shared via extensions?
I'm currently working on a service where I have tonic (gRPC) and axum on the same port, and they share a lot of middleware.
You can just call `.into_response()` like documented here https://docs.rs/axum/latest/axum/response/index.html#returni...
> I'm not sure what you're talking about? You just add `Cookies` extractor to your handler and use that. You don't need to add it to the return, middleware takes care of it.
I think you're thinking of tower-cookies which works the same way poem's CookieJarManager does.
axum-extra's cookies work differently in that you have to return the updated jar from handlers. We chose this design because it composes better when you have multiple libraries that all wanna set cookies. tower-cookies doesn't work if you accidentally add multiple cookie middleware, whereas axum-extra's approach does. So its a trade-off.
Yes, that's what I'm using.
> You can just call `.into_response()` like documented here https://docs.rs/axum/latest/axum/response/index.html#returni...
Oh yeah, totally forgot about that one.
Anyways implementing a custom `IntoResponse` shouldn't be necessary in the first 30m of using a framework (not saying I didn't just miss something, but I've read the docs multiple times).
This is also whats shown in the "Implementing IntoResponse" examples https://docs.rs/axum/latest/axum/response/trait.IntoResponse...
I actually read that, but I didn't realize that it was a specific type and my IDE auto-probably imported the wrong one. There's a lot of types named `Response` and with how common re-exports are I probably wouldn't have noticed unless I was expecting it. And since they're all part of the same type tree you aren't going to get clear error messages from the compiler either.
Naming it something like `DefaultResponse` or `AxumResponse` or calling it out explicitly by changing
> Use Response for more low level control:
to
> The above all eventually gets turned into axum::response::Response (a specialization of `http::Response`), which you can instantiate directly for more low level control:
would help immensely.
Edit: No, reading more closely `Response` has a default body of `UnsyncBoxBody<Bytes, Error>` but other than that it's still generic and identical to `http::Response`? That's not going to help if you're having generic issues unless you explicitly convert things to `Bytes` AFAICT, and a default parameter is hardly obvious documentation.