Actix grouping routes

hello there hope you having great day and good coding, i just wanted to know how to group routes in actix, i read the docs from the page, but to me wasn't completely clear, i wanna know how to group routes from a different file than the main.rs i mean, i don't know how many of you are familiar with express.js framework but to give an example of what i mean

//something_controller.js
import express from 'express';
const router = express.Router();
router.get("/myroute",(req,res)=>{some code})

export default router;

then in index.js

import somethingRouter from 'routes/something_controller.js'

//then to use that group of routes
app.use('/api/something',somethingRouter)

i know that i can configure a scope in the main file by adding a scope in

#[actix_web::main]
async fn main()->std::io::Result<()> {
    let db = MongoRepo::innit().await;
    let db_data = Data::new(db);
    HttpServer::new(move||{
      let my_controller = web::scope("/something") // i wanna have this scope in another file and call it in the main so i can modularize the code and have the main file as clean as posible like with express shown in the example before
               .service(add_item)
               .service(get_item)
               .service(update_item)
               .service(delete_item)
        App::new()
            .app_data(db_data.clone())
    })
    .bind(("localhost",4000))?
    .run()
    .await
}

how can i do that, thanks for reading, and the help

You just need to use App::configure function which exists for this purpose, example is in documentation. You can call on cfg there all relevant methods.

I personally just put my routes in different modules but still construct the app in one single place as I find that that is the version that is most readable. Say you put your endpoints in a module endpoints, I'd still explicitly construct the scope in main:

mod endpoints;

#[actix_web::main]
async fn main()->std::io::Result<()> {
    let db = MongoRepo::innit().await;
    let db_data = Data::new(db);
    HttpServer::new(move||{
      let my_controller = web::scope("/something") // i wanna have this scope in another file and call it in the main so i can modularize the code and have the main file as clean as posible like with express shown in the example before
               .service(endpoints::add_item)
               .service(endpoints::get_item)
               .service(endpoints::update_item)
               .service(endpoints::delete_item)
        App::new()
            .app_data(db_data.clone())
            .service(my_controller)
    })
    .bind(("localhost",4000))?
    .run()
    .await
}

But you can also just return a Scope from a function and pass that to App:

use actix_web::{web, Scope};

fn my_controller() -> Scope {
    web::scope("/something")
        .service(add_item)
        .service(get_item)
        .service(update_item)
        .service(delete_item)
}

#[actix_web::main]
async fn main()->std::io::Result<()> {
    let db = MongoRepo::innit().await;
    let db_data = Data::new(db);
    HttpServer::new(move|| {
        App::new()
            .app_data(db_data.clone())
            .service(my_controller())
    })
    .bind(("localhost",4000))?
    .run()
    .await
}
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.