#3207·actix-web

设置了 path_filter 和 show_files_listing,但列表页仍然显示所有文件。

作者: ahaoboy创建于 2023年12月2日更新于 2026年9月15日
标签C-improvementgood-first-issueA-files
rust
use actix_files::Files;
use actix_web::{get, guard, middleware, App, HttpServer, Responder};
const EXAMPLES_DIR: &str = concat![env!("CARGO_MANIFEST_DIR"), "/src"];

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));

    log::info!("starting HTTP server at http://localhost:8080");

    HttpServer::new(|| {
        App::new()
            .service(
                Files::new("/", EXAMPLES_DIR)
                    .path_filter(|n, _| {
                        println!("{:?}", n);
                        let s = n.to_str().unwrap();
                        if s == "lib.rs" || s == "" {
                            true
                        } else {
                            false
                        }
                    })
                    .show_files_listing(),
            )
            .wrap(middleware::Logger::default())
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

Expected Behavior

Only display the files that pass the filter, as in the example above, only the lib.rs file should be displayed.

内容来源: actix/actix-web