If you want to share a value, the answer is typically Arc. This type is a wrapper that you can put around any value. Whenever you clone an Arc, this doesn't clone the inner value, but provides a new shared wrapper that points to the same value.
error[E0599]: no method named `arc_bucket` found for struct `Bucket` in the current scope
--> src/main.rs:88:67
|
88 | HttpServer::new(move || App::new().service(index).data(bucket.arc_bucket()))
| ^^^^^^^^^^ method not found in `Bucket`
Here is the code:
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let cb_url = env::var("COUCHBASE_URL").unwrap();
let cb_user = env::var("COUCHBASE_USER").unwrap();
let cb_password = env::var("COUCHBASE_PASSWORD").unwrap();
let cb_bucket = env::var("COUCHBASE_PASSWORD").unwrap();
let cluster= Cluster::connect(cb_url, cb_user, cb_password);
let bucket = cluster.bucket(cb_bucket);
let arc_bucket = Arc::new(bucket);
HttpServer::new(move || App::new().service(index).data(bucket.arc_bucket()))
.bind(env::var("LISTEN_ON").unwrap_or_else(|_| String::from("0.0.0.0:8080")))?
.run()
.await
}
Indeed, I messed up my code when trying this, thanks!
It compiles now, but when I try to get in couchbase, it stays stuck
#[get("/api/profile")]
async fn index(
parameters: web::Query<QueryParameters>,
collection: web::Data<Arc<Collection>>,
) -> HttpResponse {
println!("Input: {:?}", parameters);
let digest = format!("{:x}", md5::compute(parameters.url.as_bytes()));
let r = collection.get(digest, GetOptions::default()).await.unwrap();
println!("Please show me the result {:?}", r); // <-- Stuck just before this print
HttpResponse::Ok()
.content_type("application/json")
.json(r.content::<CouchbaseDocument>().unwrap())
}
Maybe it's due to how the couchbase lib is implemented?
let cb_url = env::var("COUCHBASE_URL").unwrap();
let cb_user = env::var("COUCHBASE_USER").unwrap();
let cb_password = env::var("COUCHBASE_PASSWORD").unwrap();
let cb_bucket = env::var("COUCHBASE_PASSWORD").unwrap();
should obviously be
let cb_url = env::var("COUCHBASE_URL").unwrap();
let cb_user = env::var("COUCHBASE_USER").unwrap();
let cb_password = env::var("COUCHBASE_PASSWORD").unwrap();
let cb_bucket = env::var("COUCHBASE_BUCKET").unwrap();
The couchbase library has a weird behavior here, but once I fix this, everything went fine!