I am trying to figure out a better way to organize the routes in my Actix application, the examples on actix.rs don’t seem to have much on modularizing routes in other files. Currently I have something like this:
main.rs
HttpServer::new(move || {
App::new()
.service(web::scope("/api").configure(routes::user::config))
})
.bind(("127.0.0.1", 8000))?
.run()
.await
routes/user.rs
pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service(
web::resource("/user/login")
.route(web::post().to(login))
);
cfg.service(
web::resource("/user/{user_id}")
.route(web::get().to(retrieve))
.route(web::put().to(update))
);
cfg.service(
web::resource("/user")
.route(web::post().to(create))
);
}
In user.rs
I am calling cfg.service()
for each different route, which is working, but seems less than ideal. I feel like I should maybe be able to use cfg.service()
once for `/user’, then nest the others in that somehow. However, I can’t figure out how to do this.
Do I have to call cfg.service()
for each individual route or is there a way to nest this? Or am I missing something and there is a much better way to handle/organize the routes?