我无法让Rocket 车把示例 工作.这些是我的Cargo.toml依赖项:
[dependencies]
rocket = "*"
rocket_codegen = "*"
rocket_contrib = "*"
serde = "*"
serde_json = "*"
serde_derive = "*"
Run Code Online (Sandbox Code Playgroud)
错误:
error[E0432]: unresolved import `rocket_contrib::Template`
--> src\main.rs:29:5
|
29 | use rocket_contrib::Template;
| ^^^^^^^^^^^^^^^^^^^^^^^^ no `Template` in the root
error[E0599]: no method named `attach` found for type `rocket::Rocket` in the current scope
--> src\main.rs:62:10
|
62 | .attach(Template::fairing())
| ^^^^^^
Run Code Online (Sandbox Code Playgroud)
第一个错误查找Template并找不到它.在该示例的git repo中,它不存在.该示例如何工作?我确信我的main.rs中的Rust代码没问题,它与示例中的相同.我认为这只是一个依赖性问题.
我将Cargo.toml更改为:
[dependencies]
rocket = "*"
rocket_codegen = "*"
serde = "*"
serde_json …Run Code Online (Sandbox Code Playgroud) 我正在使用Rust和Rocket构建REST API.我有一个端点,我在其中创建一个新用户,定义如下:
/// View with which to create a user
#[post("/users", format = "application/json", data = "<user_data>")]
fn create_user(user_data: Json<UserData>, db: DB) -> Status<Json<Value>> {
let conn = db.conn();
let _new_user_result = user_data.into_new_user(&conn);
unimplemented!()
}
Run Code Online (Sandbox Code Playgroud)
请注意,此处没有借用内容; 既user_data和db所有.不过,我在编译时遇到以下错误:
error[E0507]: cannot move out of borrowed content
--> src/views/user_account.rs:75:28
|
75 | let _new_user_result = user_data.into_new_user(&conn);
| ^^^^^^^^^ cannot move out of borrowed content
Run Code Online (Sandbox Code Playgroud)
供参考,功能签名into_new_user是
fn into_new_user(self, conn: &SqliteConnection) -> Result<NewUser, Status<Json<Value>>> {
...
}
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?如果我实际借用任何东西,这个错误会更容易理解,但鉴于我拥有所有问题,我感到很困惑.
$ …Run Code Online (Sandbox Code Playgroud) 我正在使用Rocket State,它传递给HTTP请求.此结构包含一个Mutex<DatastoreInstance>访问SQLite数据库的框架,并使用互斥锁锁定以使读写安全.
pub struct DatastoreInstance {
conn: Connection,
}
Run Code Online (Sandbox Code Playgroud)
当DatastoreInstance结构看起来像这样时,只有一个SQLite连接一切正常,但我还想在这个结构中添加一个事务对象:
pub struct DatastoreInstance {
conn: Connection,
events_transaction: Transaction,
}
Run Code Online (Sandbox Code Playgroud)
这没有编译,因为Transaction对象需要引用一个Connection应该具有它所知道的生命周期的对象.我正在使用的rusqlite中的Connection和Transaction对象定义如下:
pub struct Connection {
db: RefCell<InnerConnection>,
cache: StatementCache,
path: Option<PathBuf>,
}
pub struct Transaction<'conn> {
conn: &'conn Connection,
drop_behavior: DropBehavior,
}
Run Code Online (Sandbox Code Playgroud)
要解决生命周期问题,我必须添加这些生命周期参数才能使其正常工作:
pub struct DatastoreInstance<'a> {
conn: Connection,
events_transaction: Transaction<'a>,
}
Run Code Online (Sandbox Code Playgroud)
这是结果,并且应该根据我对生命周期和互斥体的理解而工作,但是我现在得到编译器错误告诉我:
`std::cell::RefCell<lru_cache::LruCache<std::string::String, rusqlite::raw_statement::RawStatement>>` cannot be shared between threads safely
|
= help: within `rusqlite::Connection`, the trait …Run Code Online (Sandbox Code Playgroud) 如何将具有非静态寿命的对象传递给Rocket manage?目前,我遵循以下原则:
fn foo<'a>(bar: Bar<'a>) -> Result<(), Error> {
rocket::ignite()
.manage(bar)
.mount("/", routes![index])
.launch();
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
cannot infer an appropriate lifetime due to conflicting requirements
note: ...so that the expression is assignable:
expected bar::Bar<'_>
found bar::Bar<'a>
note: but, the lifetime must be valid for the static lifetime...
Run Code Online (Sandbox Code Playgroud)
要添加更多上下文,Bar是一个struct包含框的闭包,它们使用运行时参数进行初始化。args包含密码,密钥和机密之类的东西-实际代码是开源的,因此可以在此处找到。它是WIP,所以会有所变化,并且不是最新的,但希望能为最终目标提供一个思路。
我有一个运行 Rocket.rs 的后端,我的 Flutter Web 应用程序向它发送请求,但它无法通过 OPTIONS 响应。
我曾尝试将 CORS (rocket_cors) 添加到后端并有一个选项响应,但它仍然发回:
Error: XMLHttpRequest error.
dart:sdk_internal 124039:30 get current
packages/http/src/browser_client.dart.lib.js 214:124 <fn>
Run Code Online (Sandbox Code Playgroud)
我在我的火箭项目中添加了以下内容:
Error: XMLHttpRequest error.
dart:sdk_internal 124039:30 get current
packages/http/src/browser_client.dart.lib.js 214:124 <fn>
Run Code Online (Sandbox Code Playgroud)
我的颤振应用程序正在运行此请求:
Future<String> fetchData() async {
final data2 = await http.get("http://my-web-site.com").then((response) { // doesn't get past here
return response.body;
});
return data2;
}
Run Code Online (Sandbox Code Playgroud)
问题:这是响应 OPTION 请求的正确方法,如果不是,我如何在 Rocket.rs 中实现它?
我有一个关于将数据结构插入数据库的问题,但我似乎找不到任何相关文档。
我有一个数据结构
#[derive(FromRow, Getters, Default, Serialize, Deserialize, Debug)]
#[serde(crate = "rocket::serde")]
#[getset(get = "pub")]
pub struct RefreshKeys {
id: i64,
customer_id: i64,
key: String,
enabled: bool,
}
Run Code Online (Sandbox Code Playgroud)
我想将其插入到具有相同字段的数据库中,称为refresh_keys.
rocket_db_pools::sqlx::query_as::<_, RefreshKeys>(
"INSERT INTO refresh_keys (id, customer_id, key, enabled)
VALUES (?1, ?2, ?3, ?4)"
)
.fetch_one(&mut *db)
.await?
Run Code Online (Sandbox Code Playgroud)
遗憾的是,这似乎不起作用,我收到以下错误:
SqliteError { code: 1299, message: "NOT NULL constraint failed: refresh_keys.customer_id" }
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几个小时来查找相关文档,但我什么也没找到。
提前致谢!
我无法理解这段代码的生命周期之间的关系。基本上,我有一个接收一些x-www-form-urlencoded数据的 Rocket API,只有一个键:json. 这个键直观地包含一个 JSON 值,用百分比编码,一个 struct Message<T>。
(我知道这是次优的 API 设计,但这是逆向工程工作,所以我别无选择)
为了方便地用作请求保护器From<Message<T>>,我正在实现FromForm. 为此,我需要FromForm<'f>为任何Message<T>where Timplements 实现Deserialize<'de>。我将我的 impl 签名写为impl<'f, 'de, T> FromForm<'f> for Message<T> where T: Deserialize<'de>.
为了实际执行解码,我:
"json"表单数据的key;尽快脱险。这样做的代码(为方便读者使用显式类型注释):
fn from_form(items: &mut FormItems<'f>, strict: bool) -> Result<Self, Self::Error> {
// Get JSON field
let encoded: Option<&RawStr> = items.find(|&(k, _)| k.as_str() == "json")
.map(|(_, v)| v);
if let …Run Code Online (Sandbox Code Playgroud) 我正在使用Rocket设置Rust服务器,我正在尝试将它与JWT库一起使用.他们使用*ring*crate的不同版本,我在以下期间收到错误cargo build:
error: multiple packages link to native library `ring-asm`, but a native library can be linked only once
package `ring v0.12.1`
... which is depended on by `jsonwebtoken v4.0.1`
... which is depended on by `auther v0.1.0 (file:///home/drpytho/x/downloadble/auther)`
links to native library `ring-asm`
package `ring v0.11.0`
... which is depended on by `cookie v0.9.2`
... which is depended on by `rocket v0.3.6`
... which is depended on by `rocket_codegen v0.3.6`
... which is …Run Code Online (Sandbox Code Playgroud) Responder在使用 Rocket.rs 的 Web 服务器应用程序中,我使用在整个 API 中实现的错误类型。此错误类型可确保统一呈现所有错误(如 RFC 7807 json)。
但是,我找不到在RequestGuards. 该from_request函数似乎会导致Outcome使用完全不同的模型,并返回Outcome::Failure((Status, T))错误。
如何确保这些请求防护中的错误以相同的 JSON 格式呈现?它甚至可以定制吗?
我尝试使用捕手,但这似乎没有检索任何错误信息。
我正在尝试使用Rocket crate创建后端:
fn main() {
rocket::ignite().mount("/", routes![helloPost]).launch();
}
#[derive(Debug, PartialEq, Eq, RustcEncodable, FromForm)]
struct User {
id: i64,
USR_Email: String,
USR_Password: String,
USR_Enabled: i32,
USR_MAC_Address: String
}
#[post("/", data = "<user_input>")]
fn helloPost(user_input: Form<User>) -> String {
println!("print test {}", user_input);
}
Run Code Online (Sandbox Code Playgroud)
当我运行时cargo run一切正常,但是当我使用邮递员发送 POST 请求进行测试时,我收到此错误:
fn main() {
rocket::ignite().mount("/", routes![helloPost]).launch();
}
#[derive(Debug, PartialEq, Eq, RustcEncodable, FromForm)]
struct User {
id: i64,
USR_Email: String,
USR_Password: String,
USR_Enabled: i32,
USR_MAC_Address: String
}
#[post("/", data = "<user_input>")]
fn helloPost(user_input: …Run Code Online (Sandbox Code Playgroud) rust ×10
rust-rocket ×10
api ×1
dart ×1
dependencies ×1
flutter-web ×1
http ×1
json ×1
mutex ×1
rust-sqlx ×1
serde ×1
sqlite ×1