You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For example, I want to access a database. I can't figure out how to pass the database into the function. I've tried implementing the AsyncInterceptor, but ran into the fact that the associated type is a future, and I can't figure out how to name the future because of rust-lang/rust#63063
#[derive(Clone)]pubstructMyAuthInterceptor{pubdb:Pool<Postgres>,}implAsyncInterceptorforMyAuthInterceptor{typeFuture = Pin<Box<dynFuture<Output = Result<Request<()>,Status>> + Send + 'static>>;fncall(&mutself,request:Request<()>) -> Self::Future{let fut = self.authenticate(request);Box::pin(fut)}}implMyAuthInterceptor{pubfnnew(db:Pool<Postgres>) -> Self{Self{ db }}pubfnauthenticate(&self,request:Request<()>,) -> implFuture<Output = Result<Request<()>,Status>> + Send + 'static{let db = self.db.clone();// Clone the database poollet metadata = request.metadata().clone();// Clone the request metadataasyncmove{// Now we can use db inside the async block// match sqlx::query("...").fetch_one(db).await {// Ok(_) => {}// Err(_) => {}// }Ok(request)}}}#[tokio::main]asyncfnmain() -> Result<(),Box<dyn std::error::Error>>{// Setup stuff like `svc` and `address`let db = PgPoolOptions::new().max_connections(5).connect(&dsn).await?;let auth_service = MyAuthInterceptor::new(db);Server::builder().layer(async_interceptor(auth_service))// <----- Add the async interceptor here.add_service(svc).serve(address).await?;}
For example, I want to access a database. I can't figure out how to pass the database into the function. I've tried implementing the
AsyncInterceptor
, but ran into the fact that the associated type is a future, and I can't figure out how to name the future because of rust-lang/rust#63063doesn't work. I've also tried
type Future = Box<dyn futures_core::Future<Output=Result<Request<()>, Status>> + Unpin>;
but ran intoThe text was updated successfully, but these errors were encountered: