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 });
|
||||
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;
|
||||
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);
|
||||
}
|
|
@ -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<DbPool>) -> Result<HttpResponse, Error> {
|
|||
.await?
|
||||
.map_err(actix_web::error::ErrorInternalServerError)?;
|
||||
|
||||
let renderer =
|
||||
ServerRenderer::<UsersComponent>::with_props(UsersComponentProps { users: users.clone() });
|
||||
let renderer = ServerRenderer::<UsersComponent>::with_props(UsersComponentProps {
|
||||
users: users.clone(),
|
||||
});
|
||||
Ok(HttpResponse::Ok().body(renderer.render().await))
|
||||
}
|
||||
|
||||
#[get("")]
|
||||
#[get("create")]
|
||||
async fn create(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> {
|
||||
let user = User::new(NewUser {
|
||||
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() });
|
||||
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_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()
|
||||
|
|
Loading…
Reference in a new issue