Trouble with Missing Request Extension for ScyllaDB Session in Axum #2158
-
Hi everyone, I'm currently working on a Rust project using Axum for managing my API routes. I have several routes defined in different files and I'm using Axum's 'extensions' to share a ScyllaDB session across these routes. The idea is to put my ScyllaDB session (ScyllaDB is a NoSQL database) into an extension, so I can access it in every request. However, I'm encountering an issue with this approach. The error I'm getting is: I suspect that I might have missed adding this extension to the request somewhere in my code. However, I'm not exactly sure where the issue is originating from. Could someone help me understand what might be going wrong and how I can ensure that each request properly includes this extension? Here's a snippet of my main function where I'm defining my routes and trying to use the ScyllaDB session: #[tokio::main]
async fn main() {
if let Ok(session) = SessionBuilder::new()
.known_node("162.19.00.00:9042")
.user("user", "password")
.build()
.await
{
let app = Router::new()
.nest("/api", routes())
.route("/backroute", get(handler));
let addr = SocketAddr::from(([127, 0, 0, 1], 3001));
println!("Listening on http://{}", addr);
if let Err(e) = Server::bind(&addr).serve(app.into_make_service()).await {
eprintln!("Server error: {}", e);
}
} else {
eprintln!("Unable to create session");
}
} If you need more code snippets from other files of my project, just let me know. I appreciate any help or insights you could provide. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You’re either not adding the extension or extracting an extension with the wrong type. In the code you posted you don’t add the extension anywhere. You need to use https://docs.rs/axum/0.6.20/axum/struct.Extension.html |
Beta Was this translation helpful? Give feedback.
You’re either not adding the extension or extracting an extension with the wrong type. In the code you posted you don’t add the extension anywhere. You need to use https://docs.rs/axum/0.6.20/axum/struct.Extension.html