Fat*_*sim 1 static-files rust rust-actix actix-web
我正在为 rust 的 actix-web 2.0 框架苦苦挣扎。我希望我的 Rust 服务器为我的 index.html 文件提供服务,但大多数可用的帮助都是旧版本,因此在新版本中发生了很多变化。我尝试了以下代码,但它不适用于 actix-web 2.0。请在 actix-web 2.0 中提出一些可行的解决方案。对不起,因为我对 Rust 很陌生。
use actix_files::NamedFile;
use actix_web::{HttpRequest, Result};
async fn index(req: HttpRequest) -> Result<NamedFile> {
Ok(NamedFile::open(path_to_file)?)
}
Run Code Online (Sandbox Code Playgroud)
编辑 1:我收到类型不匹配错误,但使用评论中回答的 pathBuf 拯救了我。
Edit2:通过尝试回答中给出的代码,我可以提供单个 html 文件,但无法加载链接的 javascript 文件。我已经尝试了https://actix.rs/docs/static-files/ 中建议的以下方法来为目录提供服务
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
dotenv::dotenv().ok();
std::env::set_var("RUST_LOG", "actix_web=debug");
let database_url = std::env::var("DATABASE_URL").expect("set DATABASE_URL");
// create db connection pool
let manager = ConnectionManager::<PgConnection>::new(database_url);
let pool: Pool = r2d2::Pool::builder()
.build(manager)
.expect("Failed to create pool.");
//Serving the Registration and sign-in page
async fn index(_req: HttpRequest) -> Result<NamedFile> {
let path: PathBuf = "./static/index.html".parse().unwrap();
Ok(NamedFile::open(path)?)
}
// Start http server
HttpServer::new(move || {
App::new()
.data(pool.clone())
.service(fs::Files::new("/static", ".").show_files_listing())
.route("/", web::get().to(index))
.route("/users", web::get().to(handler::get_users))
.route("/users/{id}", web::get().to(handler::get_user_by_id))
.route("/users", web::post().to(handler::add_user))
.route("/users/{id}", web::delete().to(handler::delete_user))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
Run Code Online (Sandbox Code Playgroud)
以上是我的主要方法。在浏览器控制台中,我仍然收到无法加载 Registration.js 资源的错误。以下是我的文件夹结构:
-migrations
-src
-main.rs
-handler.rs
-errors.rs
-models.rs
-schema.rs
-static
-index.html
-Registration.js
-target
Cargo.toml
.env
Cargo.lock
diesel.toml
Run Code Online (Sandbox Code Playgroud)
我已经使用 db 集成构建了后端,并且通过 curl 命令检查它工作正常,现在我正在尝试构建前端并作为尝试提供静态文件的第一步。
Edit3:这个例子也帮助我和这里的答案一起解决这个问题是https://github.com/actix/examples/tree/master/static_index
小智 7
如果您想真正将资源嵌入到可执行文件中,可以使用https://crates.io/crates/actix-web-static-files。
它用于build.rs准备资源,稍后您可以只拥有单个可执行文件而无需依赖项。
npm作为额外的,它基于开箱即用的支持。
基本上我是这个箱子的作者。有 2.x 和 3.x 版本的版本actix-web。
由于描述不详细,我不确定您面临什么问题,但是,我运行了默认示例并且正在工作。
use actix_files::NamedFile;
use actix_web::{HttpRequest, Result};
use std::path::PathBuf;
/// https://actix.rs/docs/static-files/
async fn index(_req: HttpRequest) -> Result<NamedFile> {
let path: PathBuf = "./files/index.html".parse().unwrap();
Ok(NamedFile::open(path)?)
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind("127.0.0.1:8088")?
.run()
.await
}
Run Code Online (Sandbox Code Playgroud)
项目结构
- files/index.html
- src/index.rs
- cargo.toml
Run Code Online (Sandbox Code Playgroud)
依赖
[dependencies]
actix-web = "2.0.0"
actix-files = "0.2.2"
actix-rt = "1.1.1"
Run Code Online (Sandbox Code Playgroud)
希望这对你有用