我正在开始使用 Rust 和 Rocket。
我正在按照 Rocket Getting Started 中的说明进行操作,但出现“错误:无法编译” state。完整日志在这里:`
~/.cargo/bin/cargo run 更新注册表
https://github.com/rust-lang/crates.io-index下载 Rocket_codegen v0.3.3 下载 Rocket v0.3.3
编译 gcc v0.3.54 编译 smallvec v0.4.4 编译 libc v0.2.34 编译 version_check v0.1.3 编译 language-tags v0.2.2 编译 serde v1.0.23 编译状态 v0.3.2 错误[E0554]: #![feature] 可能无法在稳定发布通道上使用 --> /Users/ktenjin/.cargo/registry/src/github.com-1ecc6299db9ec823/state- 0.3.2/src/lib.rs:1:1 | 1 | #![特征(const_fn)] | ^^^^^^^^^^^^^^^^^^^^^错误[E0554]:#![feature] 可能无法在稳定发布渠道上使用 --> /Users/ktenjin/.cargo/registry/src/github.com-1ecc6299db9ec823/state-0.3.2/src/lib。 rs:2:1 | 2 | #![特征(const_unsafe_cell_new)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
错误[E0554]:#![feature] 可能无法在稳定发布渠道上使用 --> /Users/ktenjin/.cargo/registry/src/github.com-1ecc6299db9ec823/state-0.3.2/src/lib。 rs:3:1 | 3 | #![特征(const_atomic_usize_new)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
错误[E0554]:#![feature] 可能无法在稳定发布渠道上使用 --> /Users/ktenjin/.cargo/registry/src/github.com-1ecc6299db9ec823/state-0.3.2/src/lib。 …
我正在使用 Rocket 创建一个带有身份验证的 web 应用程序。为此,我创建了一个User实现FromRequest. 它采用授权标头,其中包含一个 JSON Web 令牌。我反序列化此令牌以获取有效负载,然后从数据库中查询用户。这意味着FromRequest实现需要一个diesel::PgConnection. 在 Rocket 0.3 中,这意味着调用PgConnection::establish,但在 Rocket 0.4 中,我们可以访问连接池。通常我会按如下方式访问这个连接池:
fn get_data(conn: db::MyDatabasePool) -> MyModel {
MyModel::get(&conn)
}
Run Code Online (Sandbox Code Playgroud)
但是,在 impl 块中,FromRequest我不能只将conn参数添加到函数的参数列表中from_request。如何在请求保护之外访问我的连接池?
我的 Rocket 应用程序有以下工作数据库连接设置:
主要.rs:
#[database("my_db")]
pub struct DbConn(diesel::PgConnection);
Run Code Online (Sandbox Code Playgroud)
火箭.toml:
[global.databases]
my_db = { url = "postgres://user:pass@localhost/my_db" }
Run Code Online (Sandbox Code Playgroud)
我想从环境中设置用户名、密码和数据库名称。预计会是这样的ROCKET_MY_DB=postgres://user:pass@localhost/my_db,但没有成功。无法找到 Rocket 的相关数据库示例。
我需要在生产环境中为基于 Rocket 的应用程序运行 Diesel 数据库迁移。通常有几种方法可以为数据库执行迁移:
我更喜欢使用--migrate应用程序二进制文件的标志调用的第二个选项,但由于目标应用程序相当简单,第一种方法就可以了。
Diesel 问题跟踪器中有一个关于在生产中运行迁移的线程,并提供有关如何执行此操作的建议:
- 添加
diesel_migrations到您的依赖项- 包括
extern crate diesel_migrations在你的箱子,并确保与装饰它#[macro_use]- 在代码的开头,添加
embed_migrations!()- 要运行迁移,请使用
embedded_migrations::run(&db_conn)
在main.rs我做了:
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate rocket_contrib;
#[database("my_db_name")]
pub struct DbConn(diesel::PgConnection);
fn main() {
// Update database
embed_migrations!();
embedded_migrations::run(&DbConn);
// Launch the app
...
}
Run Code Online (Sandbox Code Playgroud)
这导致错误:
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
#[macro_use]
extern …Run Code Online (Sandbox Code Playgroud) 我正在使用 Rust 和 Rocket 构建一个简单的 REST API。其中一个端点接受 POST 方法请求,并从请求正文中读取一个大字符串。我不知道如何用火箭做到这一点。
该文档描述了如何从 POST 请求的正文中读取 JSON 对象,以及如何读取多部分表单数据,而不是原始字符串。有谁知道如何做到这一点?
更新:
按照下面 Dave 的回答中的建议,我实现了 FromDataSimple 特征来尝试解析请求正文。这是我已经实施的,但它只会导致“404 Not Found”响应:
struct Store {
contents: String,
}
impl FromDataSimple for Store {
type Error = String;
fn from_data(req: &Request, data: Data) -> data::Outcome<Self, String> {
let mut contents = String::new();
if let Err(e) = data.open().take(256).read_to_string(&mut contents) {
return Failure((Status::InternalServerError, format!("{:?}", e)));
}
Success(Store { contents })
}
}
#[post("/process", format = "application/json", data = "<input>")]
fn process_store(input: Store) -> …Run Code Online (Sandbox Code Playgroud) 我试图在两个文件之间共享一个结构,但出现错误。
我有以下文件夹结构:
src/
Models/
Login.rs
Routes/
LoginRoute.rs
Services/
LoginService.rs
main.rs
Run Code Online (Sandbox Code Playgroud)
在Login.rs我有:
#[derive(Serialize, Deserialize, Debug)]
pub struct UserLoginResponse {
id: i32,
username: String,
token: String
}
Run Code Online (Sandbox Code Playgroud)
在LoginRoute.rs我有:
#[path = "../Models/Login.rs"]
pub mod Login;
#[path = "../Services/LoginService.rs"]
pub mod LoginService;
#[post("/login", format = "application/json", data = "<user>")]
pub async fn login(user: String) -> Json<Login::UserLoginResponse> {
if let Ok(sk) = LoginService::callAuthenticate(user).await {
return sk
......
Run Code Online (Sandbox Code Playgroud)
在LoginService.rs我有:
#[path = "../Models/Login.rs"]
pub mod Login;
pub async fn callAuthenticate(user: String)-> …Run Code Online (Sandbox Code Playgroud) 我是 Rust lang 的新手。-> _返回对于函数来说意味着什么rocket?
#[launch]
fn rocket() -> _ {
rocket::build()
.attach(json::stage())
.attach(msgpack::stage())
.attach(uuid::stage())
}
Run Code Online (Sandbox Code Playgroud) 我有一个 Rust 服务器在我的机器上运行,本地主机,端口:4200。我正在尝试使用使用 axios 库的 JavaScript 客户端向该服务器发出请求。
运行时的代码给出以下错误:
错误:connect ECONNREFUSED 127.0.0.1:4200 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
我尝试重写代码以使用 fetch 库。这也返回连接拒绝错误。
从 Postman 尝试时,API 按要求工作。Get 调用也在浏览器中工作。从 JavaScript 调用时,无法找出此调用的连接被拒绝的原因。
我在 Rust 服务器中启用了 CORS 选项。
fn main() {
let options = rocket_cors::Cors::default();
rocket::ignite()
.mount("/", routes![index, sign, generate])
.attach(options)
.launch();
}
Run Code Online (Sandbox Code Playgroud)
编辑:
从我的机器运行时出现上述错误的客户端代码:
const fetch = require("node-fetch");
var requestOptions = {
method: "GET",
headers: { "Content-Type": "application/json" }
};
fetch("http://localhost:4200/createOffer/1/license", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log("error", error));
Run Code Online (Sandbox Code Playgroud)
在我的机器上工作的浏览器请求: http://localhost:4200/createOffer/1/license