-
Hi all, I have rust folder like so:
mod.rs: mod routes;
mod user_api;
pub use routes::init_routes;
pub use user_api::{AddUsers, testOnly}; This function is inside: user_api.rs file: pub async fn AddUsers(cgrpc: APIClient<tonic::transport::Channel>, uid: u64) -> Result<TResponse<UserResponse>, Status> {
let request = tonic::Request::new(UserUID {
uid
});
let response = cgrpc.clone().nuser(request).await;
println!("RESPONSE={:?}", response);
response
} This one inside routes.rs: #[post("/adduser")]
async fn add_user(_req: HttpRequest, cgrpc: web::Data<APIClient<tonic::transport::Channel>>) -> HttpResponse {
user_api::testOnly(1);
match AddUsers(cgrpc, 1) {
Ok(result) => HttpResponse::Ok().json(result.unwrap()),
Err(err) => HttpResponse::Ok().json(err),
}
} But I get this error for cgrpc:
Inside main.rs: ...
HttpServer::new(move || {
App::new()
.data(cgrpc.clone())
.wrap(middleware::Compress::new(ContentEncoding::Br))
.wrap(middleware::Logger::default())
.service(web::scope("/user").configure(api::user_api::init_routes))
})
.bind("127.0.0.1:80")?
.run()
.await I can copy paste all functions inside user_api.rs to routes.rs and use directly: web::Data<APIClient<tonic::transport::Channel>> But for organization purpose, I need it like so, your help will be appreciated. Thank you ! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
In Here is an example. Modify AddUsers: pub async fn AddUsers(cgrpc: &APIClient<tonic::transport::Channel> ... in add_user (you also need to await): AddUsers(cgrpc.get_ref(), 1).await;
// or by deref coercion
AddUsers(&cgrpc, 1).await; |
Beta Was this translation helpful? Give feedback.
In
add_user
,cgrpc
is of typeData
. You can only get&APIClient<_>
from it by either usingData::get_ref
or by Deref trait and deref coercion. However, you should modifyAddUsers
to receive a&APIClient
, instead ofAPIClient
(or you can clone it!).Here is an example.
Modify AddUsers:
in add_user (you also need to await):