Actix-web: Make module scope public

Created on 24 Jan 2020  路  2Comments  路  Source: actix/actix-web

My team and I would like that the scope module is turned public due to the fact that our project architecture should include all the web::scopes (code below) in a module called routes. This is so that the routes in integration tests are the same as the main and our code distribution seems cleaner. Something like the code below:

```rust
// todo_api_web/routes.rs
use actix_web::{web, App};

use todo_api_web::controller::{
pong, readiness,
todo::create_todo
};

pub fn get_routes() -> actix_web::scope::Scope {
web::scope("/")
.service(
web::scope("api/")
.route("create", web::post().to(create_todo))
)
.route("ping", web::get().to(pong))
.route("~/ready", web::get().to(readiness))
.route("", web::get().to(|| HttpResponse::NotFound()))
}

// main.rs
...
use todo_api_web::routes::get_routes;

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().service(
get_routes()
)
})
.workers(num_cpus::get() + 2)
.bind("127.0.0.1:4000")
.unwrap()
.run()
.await
}

// tests/todo_api_web/controller.rs
mod ping_readiness {
use todo_server::todo_api_web::{
routes::get_routes;
};

 use actix_web::{
     test, App,
 };

 #[actix_rt::test]
 async fn test_ping_pong() {
     let mut app = test::init_service(
         App::new().service(
             get_routes()
         )
     ).await;
     ...
 }

 #[actix_rt::test]
 async fn test_readiness_ok() {
     let mut app = test::init_service(
         App::new()
             .service(get_routes())
     ).await;
     ...
 }

}

mod create_todo {
use todo_server::todo_api_web::{
routes::get_routes;
};

 use actix_web::{
     test, App,
 };

 #[actix_rt::test]
 async fn valid_todo_post() {
     let mut app = test::init_service(
         App::new()
             .service(
                 get_routes()
         )
     ).await;
     ...
 }

}
```

Please consider this option

Most helpful comment

Thank you very much, and sorry I did not realize this before

All 2 comments

If I understand what you want correctly, you can achieve this using the .configure function on App or Scope. Here's an example using it.

Thank you very much, and sorry I did not realize this before

Was this page helpful?
0 / 5 - 0 ratings