[dependencies]
actix-web = "4"
serde = "1.0.136"
dotenv = "0.15.0"
futures = "0.3"

[dependencies.mongodb]
version = "2.2.0"
default-features = false
features = ["async-std-runtime"]

actix-web = "4"是一個(gè)基于 Rust 的框架,用于構(gòu)建 Web 應(yīng)用程序。

serde = "1.0.136"是一個(gè)用于序列化和反序列化 Rust 數(shù)據(jù)結(jié)構(gòu)的框架。例如,將 Rust 結(jié)構(gòu)轉(zhuǎn)換為 JSON。

dotenv = "0.15.0"是一個(gè)用于管理環(huán)境變量的庫(kù)。

futures = "0.3"是一個(gè)用 Rust 進(jìn)行異步編程的庫(kù)

[dependencies.mongodb]是用于連接 MongoDB 的驅(qū)動(dòng)程序。它還指定所需的版本和功能類(lèi)型(異步 API)。

我們需要運(yùn)行以下命令來(lái)安裝依賴項(xiàng):

 cargo build

應(yīng)用程序入口點(diǎn)

安裝項(xiàng)目依賴項(xiàng)之后,請(qǐng)修改位于src文件夾中的main.rs文件,將其內(nèi)容更新為以下所示:

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
}

上面的代碼片段執(zhí)行以下操作:

接下來(lái),我們可以通過(guò)在終端中運(yùn)行以下命令來(lái)測(cè)試我們的應(yīng)用程序。

cargo run
測(cè)試應(yīng)用程序

Rust 中的模塊系統(tǒng)

在Rust中,模塊是一種機(jī)制,用于將代碼拆分為可重用的組件,并管理這些組件之間的可見(jiàn)性。模塊有助于我們維護(hù)項(xiàng)目,使其具有良好的結(jié)構(gòu)。

為此,我們需要導(dǎo)航到該src文件夾??并創(chuàng)建api、modelsrepository文件夾以及相應(yīng)的mod.rs 文件來(lái)管理可見(jiàn)性。

更新了項(xiàng)目文件夾結(jié)構(gòu)

api用于模塊化 API 處理程序。

models用于模塊化數(shù)據(jù)邏輯。

repository用于模塊化數(shù)據(jù)庫(kù)邏輯。

添加對(duì)模塊的引用
要使用模塊中的代碼,我們需要將它們聲明為模塊并將它們導(dǎo)入到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

設(shè)置 MongoDB

完成后,我們需要登錄或注冊(cè)我們的MongoDB帳戶。單擊項(xiàng)目下拉菜單,然后單擊“新建項(xiàng)目”按鈕。

新項(xiàng)目

輸入rust-api為項(xiàng)目名稱,單擊Next,然后單擊Create Project..

輸入項(xiàng)目名稱
創(chuàng)建項(xiàng)目

單擊構(gòu)建數(shù)據(jù)庫(kù)

選擇共享作為數(shù)據(jù)庫(kù)類(lèi)型。

共享內(nèi)容以紅色突出顯示

單擊“創(chuàng)建”以設(shè)置集群。這可能需要一些時(shí)間來(lái)設(shè)置。

創(chuàng)建集群

接下來(lái),我們需要?jiǎng)?chuàng)建一個(gè)用戶來(lái)從外部訪問(wèn)數(shù)據(jù)庫(kù),輸入用戶名、密碼,然后單擊創(chuàng)建用戶。我們還需要點(diǎn)擊“添加我當(dāng)前的IP地址”按鈕,以便添加IP地址從而安全地連接到數(shù)據(jù)庫(kù)。之后,點(diǎn)擊“完成并關(guān)閉”以保存所做的更改。

創(chuàng)建用戶
添加IP

保存更改后,我們應(yīng)該看到數(shù)據(jù)庫(kù)部署屏幕,如下所示:

數(shù)據(jù)庫(kù)屏幕

將我們的應(yīng)用程序連接到 MongoDB

完成配置后,我們需要將應(yīng)用程序與創(chuàng)建的數(shù)據(jù)庫(kù)連接起來(lái)。為此,請(qǐng)單擊“連接”按鈕

