I am building an handler that requires state to be injected and also needs to extract the query parameters.
I started off by only extracting the state and that worked. The code for that looks something like this:
#[derive(ValueEnum, Clone, Debug, serde::Deserialze, serde::Serialize)]
pub enum MyParams {
Normal,
Verbose,
}
#[derive(Debug)]
pub struct MyState {
port: u16,
}
pub async fn serve(self) {
let port = self.port;
let app = Router::new()
.route("/path", axum::routing::get(path))
.with_state(Arc::new(self));
let addr = SocketAddr::from(([127, 0, 0, 1], port));
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn path(State(middle_ware): State<Arc<MyState>>) -> impl IntoResponse {
let result = middle_ware.process().await;
(StatusCode::OK, Json(result))
}
Now I want to extract query parameters so I updated the code as follows:
async fn path(State(middle_ware): State<Arc<MyState>>, params: Query<MyParams>) -> impl IntoResponse {
println!("{:?}", params);
let result = middle_ware.process().await;
(StatusCode::OK, Json(result))
}
But this fails to compile with the error
|
24 | .route("/path", axum::routing::get(path))
| ------------------ ^^^^^^^^^ the trait `Handler<_, _, _>` is not implemented for fn item `fn(State<Arc<MyState>>, Query<MyParams>) -> impl futures::Future<Output = impl IntoResponse> {path}`
| |
| required by a bound introduced by this call
Any ideas what to do, to be able to use both axum::extract::Query and axum::extract::State with Axum?