我创建了一个 docker 镜像,其中包含一个 rust 应用程序,该应用程序响应端口 8000 上的获取请求。应用程序本身是一个使用火箭库 ( https://rocket.rs/ )的基本示例,它看起来像这样
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
fn main() {
rocket::ignite().mount("/", routes![index]).launch();
}
Run Code Online (Sandbox Code Playgroud)
我已经编译了这个并称之为 server
然后我创建了一个 Docker 文件来托管它
FROM ubuntu:16.04
RUN apt-get update; apt-get install -y curl
COPY server /root/
EXPOSE 8000
CMD ["/root/server"]
Run Code Online (Sandbox Code Playgroud)
我构建 docker 镜像
$ docker build -t port_test并运行它$ docker run -p 8000:8000 port_test
在这一点上,一切看起来都不错
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Rocket 构建一个 Rust 项目,但在编译时我总是遇到此错误,即使在成功覆盖项目的工具链之后也是如此:
D:\GitHub\Learning-Rust\poke_api> rustup override set nightly
info: using existing install for 'nightly-x86_64-pc-windows-msvc'
info: override toolchain for 'D:\GitHub\Learning-Rust\poke_api' set to 'nightly-x86_64-pc-windows-msvc'
nightly-x86_64-pc-windows-msvc unchanged - rustc 1.52.0-nightly (4a8b6f708 2021-03-11)
PS D:\GitHub\Learning-Rust\poke_api> cargo build
Compiling winapi v0.3.9
Compiling serde_derive v1.0.124
Compiling rocket v0.4.7
Compiling pear_codegen v0.1.4
Compiling rocket_codegen v0.4.7
Compiling proc-macro2 v1.0.24
Compiling pq-sys v0.4.6
Compiling aho-corasick v0.6.10
Compiling serde_json v1.0.64
error: …Run Code Online (Sandbox Code Playgroud) 我使用Rocket库,我需要创建一个端点,其中包含动态参数"type",一个关键字.
我试过这样的东西,但它没有编译:
#[get("/offers?<type>")]
pub fn offers_get(type: String) -> Status {
unimplemented!()
}
Run Code Online (Sandbox Code Playgroud)
编译器错误:
error: expected argument name, found keyword `type`
Run Code Online (Sandbox Code Playgroud)
是否有可能在火箭中有一个名为"type"的参数?由于我遵循的规范,我无法重命名参数.
我正在尝试获得以下场景的工作原型:
rustc 1.45.0-nightly (ad4bc3323 2020-06-01))运行bazel build //web-api产生以下错误。我相信,基于查看Cargo.lock文件,这是因为 Rocket 对hyper库的依赖指定了对库的依赖log 0.3.9。无论出于何种原因,它都没有使用更新的log=0.4.x. 也就是说,我不知道它为什么要拉这个库,因为如果我手动构建它,它工作正常。
ERROR: /private/var/tmp/_bazel_nathanielford/2a39169ea9f6eb02fe788b12f9eae88f/external/raze__log__0_3_9/BUILD.bazel:27:1: error executing shell command: '/bin/bash -c CARGO_MANIFEST_DIR=$(pwd)/external/raze__log__0_3_9 external/rust_darwin_x86_64/bin/rustc "$@" --remap-path-prefix="$(pwd)"=__bazel_redacted_pwd external/raze__log__0_3_9/src/lib.rs -...' failed (Exit 1) bash failed: error executing command /bin/bash -c 'CARGO_MANIFEST_DIR=$(pwd)/external/raze__log__0_3_9 external/rust_darwin_x86_64/bin/rustc "$@" --remap-path-prefix="$(pwd)"=__bazel_redacted_pwd' '' external/raze__log__0_3_9/src/lib.rs ... (remaining 24 argument(s) skipped)
Use --sandbox_debug to see verbose messages from the sandbox
error[E0425]: cannot …Run Code Online (Sandbox Code Playgroud) 我是铁锈和柴油的新手。并尝试使用火箭框架创建一个小型演示 api。
得到错误:不满足特征界限NaiveDateTime: Deserialize<'_>
我用谷歌搜索并找到了一些有用的链接,比如这里:https : //github.com/serde-rs/serde/issues/759
看起来版本有问题。
这是我的文件:
schema.rs
table! {
department (dept_id) {
dept_id -> Int4,
dept_name -> Nullable<Text>,
created_on -> Nullable<Timestamp>,
created_by -> Nullable<Text>,
modified_on -> Nullable<Timestamp>,
modified_by -> Nullable<Text>,
is_active -> Nullable<Bool>,
}
}
Run Code Online (Sandbox Code Playgroud)
货物.toml
[dependencies]
diesel = { version = "1.4.5", features = ["postgres","chrono","numeric"] }
dotenv = "0.15.0"
chrono = { version = "0.4.19" }
bigdecimal = { version = "0.1.0" }
rocket = "0.4.6"
rocket_codegen = "0.4.6"
r2d2-diesel = "1.0.0" …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用柴油将 postgres 数据库添加到火箭应用程序。我的main.rs文件看起来像这样,但给出了错误“该特征diesel::Connection未实现DbConnection”.get_result(connection)
#[macro_use] extern crate diesel;
extern crate dotenv;
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
use diesel::prelude::*;
use rocket_contrib::database;
use rocket_contrib::json::JsonValue;
mod models;
mod schema;
use self::models::*;
use self::schema::*;
#[database("my_db")]
struct DbConnection(diesel::PgConnection);
#[get("/")]
fn index(connection: DbConnection) -> JsonValue {
json!(all_bicycles(&connection))
}
fn create_bicycle<'a>(connection: &DbConnection, make: &'a str, model: &'a str, rider_type: &'a str, size: &'a str) -> Bicycle {
let new_bicycle = NewBicycle {
make,
model,
rider_type,
size
};
diesel::insert_into(bicycles::table) …Run Code Online (Sandbox Code Playgroud) 基本上我希望有一个进程同时处理多个事情(特别是希望有一个http端点来监视非http服务),但似乎我陷入了Rocket,具体取决于火箭特定的tokio运行时,我不知道如何解决这个问题。我曾考虑过让 Rocket 作为主要入口点,而不是 tokio 的标准入口点,并从该运行时启动其他东西,但这似乎是错误的,而且很脆弱,因为我可能会切换库。我考虑过使用 hyper 和 warp,并且我有点自信我可以让它们在这种情况下工作,但我想使用 Rocket,因为我在这个项目的其他地方使用它。我使用的是0.5-rc01版本。
来自文档:
Rocket v0.5 uses the tokio runtime. The runtime is started for you if you use #[launch] or #[rocket::main], but you can still launch() a Rocket instance on a custom-built runtime by not using either attribute.
Run Code Online (Sandbox Code Playgroud)
不幸的是,我找不到任何关于此定制运行时的要求的进一步解释,也找不到任何不使用启动/主宏的示例。
这是我尝试使用的代码的简化版本:
#[rocket::get("/ex")]
async fn test() -> &'static str {
"OK"
}
#[tokio::main]
async fn main() {
tokio::spawn(async {
let config = Config {
port: 8001,
address: std::net::Ipv4Addr::new(127, 0, 0, 1).into(),
..Config::debug_default()
};
rocket::custom(&config)
.mount("/", …Run Code Online (Sandbox Code Playgroud) 这是一个与火箭箱相关的非常具体的问题。我不确定这是板条箱的问题还是我只是做了一些明显错误的事情。我在生命周期和宏方面不是最好的。
\n我正在尝试设置 SSE 连接,并且希望宏借用传递到函数中的值。
\n我最初按照他们的教程设置 SSE,并编写了以下代码......
\n#[get("/my/stream")]\npub fn my_stream(config: &State<config::Config>, _pool: &State<Pool<Sqlite>>) -> EventStream![] {\n EventStream! {\n let mut id = 0;\n let mut interval = time::interval(Duration::from_secs(1));\n loop {\n yield Event::data("test data").event("test").id(id.to_string());\n interval.tick().await;\n id += 1;\n }\n } \n}\nRun Code Online (Sandbox Code Playgroud)\n这编译并工作得很好,但是当我尝试做一些简单的事情并借用 config 或 _pool 时,它会抱怨宏中的生命周期。编译器非常清晰,并且与他们文档中的内容相匹配。所以我将生命周期添加到返回值中。
\n#[get("/my/stream")]\npub fn my_stream(config: & State<config::Config>, _pool: &State<Pool<Sqlite>>) -> EventStream![Event + '_] {\n EventStream! {\n let _test = config.database.clone();\n let mut id = 0;\n let mut interval = time::interval(Duration::from_secs(1));\n loop {\n yield …Run Code Online (Sandbox Code Playgroud) 我正在与 Rust 和 Rocket 合作。我有一个端点可以一次上传一个文件form-data:
use rocket::form::{Form, FromForm};\nuse rocket::fs::TempFile;\nuse std::ffi::OsStr;\nuse std::path::{Path};\nuse uuid::Uuid;\n\n#[post("/file_upload", format = "multipart/form-data", data = "<form>")]\npub async fn file_upload(mut form: Form<Upload<\'_>>) -> std::io::Result<String> {\n // Get raw file \n let file_name = form.file.raw_name().unwrap().dangerous_unsafe_unsanitized_raw().as_str();name\n // Get extension of file name\n let extension = Path::new(file_name).extension().and_then(OsStr::to_str).unwrap(); \n // Generate new UUID\n let id: String = Uuid::new_v4().to_string(); \n // Build path to save file\n let file_path = String::from("media/temp_files") + "/" + &id + "." + extension; \n\n // Save …Run Code Online (Sandbox Code Playgroud) rust-rocket ×10
rust ×9
rust-diesel ×2
bazel ×1
curl ×1
docker ×1
networking ×1
postgresql ×1
rust-chrono ×1
rustup ×1
serde ×1