如何在 Rust Actix 中传递 ScyllaDB 连接
How passing ScyllaDB connection in Rust Actix
我在 Rust Actix 上实现 scylla-rust-driver 时遇到问题。我只想用 scyllaDb 创建简单的 CRUD。
首先我为应用程序数据创建结构
struct AppState {
scy_session: Session,
app_name: String,
}
接下来我创建简单函数
#[get("/")]
async fn index(data: web::Data<AppState>) -> String {
// I want to CRUD in this function with ScyllaDB
let app_name = &data.app_name; // <- get app_name
format!("Hello {}!", app_name) // <- response with app_name
}
最后 main.rs 是
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let session: Session = SessionBuilder::new()
.known_node("localhost:9042")
.user("username", "password")
.build()
.await
.expect("Some error message");
HttpServer::new(|| {
App::new()
.data(AppState {
scy_session: session,
app_name: String::from("Actix-web"),
})
.service(index)
})
.bind("127.0.0.1:8080")?
.run()
.await
}
但是显示错误:
error[E0277]: the trait bound `Session: Clone` is not satisfied in `[closure@src/main.rs:27:21: 34:6]`
--> src/main.rs:27:5
|
27 | HttpServer::new(|| {
| _____^^^^^^^^^^^^^^^_-
| | |
| | within `[closure@src/main.rs:27:21: 34:6]`, the trait `Clone` is not implemented for `Session`
实际上如何在 Actix web 上实现 scylla-rust-db?
非常感谢
Session 没有什么特别之处,如此处所示 https://github.com/scylladb/scylla-rust-driver/blob/main/examples/parallel-prepared.rs
解决方案是将其包装在 Arc
中。我们不必自己这样做,因为 web::Data
自己会这样做。这是我得到的。
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use scylla::{Session, SessionBuilder};
struct AppState {
scy_session: Session,
app_name: String,
}
/// Use the `Data<T>` extractor to access data in a handler.
async fn index(data: web::Data<AppState>) -> impl Responder {
let app_name = &data.app_name; // <- get app_name
HttpResponse::Ok().body(format!("Hello {}!", app_name))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let session: Session = SessionBuilder::new()
.known_node("localhost:9042")
.user("username", "passwor")
.build()
.await
.expect("Some error message");
let data = web::Data::new(AppState {
scy_session: session,
app_name: String::from("Actix-web"),
});
HttpServer::new(move || {
App::new()
.data(data.clone())
.route("/", web::get().to(index))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
另请参阅:
我在 Rust Actix 上实现 scylla-rust-driver 时遇到问题。我只想用 scyllaDb 创建简单的 CRUD。
首先我为应用程序数据创建结构
struct AppState {
scy_session: Session,
app_name: String,
}
接下来我创建简单函数
#[get("/")]
async fn index(data: web::Data<AppState>) -> String {
// I want to CRUD in this function with ScyllaDB
let app_name = &data.app_name; // <- get app_name
format!("Hello {}!", app_name) // <- response with app_name
}
最后 main.rs 是
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let session: Session = SessionBuilder::new()
.known_node("localhost:9042")
.user("username", "password")
.build()
.await
.expect("Some error message");
HttpServer::new(|| {
App::new()
.data(AppState {
scy_session: session,
app_name: String::from("Actix-web"),
})
.service(index)
})
.bind("127.0.0.1:8080")?
.run()
.await
}
但是显示错误:
error[E0277]: the trait bound `Session: Clone` is not satisfied in `[closure@src/main.rs:27:21: 34:6]`
--> src/main.rs:27:5
|
27 | HttpServer::new(|| {
| _____^^^^^^^^^^^^^^^_-
| | |
| | within `[closure@src/main.rs:27:21: 34:6]`, the trait `Clone` is not implemented for `Session`
实际上如何在 Actix web 上实现 scylla-rust-db? 非常感谢
Session 没有什么特别之处,如此处所示 https://github.com/scylladb/scylla-rust-driver/blob/main/examples/parallel-prepared.rs
解决方案是将其包装在 Arc
中。我们不必自己这样做,因为 web::Data
自己会这样做。这是我得到的。
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use scylla::{Session, SessionBuilder};
struct AppState {
scy_session: Session,
app_name: String,
}
/// Use the `Data<T>` extractor to access data in a handler.
async fn index(data: web::Data<AppState>) -> impl Responder {
let app_name = &data.app_name; // <- get app_name
HttpResponse::Ok().body(format!("Hello {}!", app_name))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let session: Session = SessionBuilder::new()
.known_node("localhost:9042")
.user("username", "passwor")
.build()
.await
.expect("Some error message");
let data = web::Data::new(AppState {
scy_session: session,
app_name: String::from("Actix-web"),
});
HttpServer::new(move || {
App::new()
.data(data.clone())
.route("/", web::get().to(index))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
另请参阅: