我正在与 Rust 的 actix-web 2.0 框架作斗争。我希望我的 rust 服务器能够为我的 index.html 文件提供服务,但大多数可用的帮助都是旧版本的,因此新版本中发生了很多变化。我尝试了以下代码,但它不适用于 actix-web 2.0。请在 actix-web 2.0 中提出一些可行的解决方案。
use actix_files::NamedFile;
use actix_web::{HttpRequest, Result};
async fn index(req: HttpRequest) -> Result<NamedFile> {
Ok(NamedFile::open(path_to_file)?)
}
通过尝试答案中给出的代码,我可以提供单个 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
}
以上是我的主要方法。在浏览器控制台中,我仍然收到无法加载 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
我已经通过数据库集成构建了后端,并且通过curl命令检查它工作正常,现在我正在尝试构建前端并作为尝试提供静态文件的第一步。
我不确定您面临什么问题,因为描述不详细,但是,我运行了默认示例并且它正在工作。
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
}
项目结构
- files/index.html
- src/index.rs
- cargo.toml
依赖关系
[dependencies]
actix-web = "2.0.0"
actix-files = "0.2.2"
actix-rt = "1.1.1"
如果您想真正将资源嵌入到可执行文件中,可以使用 https://crates.io/crates/actix-web-static-files。
它使用
build.rs
来准备资源,稍后您可以只拥有单个可执行文件而无需依赖项。
作为额外的,它支持基于
npm
的开箱即用构建。
基本上我是这个箱子的作者。
actix-web
有 2.x 和 3.x 版本。
请参阅下面的代码,它适用于整个子目录:
main.rs
use actix_files as fs;
use actix_web::{App, HttpServer};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().service(
fs::Files::new("/", "./public")
.show_files_listing()
.index_file("index.html")
.use_last_modified(true),
)
})
.bind("0.0.0.0:8000")?
.run()
.await
}
货物.toml
[package]
name = "actixminimal"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4"
actix-files = "0.6.2"
我尝试过它执行文件服务的速度非常快。
在这种情况下,您可以创建一个“public”,也可以使用自己的“static”文件夹(然后更改与文件夹名称相关的代码),或者可以从 Web 框架导出静态文件。我使用 GatsbyJS 并使用部署参数到“public”文件夹。 我在我的 github 上分享它。 https://github.com/openmymai/actixminimal.git