Refactor controllers declaration
This commit is contained in:
parent
d20ca7cfa9
commit
6facdae336
4 changed files with 23 additions and 9 deletions
|
@ -9,3 +9,7 @@ async fn index(name: web::Path<String>) -> impl Responder {
|
||||||
let renderer = ServerRenderer::<Hello>::with_props(HelloProps { name });
|
let renderer = ServerRenderer::<Hello>::with_props(HelloProps { name });
|
||||||
HttpResponse::Ok().body(renderer.render().await)
|
HttpResponse::Ok().body(renderer.render().await)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn init_routes(cfg: &mut web::ServiceConfig) {
|
||||||
|
cfg.service(web::scope("/hello").service(index));
|
||||||
|
}
|
||||||
|
|
|
@ -1,2 +1,9 @@
|
||||||
pub mod hello_controller;
|
use actix_web::web;
|
||||||
pub mod users_controller;
|
|
||||||
|
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);
|
||||||
|
}
|
|
@ -5,7 +5,7 @@ use crate::{
|
||||||
models::user::{NewUser, User},
|
models::user::{NewUser, User},
|
||||||
views::components::{
|
views::components::{
|
||||||
user::{UserComponent, UserComponentProps},
|
user::{UserComponent, UserComponentProps},
|
||||||
users::{UsersComponent, UsersComponentProps}
|
users::{UsersComponent, UsersComponentProps},
|
||||||
},
|
},
|
||||||
DbPool,
|
DbPool,
|
||||||
};
|
};
|
||||||
|
@ -19,12 +19,13 @@ async fn index(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
||||||
.await?
|
.await?
|
||||||
.map_err(actix_web::error::ErrorInternalServerError)?;
|
.map_err(actix_web::error::ErrorInternalServerError)?;
|
||||||
|
|
||||||
let renderer =
|
let renderer = ServerRenderer::<UsersComponent>::with_props(UsersComponentProps {
|
||||||
ServerRenderer::<UsersComponent>::with_props(UsersComponentProps { users: users.clone() });
|
users: users.clone(),
|
||||||
|
});
|
||||||
Ok(HttpResponse::Ok().body(renderer.render().await))
|
Ok(HttpResponse::Ok().body(renderer.render().await))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("")]
|
#[get("create")]
|
||||||
async fn create(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
async fn create(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
||||||
let user = User::new(NewUser {
|
let user = User::new(NewUser {
|
||||||
name: "John".to_string(),
|
name: "John".to_string(),
|
||||||
|
@ -42,3 +43,7 @@ async fn create(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
||||||
ServerRenderer::<UserComponent>::with_props(UserComponentProps { user: user.clone() });
|
ServerRenderer::<UserComponent>::with_props(UserComponentProps { user: user.clone() });
|
||||||
Ok(HttpResponse::Ok().body(renderer.render().await))
|
Ok(HttpResponse::Ok().body(renderer.render().await))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn init_routes(cfg: &mut web::ServiceConfig) {
|
||||||
|
cfg.service(web::scope("/users").service(index).service(create));
|
||||||
|
}
|
||||||
|
|
|
@ -30,9 +30,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
App::new()
|
App::new()
|
||||||
.app_data(web::Data::new(pool.clone()))
|
.app_data(web::Data::new(pool.clone()))
|
||||||
.wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
.service(web::scope("/hello").service(controllers::hello_controller::index))
|
.configure(controllers::init_routes)
|
||||||
.service(web::scope("/users").service(controllers::users_controller::index))
|
|
||||||
.service(web::scope("/user").service(controllers::users_controller::create))
|
|
||||||
})
|
})
|
||||||
.bind(("127.0.0.1", 8000))?
|
.bind(("127.0.0.1", 8000))?
|
||||||
.run()
|
.run()
|
||||||
|
|
Loading…
Reference in a new issue