How to separate passing of App closure instance to HttpServer?
Closed this issue · 2 comments
amber-m-o-r-e commented
I tried the following code to do app separation logic
fn routes() -> impl Fn() -> actix_web::app::App<actix_web::app_service::AppEntry, actix_http::actix_http::Body> {
|| {
let app = App::new()
.route("/", web::get().to(greet))
.route("/{name}", web::get().to(greet));
return app;
}
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(routes())
.bind("127.0.0.1:8088")?
.run()
.await
}
But with this compiler is giving me errors-
Compiling main-example v1.0.0 (/home/bob/code/rust/hello_world)
error[E0433]: failed to resolve: use of undeclared type or module `actix_http`
--> src/main.rs:17:83
|
17 | fn routes() -> impl Fn() -> actix_web::app::App<actix_web::app_service::AppEntry, actix_http::actix_http::Body> {
| ^^^^^^^^^^ use of undeclared type or module `actix_http`
error[E0603]: module `app` is private
--> src/main.rs:17:40
|
17 | fn routes() -> impl Fn() -> actix_web::app::App<actix_web::app_service::AppEntry, actix_http::actix_http::Body> {
| ^^^ this module is private
|
note: the module `app` is defined here
--> /home/bob/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-2.0.0/src/lib.rs:81:1
|
81 | mod app;
| ^^^^^^^^
error[E0603]: module `app_service` is private
--> src/main.rs:17:60
|
17 | fn routes() -> impl Fn() -> actix_web::app::App<actix_web::app_service::AppEntry, actix_http::actix_http::Body> {
| ^^^^^^^^^^^ this module is private
|
note: the module `app_service` is defined here
--> /home/bob/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-2.0.0/src/lib.rs:82:1
|
82 | mod app_service;
| ^^^^^^^^^^^^^^^^
error[E0277]: the trait bound `impl std::ops::Fn<()>: std::clone::Clone` is not satisfied
--> src/main.rs:29:21
|
29 | HttpServer::new(routes())
| ^^^^^^^^ the trait `std::clone::Clone` is not implemented for `impl std::ops::Fn<()>`
|
= note: required by `actix_web::server::HttpServer::<F, I, S, B>::new`
error[E0277]: the trait bound `impl std::ops::Fn<()>: std::clone::Clone` is not satisfied
--> src/main.rs:29:5
|
29 | HttpServer::new(routes())
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `impl std::ops::Fn<()>`
|
= note: required by `actix_web::server::HttpServer`
So how can I do separation of routes list from the server declaration?
robjtede commented
There is a .configure method on most routing table structs that can delegate to a function for setting up (sub)routes.
See docs at the bottom of this page https://actix.rs/docs/application/
amber-m-o-r-e commented
Thanks, it does the job.