Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to split server routes declaration in actix-web?

Tags:

rust

actix-web

I have the following server declaration right now

let server = HttpServer::new(move || {
    App::new()
        .app_data(actix_web::web::Data::new(pool.clone()))
        .service(ping)
        .service(stock::controller::get_all)
        .service(stock::controller::find_one)
        .service(stock::controller::insert_one)
        .service(stock::controller::insert_many)
})
.bind(("127.0.0.1", 7777))?
.run();

I feel like it will be very hard to control it when I'll add other routes. Is there a way to split it so I could have something like

App::new()
        .app_data(actix_web::web::Data::new(pool.clone()))
        .service(ping)
        .service(stock::controller::routes)

And have the routes function add all the services declared in first code sample?

like image 655
nxyt Avatar asked Oct 23 '25 08:10

nxyt


1 Answers

You can create a route scope:

HttpServer::new(|| {
    let stocks_controller = actix_web::web::scope("/stocks")
        .service(get_all)
        .service(find_one)
        .service(insert_one);
    App::new()
        .service(ping)
        .service(stocks_controller)
})

More info here.

like image 192
aedm Avatar answered Oct 26 '25 03:10

aedm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!