-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement basic RESTful API endpoints for user management (#1)
- Loading branch information
Showing
4 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mod web_development; | ||
|
||
pub fn run() { | ||
println!("Days 16-18: Web Development"); | ||
web_development::examples(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use actix_web::{web, App, HttpResponse, HttpServer, Responder}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
// định nghĩa user | ||
#[derive(Serialize, Deserialize)] | ||
struct User { | ||
id: u32, | ||
name: String, | ||
} | ||
|
||
// hàm req GET để lấy danh sách user | ||
async fn get_users() -> impl Responder { | ||
let users = vec![ | ||
User { id: 1, name: "Alice".to_string() }, | ||
User { id: 2, name: "Bob".to_string() }, | ||
]; | ||
HttpResponse::Ok().json(users) | ||
} | ||
|
||
// hàm req GET để get thông tin một detail của user | ||
async fn get_user(path: web::Path<u32>) -> impl Responder { | ||
let user_id = path.into_inner(); | ||
let user = User { id: user_id, name: format!("User {}", user_id) }; | ||
HttpResponse::Ok().json(user) | ||
} | ||
|
||
// hàm req POST để tạo user mới | ||
async fn create_user(user: web::Json<User>) -> impl Responder { | ||
HttpResponse::Created().json(user.into_inner()) | ||
} | ||
|
||
// server | ||
#[actix_web::main] | ||
async fn main() -> std::io::Result<()> { | ||
println!("Khởi động máy chủ web tại http://127.0.0.1:8080"); | ||
|
||
HttpServer::new(|| { | ||
App::new() | ||
.route("/users", web::get().to(get_users)) | ||
.route("/users/{id}", web::get().to(get_user)) | ||
.route("/users", web::post().to(create_user)) | ||
}) | ||
.bind("127.0.0.1:8080")? | ||
.run() | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters