
NG是什么意思:深度解析與應用
actix-web =
"
4
"
是一個基于 Rust 的框架,用于構建 Web 應用程序。
serde = "1.0.136"
是一個用于序列化和反序列化 Rust 數據結構的框架。例如,將 Rust 結構轉換為 JSON。
dotenv = "0.15.0"
是一個用于管理環境變量的庫。
futures = "0.3"
是一個用 Rust 進行異步編程的庫
[dependencies.mongodb]
是用于連接 MongoDB 的驅動程序。它還指定所需的版本和功能類型(異步 API)。
我們需要運行以下命令來安裝依賴項:
cargo build
安裝項目依賴項之后,請修改位于src
文件夾中的main.rs
文件,將其內容更新為以下所示:
use actix_web::{get, App, HttpResponse, HttpServer, Responder};
#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().json("Hello from rust and mongoDB")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(hello))
.bind(("localhost", 8080))?
.run()
.await
}
上面的代碼片段執行以下操作:
hello
的處理程序,該程序利用Rust宏來指定HTTP方法以及路由路徑/
,并且返回JSON格式的數據,內容為“Hello from rust and mongoDB
”。#[actix_web::main]
宏在Actix運行時內異步運行一個main
函數,該main
函數執行以下操作:
HttpServer
服務器,該服務器通過閉包與一個App
實例相結合,用于處理傳入的請求。App
負責注冊各種處理程序,例如hello
處理程序。在這個架構中,HttpServer
扮演著應用程序核心支柱的角色,它處理請求、管理允許的最大連接數、實施分層安全性等關鍵任務;而App
則專注于處理應用程序邏輯,包括請求處理程序、中間件、路由等。localhost:8080
上的 HTTP 請求。接下來,我們可以通過在終端中運行以下命令來測試我們的應用程序。
cargo run
在Rust中,模塊是一種機制,用于將代碼拆分為可重用的組件,并管理這些組件之間的可見性。模塊有助于我們維護項目,使其具有良好的結構。
為此,我們需要導航到該src
文件夾??并創建api
、models
和repository
文件夾以及相應的mod.rs
文件來管理可見性。
api
用于模塊化 API 處理程序。
models
用于模塊化數據邏輯。
repository
用于模塊化數據庫邏輯。
添加對模塊的引用
要使用模塊中的代碼,我們需要將它們聲明為模塊并將它們導入到main.rs
文件中。
//add the modules
mod api;
mod models;
mod repository;
use actix_web::{get, App, HttpResponse, HttpServer, Responder};
// the remaining part of our code goes here
完成后,我們需要登錄或注冊我們的MongoDB帳戶。單擊項目下拉菜單,然后單擊“新建項目”按鈕。
輸入rust-api
為項目名稱,單擊Next,然后單擊Create Project..
單擊構建數據庫
選擇共享作為數據庫類型。
單擊“創建”以設置集群。這可能需要一些時間來設置。
接下來,我們需要創建一個用戶來從外部訪問數據庫,輸入用戶名、密碼,然后單擊創建用戶。我們還需要點擊“添加我當前的IP地址”按鈕,以便添加IP地址從而安全地連接到數據庫。之后,點擊“完成并關閉”以保存所做的更改。
保存更改后,我們應該看到數據庫部署屏幕,如下所示:
完成配置后,我們需要將應用程序與創建的數據庫連接起來。為此,請單擊“連接”按鈕
單擊“連接您的應用程序”,將“驅動程序”更改為“版本”,如下所示。然后單擊復制圖標復制連接字符串。
設置環境變量
接下來,我們需要使用之前創建的用戶密碼來修改復制的連接字符串,并同時更改數據庫的名稱。為了完成這個任務,首先,我們需要在項目的根目錄下創建一個名為.env
的文件,并將復制的代碼片段粘貼到這個文件中。
MONGOURI=mongodb+srv://<YOUR USERNAME HERE>:<YOUR PASSWORD HERE>@cluster0.e5akf.mongodb.net/myFirstDatabese?retryWrites=true&w=majority
下面是正確填充的連接字符串的示例:
MONGOURI=mongodb+srv://malomz:malomzPassword@cluster0.e5akf.mongodb.net/golangDB?retryWrites=true&w=majority
設置完成后,我們需要創建一個模型來表示我們的應用程序數據。為此,我們需要導航到該models
文件夾??,并在此文件夾中創建一個user_model.rs
文件并添加以下代碼片段:
use mongodb::bson::oid::ObjectId;
use serde::{Serialize, Deserialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct User {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<ObjectId>,
pub name: String,
pub location: String,
pub title: String,
}
上面的代碼片段執行以下操作:
derive
宏生成對格式化輸出、序列化和反序列化數據結構的實現支持。User
具有所需屬性的結構。我們還向屬性添加了字段屬性id
,以重命名并忽略該字段(如果該字段為空)。PS:修飾符pub
使結構及其屬性公開,并且可以從其他文件/模塊訪問。
接下來,我們需要將user_model.rs
文件注冊為models
模塊的一部分。為此,請打開位于models
文件夾中的mod.rs
文件,并添加以下代碼片段:
pub mod user_model;
創建用戶端點
模型完全設置并可供使用后,我們現在可以創建數據庫邏輯來創建用戶。為此,首先,我們需要導航到該repository
文件夾??,并在此文件夾中創建一個mongodb_repo.rs
文件并添加以下代碼片段:
use std::env;
extern crate dotenv;
use dotenv::dotenv;
use mongodb::{
bson::{extjson::de::Error},
results::{ InsertOneResult},
Client, Collection,
};
use crate::models::user_model::User;
pub struct MongoRepo {
col: Collection<User>,
}
impl MongoRepo {
pub async fn init() -> Self {
dotenv().ok();
let uri = match env::var("MONGOURI") {
Ok(v) => v.to_string(),
Err(_) => format!("Error loading env variable"),
};
let client = Client::with_uri_str(uri).unwrap();
let db = client.database("rustDB");
let col: Collection<User> = db.collection("User");
MongoRepo { col }
}
pub async fn create_user(&self, new_user: User) -> Result<InsertOneResult, Error> {
let new_doc = User {
id: None,
name: new_user.name,
location: new_user.location,
title: new_user.title,
};
let user = self
.col
.insert_one(new_doc, None)
.await
.ok()
.expect("Error creating user");
Ok(user)
}
}
上面的代碼片段執行以下操作:
MongoRepo
帶有col
字段的結構體來訪問 MongoDB 集合MongoRepo
創建一個向結構體添加方法的實現塊MongoRepo
結構體的實現塊中添加一個方法,這個方法的作用是加載環境變量、建立到數據庫的連接,并最終返回MongoRepo
的一個實例,我們可以將這個方法命名為init
。create_user
方法,該方法接受self
和new_user
作為參數并返回創建的用戶或錯誤。在該方法內部,我們首先利用User
結構體創建了一個新的文檔。接著,我們通過self
引用來訪問MongoRepo
結構體中的insert_one
函數(該函數作用于某個集合),以便創建新的用戶。在處理過程中,我們還對可能出現的錯誤進行了處理。最后,方法返回了所創建的用戶信息。接下來,我們必須將該mongodb_repo.rs
文件注冊為repository
模塊的一部分。為了完成這個任務,請打開位于repository
文件夾中的mod.rs
文件,并向其中添加以下代碼片段:
pub mod mongodb_repos;
其次,我們需要創建一個處理程序,使用 create_user
中的方法來創建用戶。為此,我們需要導航到該api
文件夾??,并在此文件夾中創建一個user_api.rs
文件并添加以下代碼片段:
use crate::{models::user_model::User, repository::mongodb_repo::MongoRepo};
use actix_web::{
post,
web::{Data, Json},
HttpResponse,
};
#[post("/user")]
pub async fn create_user(db: Data<MongoRepo>, new_user: Json<User>) -> HttpResponse {
let data = User {
id: None,
name: new_user.name.to_owned(),
location: new_user.location.to_owned(),
title: new_user.title.to_owned(),
};
let user_detail = db.create_user(data).await;
match user_detail {
Ok(user) => HttpResponse::Ok().json(user),
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
}
}
上面的代碼片段執行以下操作:
create_user
處理程序,該處理程序接受db
、 MongoRepo
的類型和 一個new_user
作為參數。在處理程序內部,我們創建了一個data
用于創建用戶的變量,我們應該使用該方法將數據插入到數據庫的db.create_user
中,并根據插入操作是否成功或者是否發生錯誤,返回相應的響應。PS: 定義參數時使用的 DataJson
和結構分別用于管理跨路由共享的應用程序狀態和從請求負載中提取 JSON 數據。
最后,我們需要修改應用程序入口點以包含create_user
處理程序。為此,我們需要導航到該main.rs
文件并對其進行修改,如下所示:
mod api;
mod models;
mod repository;
//modify imports below
use actix_web::{web::Data, App, HttpServer};
use api::user_api::{create_user};
use repository::mongodb_repo::MongoRepo;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let db = MongoRepo::init().await;
let db_data = Data::new(db);
HttpServer::new(move || {
App::new()
.app_data(db_data.clone())
.service(create_user)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
上面的代碼片段執行以下操作:
db
變量以通過調用該方法建立與 MongoDB 的連接init()
,并將其添加到該Data
結構的新實例中,以便數據庫狀態可以在整個應用程序范圍內可用。app_data
和service
函數將應用程序數據和處理程序添加到App
實例。PS:關鍵字move
用于閉包中,它的作用是賦予閉包對MongoDB配置的所有權。
獲取用戶端點
要獲取用戶的詳細信息,我們必須首先通過get_user
向實現塊mongodb_repo.rs
添加方法來修改文件。
use std::env;
extern crate dotenv;
use dotenv::dotenv;
use mongodb::{
bson::{extjson::de::Error, oid::ObjectId, doc}, //modify here
results::{ InsertOneResult},
Client, Collection,
};
use crate::models::user_model::User;
pub struct MongoRepo {
col: Collection<User>,
}
impl MongoRepo {
pub async fn init() -> Self {
//init code goes here
}
pub async fn create_user(&self, new_user: User) -> Result<InsertOneResult, Error> {
//create_user code goes here
}
pub async fn get_user(&self, id: &String) -> Result<User, Error> {
let obj_id = ObjectId::parse_str(id).unwrap();
let filter = doc! {"_id": obj_id};
let user_detail = self
.col
.find_one(filter, None)
.await
.ok()
.expect("Error getting user's detail");
Ok(user_detail.unwrap())
}
}
上面的代碼片段執行以下操作:
oid::ObjectId
和doc
,我們需要對依賴項進行修改。get_user
方法,該方法接受self
和id
作為參數并返回用戶詳細信息或錯誤。在方法內部,我們將 id
轉換為 ObjectId
并將其用作過濾器來獲取匹配的文檔。然后,我們使用self
引用該MongoRepo
結構體來訪問find_one
集合中的函數,以獲取用戶的詳細信息并處理錯誤。最后我們返回了創建的用戶信息。其次,我們需要user_api.rs
通過創建一個處理程序來進行修改,該處理程序使用 get_user
中的方法來repository
獲取用戶。
use crate::{models::user_model::User, repository::mongodb_repo::MongoRepo};
use actix_web::{
post, get, //modify here
web::{Data, Json, Path}, //modify here
HttpResponse,
};
#[post("/user")]
pub async fn create_user(db: Data<MongoRepo>, new_user: Json<User>) -> HttpResponse {
//create_user code goes here
}
#[get("/user/{id}")]
pub async fn get_user(db: Data<MongoRepo>, path: Path<String>) -> HttpResponse {
let id = path.into_inner();
if id.is_empty() {
return HttpResponse::BadRequest().body("invalid ID");
}
let user_detail = db.get_user(&id).await;
match user_detail {
Ok(user) => HttpResponse::Ok().json(user),
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
}
}
上面的代碼片段執行以下操作:
get
和Path
get_user
處理程序,它接收db
(數據庫連接或實例)、MongoRepo
(數據庫操作倉庫的實例)以及path
(路由路徑參數)作為輸入。在處理程序內部,我們首先從請求中提取用戶ID并存儲在變量id
中,然后調用db
(或MongoRepo
的)get_user
方法根據ID獲取用戶信息。若請求成功且用戶信息被檢索到,則返回包含用戶信息的正確響應;若請求失敗或發生錯誤,則返回相應的錯誤響應。最后,我們需要修改應用程序入口點(main.rs
)get_user
以通過導入處理程序并為其添加新服務來包含該處理程序。
mod api;
mod models;
mod repository;
//modify imports below
use actix_web::{web::Data, App, HttpServer};
use api::user_api::{create_user, get_user}; //import the handler here
use repository::mongodb_repo::MongoRepo;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let db = MongoRepo::init().await;
let db_data = Data::new(db);
HttpServer::new(move || {
App::new()
.app_data(db_data.clone())
.service(create_user)
.service(get_user) //add this
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
編輯用戶端點
要編輯用戶,我們必須首先通過edit_user
向實現塊mongodb_repo.rs
添加方法來修改文件。
use std::env;
extern crate dotenv;
use dotenv::dotenv;
use mongodb::{
bson::{extjson::de::Error, oid::ObjectId, doc},
results::{ InsertOneResult, UpdateResult}, //modify here
Client, Collection,
};
use crate::models::user_model::User;
pub struct MongoRepo {
col: Collection<User>,
}
impl MongoRepo {
pub async fn init() -> Self {
//init code goes here
}
pub async fn create_user(&self, new_user: User) -> Result<InsertOneResult, Error> {
//create_user code goes here
}
pub async fn get_user(&self, id: &String) -> Result<User, Error> {
//get_user code goes here
}
pub async fn update_user(&self, id: &String, new_user: User) -> Result<UpdateResult, Error> {
let obj_id = ObjectId::parse_str(id).unwrap();
let filter = doc! {"_id": obj_id};
let new_doc = doc! {
"$set":
{
"id": new_user.id,
"name": new_user.name,
"location": new_user.location,
"title": new_user.title
},
};
let updated_doc = self
.col
.update_one(filter, new_doc, None)
.await
.ok()
.expect("Error updating user");
Ok(updated_doc)
}
}
上面的代碼片段執行以下操作:
UpdateResult
update_user
方法,該方法接受 self
、id
和new_user
參數并返回更新的用戶詳細信息或錯誤。在該方法內部,我們將 id
轉換為ObjectId
,創建一個filter
變量來獲取我們想要更新的匹配文檔,并使用doc
宏來更新文檔字段。然后,我們使用self
引用該MongoRepo
結構體來訪問update_one
集合中的函數,以更新filter
與指定匹配的用戶并處理錯誤。最后我們返回了更新后的用戶信息。其次,我們需要user_api.rs
通過創建一個處理程序來進行修改,該處理程序使用 中的update_user
方法repository
來更新用戶。
use crate::{models::user_model::User, repository::mongodb_repo::MongoRepo};
use actix_web::{
post, get, put, //modify here
web::{Data, Json, Path},
HttpResponse,
};
use mongodb::bson::oid::ObjectId; //add this
#[post("/user")]
pub async fn create_user(db: Data<MongoRepo>, new_user: Json<User>) -> HttpResponse {
//create_user code goes here
}
#[get("/user/{id}")]
pub async fn get_user(db: Data<MongoRepo>, path: Path<String>) -> HttpResponse {
//get_user code goes here
}
#[put("/user/{id}")]
pub async fn update_user(
db: Data<MongoRepo>,
path: Path<String>,
new_user: Json<User>,
) -> HttpResponse {
let id = path.into_inner();
if id.is_empty() {
return HttpResponse::BadRequest().body("invalid ID");
};
let data = User {
id: Some(ObjectId::parse_str(&id).unwrap()),
name: new_user.name.to_owned(),
location: new_user.location.to_owned(),
title: new_user.title.to_owned(),
};
let update_result = db.update_user(&id, data).await;
match update_result {
Ok(update) => {
if update.matched_count == 1 {
let updated_user_info = db.get_user(&id).await;
return match updated_user_info {
Ok(user) => HttpResponse::Ok().json(user),
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
};
} else {
return HttpResponse::NotFound().body("No user found with specified ID");
}
}
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
}
}
上面的代碼片段執行以下操作:
put
和ObjectId
update_user
處理程序,該處理程序接受Mongo
、Repo
、path
和db
的類型作為參數。在處理程序內部,我們創建了一個變量來獲取用戶的id?,并使用該方法通過傳入更新的用戶信息來更新數據庫中的用戶詳細信息。最后,我們會驗證更新操作是否順利完成,并根據結果返回更新后的用戶信息或相應的錯誤信息(如果更新過程中出現了任何問題)。最后,我們需要修改應用程序入口點(main.rs
)以通過導入處理程序并為其添加新服務來包含該處理程序。
mod api;
mod models;
mod repository;
//modify imports below
use actix_web::{web::Data, App, HttpServer};
use api::user_api::{create_user, get_user, update_user}; //import the handler here
use repository::mongodb_repo::MongoRepo;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let db = MongoRepo::init().await;
let db_data = Data::new(db);
HttpServer::new(move || {
App::new()
.app_data(db_data.clone())
.service(create_user)
.service(get_user)
.service(update_user) //add this
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
刪除用戶端點
要刪除用戶,我們必須首先通過delete_user
向實現塊mongodb_repo.rs
添加方法來修改文件。
use std::env;
extern crate dotenv;
use dotenv::dotenv;
use mongodb::{
bson::{extjson::de::Error, oid::ObjectId, doc},
results::{ InsertOneResult, UpdateResult, DeleteResult}, //modify here
Client, Collection,
};
use crate::models::user_model::User;
pub struct MongoRepo {
col: Collection<User>,
}
impl MongoRepo {
pub async fn init() -> Self {
//init code goes here
}
pub async fn create_user(&self, new_user: User) -> Result<InsertOneResult, Error> {
//create_user code goes here
}
pub async fn get_user(&self, id: &String) -> Result<User, Error> {
//get_user code goes here
}
pub async fn update_user(&self, id: &String, new_user: User) -> Result<UpdateResult, Error> {
//update_user code goes here
}
pub async fn delete_user(&self, id: &String) -> Result<DeleteResult, Error> {
let obj_id = ObjectId::parse_str(id).unwrap();
let filter = doc! {"_id": obj_id};
let user_detail = self
.col
.delete_one(filter, None)
.await
.ok()
.expect("Error deleting user");
Ok(user_detail)
}
}
上面的代碼片段執行以下操作:
DeleteResult
delete_user
方法,該方法接受 self
和id
作為參數并返回已刪除的用戶詳細信息或錯誤。在該方法內部,我們將 轉換id
為 ObjectId
并創建一個filter
變量來獲取我們要刪除的匹配文檔。然后,我們使用self
引用該MongoRepo
結構體來訪問delete_one
集合中的函數,以刪除filter
與指定匹配的用戶并處理錯誤。最后我們返回了刪除的用戶信息。接下來,我們需要在user_api.rs
中創建一個處理程序,這個處理程序會調用repository
中的某個方法(我們假設這個方法命名為delete_user
)來刪除指定的用戶。
use crate::{models::user_model::User, repository::mongodb_repo::MongoRepo};
use actix_web::{
post, get, put, delete, //modify here
web::{Data, Json, Path},
HttpResponse,
};
use mongodb::bson::oid::ObjectId; //add this
#[post("/user")]
pub async fn create_user(db: Data<MongoRepo>, new_user: Json<User>) -> HttpResponse {
//create_user code goes here
}
#[get("/user/{id}")]
pub async fn get_user(db: Data<MongoRepo>, path: Path<String>) -> HttpResponse {
//get_user code goes here
}
#[put("/user/{id}")]
pub async fn update_user(
db: Data<MongoRepo>,
path: Path<String>,
new_user: Json<User>,
) -> HttpResponse {
//update_user code goes here
}
#[delete("/user/{id}")]
pub async fn delete_user(db: Data<MongoRepo>, path: Path<String>) -> HttpResponse {
let id = path.into_inner();
if id.is_empty() {
return HttpResponse::BadRequest().body("invalid ID");
};
let result = db.delete_user(&id).await;
match result {
Ok(res) => {
if res.deleted_count == 1 {
return HttpResponse::Ok().json("User successfully deleted!");
} else {
return HttpResponse::NotFound().json("User with specified ID not found!");
}
}
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
}
}
上面的代碼片段執行以下操作:
delete
delete_user
處理程序,該處理程序接受Mongo
、Repo
和 path
的db
類型作為參數。在處理程序內部,我們創建了一個名為id
的變量來獲取需要刪除的用戶ID。然后,我們調用了repository
中的delete_user
方法,并將id
作為參數傳遞進去,以刪除指定的用戶。最后,我們根據刪除操作的結果返回了相應的響應或錯誤(如果有)。最后,我們需要修改應用程序入口點(main.rs
)以通過導入處理程序并為其添加新服務來包含該處理程序。
mod api;
mod models;
mod repository;
//modify imports below
use actix_web::{web::Data, App, HttpServer};
use api::user_api::{create_user, get_user, update_user, delete_user}; //import the handler here
use repository::mongodb_repo::MongoRepo;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let db = MongoRepo::init().await;
let db_data = Data::new(db);
HttpServer::new(move || {
App::new()
.app_data(db_data.clone())
.service(create_user)
.service(get_user)
.service(update_user)
.service(delete_user) //add this
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
獲取所有用戶端點
要獲取用戶列表,我們必須首先通過get_all_users
向實現塊mongodb_repo.rs
添加方法來修改文件。
use std::env;
extern crate dotenv;
use dotenv::dotenv;
use mongodb::{
bson::{extjson::de::Error, oid::ObjectId, doc},
results::{ InsertOneResult, UpdateResult, DeleteResult},
Client, Collection,
};
use futures::stream::TryStreamExt; //add this
use crate::models::user_model::User;
pub struct MongoRepo {
col: Collection<User>,
}
impl MongoRepo {
pub async fn init() -> Self {
//init code goes here
}
pub async fn create_user(&self, new_user: User) -> Result<InsertOneResult, Error> {
//create_user code goes here
}
pub async fn get_user(&self, id: &String) -> Result<User, Error> {
//get_user code goes here
}
pub async fn update_user(&self, id: &String, new_user: User) -> Result<UpdateResult, Error> {
//update_user code goes here
}
pub async fn delete_user(&self, id: &String) -> Result<DeleteResult, Error> {
//delete_user code goes here
}
pub async fn get_all_users(&self) -> Result<Vec<User>, Error> {
let mut cursors = self
.col
.find(None, None)
.await
.ok()
.expect("Error getting list of users");
let mut users: Vec<User> = Vec::new();
while let Some(user) = cursors
.try_next()
.await
.ok()
.expect("Error mapping through cursor")
{
users.push(user)
}
Ok(users)
}
}
上面的代碼片段添加了一個get_all_users
方法,該方法接受 a self
作為參數并返回用戶列表或錯誤。在方法內部,我們使用self
引用結構MongoRepo
體從集合中訪問find
函數,無需任何過濾器,以便它可以匹配數據庫內的所有文檔,使用該try_next()
方法循環遍歷用戶列表以最佳方式返回列表,并處理錯誤。
其次,我們需要在user_api.rs
中創建一個新的處理程序,這個處理程序會利用repository
中提供的get_all_users
方法來獲取用戶列表。
use crate::{models::user_model::User, repository::mongodb_repo::MongoRepo};
use actix_web::{
post, get, put, delete,
web::{Data, Json, Path},
HttpResponse,
};
use mongodb::bson::oid::ObjectId;
#[post("/user")]
pub async fn create_user(db: Data<MongoRepo>, new_user: Json<User>) -> HttpResponse {
//create_user code goes here
}
#[get("/user/{id}")]
pub async fn get_user(db: Data<MongoRepo>, path: Path<String>) -> HttpResponse {
//get_user code goes here
}
#[put("/user/{id}")]
pub async fn update_user(
db: Data<MongoRepo>,
path: Path<String>,
new_user: Json<User>,
) -> HttpResponse {
//update_user code goes here
}
#[delete("/user/{id}")]
pub async fn delete_user(db: Data<MongoRepo>, path: Path<String>) -> HttpResponse {
//delet_user code goes here
}
#[get("/users")]
pub async fn get_all_users(db: Data<MongoRepo>) -> HttpResponse {
let users = db.get_all_users().await;
match users {
Ok(users) => HttpResponse::Ok().json(users),
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
}
}
上面的代碼片段執行以下操作:
get_all_users
使用該db.delete_user
方法獲取用戶列表的處理程序。然后,我們返回用戶列表或錯誤(如果有)。最終,我們需要更新應用程序的入口文件main.rs
,通過導入新的處理程序,并將它們作為新的服務添加到服務集合中,以確保它們能夠被正確地路由和處理。
mod api;
mod models;
mod repository;
//modify imports below
use actix_web::{web::Data, App, HttpServer};
use api::user_api::{create_user, get_user, update_user, delete_user, get_all_users}; //import the handler here
use repository::mongodb_repo::MongoRepo;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let db = MongoRepo::init().await;
let db_data = Data::new(db);
HttpServer::new(move || {
App::new()
.app_data(db_data.clone())
.service(create_user)
.service(get_user)
.service(update_user)
.service(delete_user)
.service(get_all_users)//add this
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
完成后,我們可以通過在終端中運行以下命令來測試我們的應用程序。
cargo run
這篇文章討論了如何模塊化 Rust 應用程序、構建 REST API 以及使用 MongoDB 保存我們的數據。
原文鏈接:https://dev.to/hackmamba/build-a-rest-api-with-rust-and-mongodb-actix-web-version-ei1