連接到數(shù)據(jù)庫(kù)

單擊“連接您的應(yīng)用程序”,將“驅(qū)動(dòng)程序”更改為“版本”,如下所示。然后單擊復(fù)制圖標(biāo)復(fù)制連接字符串。

連接應(yīng)用程序
復(fù)制連接字符串

設(shè)置環(huán)境變量
接下來(lái),我們需要使用之前創(chuàng)建的用戶密碼來(lái)修改復(fù)制的連接字符串,并同時(shí)更改數(shù)據(jù)庫(kù)的名稱。為了完成這個(gè)任務(wù),首先,我們需要在項(xiàng)目的根目錄下創(chuàng)建一個(gè)名為.env的文件,并將復(fù)制的代碼片段粘貼到這個(gè)文件中。

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

創(chuàng)建 REST API

設(shè)置完成后,我們需要?jiǎng)?chuàng)建一個(gè)模型來(lái)表示我們的應(yīng)用程序數(shù)據(jù)。為此,我們需要導(dǎo)航到該models文件夾??,并在此文件夾中創(chuàng)建一個(gè)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,
}

上面的代碼片段執(zhí)行以下操作:

PS修飾符pub使結(jié)構(gòu)及其屬性公開(kāi),并且可以從其他文件/模塊訪問(wèn)。

接下來(lái),我們需要將user_model.rs文件注冊(cè)為models模塊的一部分。為此,請(qǐng)打開(kāi)位于models文件夾中的mod.rs文件,并添加以下代碼片段:

pub mod user_model;

創(chuàng)建用戶端點(diǎn)
模型完全設(shè)置并可供使用后,我們現(xiàn)在可以創(chuàng)建數(shù)據(jù)庫(kù)邏輯來(lái)創(chuàng)建用戶。為此,首先,我們需要導(dǎo)航到該repository文件夾??,并在此文件夾中創(chuàng)建一個(gè)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)
}
}

上面的代碼片段執(zhí)行以下操作:

接下來(lái),我們必須將該mongodb_repo.rs文件注冊(cè)為repository模塊的一部分。為了完成這個(gè)任務(wù),請(qǐng)打開(kāi)位于repository文件夾中的mod.rs文件,并向其中添加以下代碼片段:

pub mod mongodb_repos;

其次,我們需要?jiǎng)?chuàng)建一個(gè)處理程序,使用 create_user中的方法來(lái)創(chuàng)建用戶。為此,我們需要導(dǎo)航到該api文件夾??,并在此文件夾中創(chuàng)建一個(gè)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()),
}
}

上面的代碼片段執(zhí)行以下操作:

PS: 定義參數(shù)時(shí)使用的 DataJson和結(jié)構(gòu)分別用于管理跨路由共享的應(yīng)用程序狀態(tài)和從請(qǐng)求負(fù)載中提取 JSON 數(shù)據(jù)。

最后,我們需要修改應(yīng)用程序入口點(diǎn)以包含create_user處理程序。為此,我們需要導(dǎo)航到該main.rs文件并對(duì)其進(jìn)行修改,如下所示:

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
}

上面的代碼片段執(zhí)行以下操作:

PS:關(guān)鍵字move用于閉包中,它的作用是賦予閉包對(duì)MongoDB配置的所有權(quán)。

獲取用戶端點(diǎn)
要獲取用戶的詳細(xì)信息,我們必須首先通過(guò)get_user向?qū)崿F(xiàn)塊mongodb_repo.rs添加方法來(lái)修改文件。

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())
}
}

上面的代碼片段執(zhí)行以下操作:

其次,我們需要user_api.rs通過(guò)創(chuàng)建一個(gè)處理程序來(lái)進(jìn)行修改,該處理程序使用 get_user中的方法來(lái)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()),
}
}

上面的代碼片段執(zhí)行以下操作:

最后,我們需要修改應(yīng)用程序入口點(diǎn)(main.rsget_user以通過(guò)導(dǎo)入處理程序并為其添加新服務(wù)來(lái)包含該處理程序。

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
}

