我自己铆接了这样一个结构,然后根据我的想法,我可以开发它
| src /
| - api /
| | -patterns /
| | | - mod.rs
| | | - patterns.rs
| | |_________
| | - mod.rs
| |______________
|
| - models /
| | - mod.rs
| | - pattern_model.rs
| | - pattern_asset_model.rs
| | - pattern_folder_model.rs
| | - device_model.rs
| | - device_asset_model.rs
| | - device_folder_model.rs
| | - universal_pattern_model.rs
| |_________________
|
| - repository /
| | | - mod.rs
| | | - mongodb_repo.rs
| |_________________
|
| - img /
| |
| |
| |_________________
|
| - .env
| - main.rs
|_________________________
- Cargo.lock
- Cargo.toml
api - 旨在调整 API 处理程序。模型 - 旨在调制数据逻辑。存储库 - 旨在调整数据库逻辑。但这不是问题的重点(尽管如果有人有任何意见,请不要害羞)。
我正在尝试通过 Rocket 从数据库启动一台服务器,然后简单地将一条记录添加到集合中。
// main.rs
mod api;
mod models;
mod repository;
#[macro_use]
extern crate rocket;
use api::patterns::patterns::{create_pattern};
use repository::mongodb_repo::MongoRepo;
#[launch]
fn rocket() -> _ {
let db = MongoRepo::init();
rocket::build().manage(db)
.mount("/api/usepi/patterns", routes![create_pattern])
}
这是数据库初始化代码..
//mongodb_repo.rs
use std::env;
extern crate dotenv;
use dotenv::dotenv;
use mongodb::{Client, Collection, options::ClientOptions};
use mongodb::error::Error;
use mongodb::results::InsertOneResult;
use crate::models::pattern_model::Pattern as ModelPattern;
pub struct MongoRepo {
pattern_collection: Collection<ModelPattern>,
}
impl MongoRepo {
pub async fn init() -> Self {
dotenv().ok();
let mongodb_uri = dotenv::var("MONGODB_URI");
println!("AAAAAA {:?}",mongodb_uri);
let client_options = ClientOptions::parse(mongodb_uri).await?;
let client = Client::with_options(client_options)?;
let db = client.database("RustBlueTractorDB");
let pattern_collection: Collection<ModelPattern> = db.collection("Pattern");
MongoRepo { pattern_collection }
}
.....
}
但这样的错误总是会发生:
error[E0277]: the trait bound `Result<std::string::String, dotenv::Error>: AsRef<str>` is not satisfied
--> src/repository/mongodb_repo.rs:26:55
|
26 | let client_options = ClientOptions::parse(mongodb_uri).await?;
| -------------------- ^^^^^^^^^^^ the trait `AsRef<str>` is not implemented for `Result<std::string::String, dotenv::Error>`
| |
| required by a bound introduced by this call
|
note: required by a bound in `ClientOptions::parse`
当你修复一件事时,会出现一个错误,并显示“rocket::build().manage(db)..”,然后又回到这一点。我尝试按照手册进行操作(虽然数量不是很多,但由于各种原因它们都不起作用)。在 .env 中我有..
MONGODB_URI=mongodb://192.168.1.63:27018/
这是pattern_model 中的内容...
use mongodb::bson::oid::ObjectId;
use serde::{Serialize, Deserialize};
#[derive(Debug, Deserialize, Serialize)]
pub struct PatternRequest {
pub data: Pattern,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Pattern {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<ObjectId>,
pub title: String,
pub description: String,
pub icon: String,
pub pattern: String,
pub create_date: String,
pub update_date: String,
pub version: String,
pub favourite: bool,
}
还有 toml:
[package]
name = "rust"
version = "0.1.0"
edition = "2021"
[dependencies]
rocket = {version = "0.5.0-rc.2", features = ["json"]}
serde = "1.0.136"
dotenv = "0.15.0"
chrono = "0.4.31"
[dependencies.mongodb]
version = "2.8.0"
default-features = false
features = ["async-std-runtime"]
问题是,我该如何做到这一点?我出于紧急需要而从nestJS切换过来,如果通常的功能看起来可以工作,那么这是一个大问题,我不明白。提前致谢!