diff --git a/src/controllers/hello_controller.rs b/src/controllers/hello_controller.rs index 32d4c32..6d5c040 100644 --- a/src/controllers/hello_controller.rs +++ b/src/controllers/hello_controller.rs @@ -9,3 +9,7 @@ async fn index(name: web::Path) -> impl Responder { let renderer = ServerRenderer::::with_props(HelloProps { name }); HttpResponse::Ok().body(renderer.render().await) } + +pub fn init_routes(cfg: &mut web::ServiceConfig) { + cfg.service(web::scope("/hello").service(index)); +} diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index cc62811..27a47aa 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -1,2 +1,9 @@ -pub mod hello_controller; -pub mod users_controller; +use actix_web::web; + +mod hello_controller; +mod users_controller; + +pub fn init_routes(cfg: &mut web::ServiceConfig) { + cfg.configure(hello_controller::init_routes); + cfg.configure(users_controller::init_routes); +} \ No newline at end of file diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs index 30cac02..87760d9 100644 --- a/src/controllers/users_controller.rs +++ b/src/controllers/users_controller.rs @@ -5,7 +5,7 @@ use crate::{ models::user::{NewUser, User}, views::components::{ user::{UserComponent, UserComponentProps}, - users::{UsersComponent, UsersComponentProps} + users::{UsersComponent, UsersComponentProps}, }, DbPool, }; @@ -19,12 +19,13 @@ async fn index(pool: web::Data) -> Result { .await? .map_err(actix_web::error::ErrorInternalServerError)?; - let renderer = - ServerRenderer::::with_props(UsersComponentProps { users: users.clone() }); + let renderer = ServerRenderer::::with_props(UsersComponentProps { + users: users.clone(), + }); Ok(HttpResponse::Ok().body(renderer.render().await)) } -#[get("")] +#[get("create")] async fn create(pool: web::Data) -> Result { let user = User::new(NewUser { name: "John".to_string(), @@ -42,3 +43,7 @@ async fn create(pool: web::Data) -> Result { ServerRenderer::::with_props(UserComponentProps { user: user.clone() }); Ok(HttpResponse::Ok().body(renderer.render().await)) } + +pub fn init_routes(cfg: &mut web::ServiceConfig) { + cfg.service(web::scope("/users").service(index).service(create)); +} diff --git a/src/main.rs b/src/main.rs index 92a96ec..e30be12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,9 +30,7 @@ async fn main() -> std::io::Result<()> { App::new() .app_data(web::Data::new(pool.clone())) .wrap(middleware::Logger::default()) - .service(web::scope("/hello").service(controllers::hello_controller::index)) - .service(web::scope("/users").service(controllers::users_controller::index)) - .service(web::scope("/user").service(controllers::users_controller::create)) + .configure(controllers::init_routes) }) .bind(("127.0.0.1", 8000))? .run()