編輯用戶端點(diǎn)
要編輯用戶,我們必須首先通過(guò)edit_user向?qū)崿F(xiàn)塊mongodb_repo.rs添加方法來(lái)修改文件。

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)
}
}

上面的代碼片段執(zhí)行以下操作:

其次,我們需要user_api.rs通過(guò)創(chuàng)建一個(gè)處理程序來(lái)進(jìn)行修改,該處理程序使用 中的update_user方法repository來(lái)更新用戶。

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()),
}
}

上面的代碼片段執(zhí)行以下操作:

最后,我們需要修改應(yīng)用程序入口點(diǎn)(main.rs)以通過(guò)導(dǎo)入處理程序并為其添加新服務(wù)來(lái)包含該處理程序。

 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
}

刪除用戶端點(diǎn)
要?jiǎng)h除用戶,我們必須首先通過(guò)delete_user向?qū)崿F(xiàn)塊mongodb_repo.rs添加方法來(lái)修改文件。

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)
}
}

上面的代碼片段執(zhí)行以下操作:

接下來(lái),我們需要在user_api.rs中創(chuàng)建一個(gè)處理程序,這個(gè)處理程序會(huì)調(diào)用repository中的某個(gè)方法(我們假設(shè)這個(gè)方法命名為delete_user)來(lái)刪除指定的用戶。

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()),
}
}

上面的代碼片段執(zhí)行以下操作:

最后,我們需要修改應(yīng)用程序入口點(diǎn)(main.rs)以通過(guò)導(dǎo)入處理程序并為其添加新服務(wù)來(lái)包含該處理程序。

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
}

獲取所有用戶端點(diǎn)
要獲取用戶列表,我們必須首先通過(guò)get_all_users向?qū)崿F(xiàn)塊mongodb_repo.rs添加方法來(lái)修改文件。

 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)
}
}

上面的代碼片段添加了一個(gè)get_all_users方法,該方法接受 a self作為參數(shù)并返回用戶列表或錯(cuò)誤。在方法內(nèi)部,我們使用self引用結(jié)構(gòu)MongoRepo體從集合中訪問(wèn)find函數(shù),無(wú)需任何過(guò)濾器,以便它可以匹配數(shù)據(jù)庫(kù)內(nèi)的所有文檔,使用該try_next()方法循環(huán)遍歷用戶列表以最佳方式返回列表,并處理錯(cuò)誤。

其次,我們需要在user_api.rs中創(chuàng)建一個(gè)新的處理程序,這個(gè)處理程序會(huì)利用repository中提供的get_all_users方法來(lái)獲取用戶列表。

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()),
}
}

上面的代碼片段執(zhí)行以下操作:

最終,我們需要更新應(yīng)用程序的入口文件main.rs,通過(guò)導(dǎo)入新的處理程序,并將它們作為新的服務(wù)添加到服務(wù)集合中,以確保它們能夠被正確地路由和處理。

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
}

完成后,我們可以通過(guò)在終端中運(yùn)行以下命令來(lái)測(cè)試我們的應(yīng)用程序。

cargo run
創(chuàng)建用戶端點(diǎn)
獲取用戶端點(diǎn)
編輯用戶端點(diǎn)
刪除用戶端點(diǎn)
獲取用戶端點(diǎn)列表
包含用戶文檔的數(shù)據(jù)庫(kù)

結(jié)論

這篇文章討論了如何模塊化 Rust 應(yīng)用程序、構(gòu)建 REST API 以及使用 MongoDB 保存我們的數(shù)據(jù)。

原文鏈接:https://dev.to/hackmamba/build-a-rest-api-with-rust-and-mongodb-actix-web-version-ei1

上一篇:

如何通過(guò)API驅(qū)動(dòng)的數(shù)字生態(tài)系統(tǒng)推動(dòng)創(chuàng)新和效率

下一篇:

改變醫(yī)療保?。簽獒t(yī)療行業(yè)構(gòu)建一個(gè)API市場(chǎng)
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門(mén)場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)