英文:
the trait `Clone` is not implemented for `actix_web::Scope`
问题
我想将我的应用程序路由分组在范围内,以便将来可以根据领域分离它们的文件位置。
我试图做的是将
HttpServer::new(move || App::new().app_data(app_state.clone()).service(delete_comment).service(update_comment).service(get_comments).service(create_comment))
.bind("127.0.0.1", 8080)?
.run()
.await
转换为:
let comment_scope = web::scope("/comments").service(delete_comment).service(update_comment).service(get_comments).service(create_comment);
HttpServer::new(move || App::new().app_data(app_state.clone()).service(comment_scope))
.bind("127.0.0.1", 8080)?
.run()
.await
但它一直告诉我,actix_web::Scope
没有实现 Clone
特性。
我该如何解决这个问题?
英文:
I want to group my app routes in scope, so I can separate their file location per Domain in the future.
what I'm trying to do is convert
HttpServer::new(move || App::new().app_data(app_state.clone()).service(delete_comment).service(update_comment).service(get_comments).service(create_comment))
.bind(("127.0.0.1", 8080))?
.run()
.await
to:
let comment_scope = web::scope("/comments").service(delete_comment).service(update_comment).service(get_comments).service(create_comment);
HttpServer::new(move || App::new().app_data(app_state.clone()).service(comment_scope))
.bind(("127.0.0.1", 8080))?
.run()
.await
but it keeps telling me that the trait Clone
is not implemented for actix_web::Scope
.
how can I fix this?
答案1
得分: 5
你只需将 comment_scope
声明移到 new
闭包中:
HttpServer::new(move || {
let comment_scope = web::scope("/comments")
.service(delete_comment)
.service(update_comment)
.service(get_comments)
.service(create_comment);
App::new()
.service(comment_scope)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
英文:
You just have to move the comment_scope
declaration into new
closure:
HttpServer::new(move || {
let comment_scope = web::scope("/comments")
.service(delete_comment)
.service(update_comment)
.service(get_comments)
.service(create_comment);
App::new()
.service(comment_scope)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
答案2
得分: 0
使用TcpListener来尝试这个:
use std::net::TcpListener;
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
let server = HttpServer::new(|| {
App::new()
.route("/comments", web::post().to(comments))
[/.../]
})
.listen(listener)?
.run();
Ok(server)
}
英文:
How about try this by using TcpListener:
use std::net::TcpListener;
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
let server = HttpServer::new(|| {
App::new()
.route("/comments", web::post().to(comments))
[/.../]
})
.listen(listener)?
.run();
Ok(server)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论