You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Rust Axum框架实现动态路由:区分文件与目录路由

在Axum中区分结尾带/的目录路由和不带/的文件路由

Axum本身的路由通配符确实不直接支持按结尾/区分路径,但可以利用路由匹配的优先级顺序来实现你的需求:先定义匹配结尾带/的目录路由,再定义匹配不带/的文件路由,Axum会优先命中先定义的规则。

实现步骤与代码示例

  1. 分别定义目录和文件的PUT处理函数:
use axum::{
    extract::{Path, Query},
    http::StatusCode,
    routing::put,
    Router,
};
use std::collections::HashMap;

// 处理目录的PUT请求(路径结尾带/)
async fn handle_put_directory(
    Path(dir_path): Path<String>,
    query_params: Query<HashMap<String, String>>,
) -> impl IntoResponse {
    // 编写目录操作逻辑:比如创建目录、修改目录属性等
    (
        StatusCode::OK,
        format!("处理目录请求: {}, 查询参数: {:?}", dir_path, query_params),
    )
}

// 处理文件的PUT请求(路径结尾不带/)
async fn handle_put_file(
    Path(file_path): Path<String>,
    query_params: Query<HashMap<String, String>>,
) -> impl IntoResponse {
    // 编写文件操作逻辑:比如写入文件内容、更新文件等
    (
        StatusCode::OK,
        format!("处理文件请求: {}, 查询参数: {:?}", file_path, query_params),
    )
}

// 你的GET处理函数
async fn handle_get(Path(path): Path<String>) -> impl IntoResponse {
    format!("GET 请求路径: {}", path)
}
  1. 按优先级顺序构建路由:
#[tokio::main]
async fn main() {
    let app = Router::new()
        // 1. 优先匹配结尾带/的路径,对应目录操作
        .route("/*path/", put(handle_put_directory))
        // 2. 再匹配不带/的路径,对应文件操作
        .route("/*path", put(handle_put_file))
        // 3. 保留你的GET路由
        .route("/*path", get(handle_get));

    let addr = ([127, 0, 0, 1], 3000).into();
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

关键说明

  • 匹配优先级:Axum会按照路由定义的顺序依次匹配,所以结尾带/的目录路由必须放在前面,否则会被不带/的通配符路由抢先匹配。
  • 路径参数提取:对于/*path/规则,提取到的dir_path会自动去掉结尾的/(比如请求/dir/sub/sub3/,得到的路径是dir/sub/sub3),刚好符合目录路径的使用需求;而/*path规则提取的file_path就是完整的文件路径(比如/dir/sub/file.txt得到dir/sub/file.txt)。
  • 查询参数兼容:两个处理函数都能通过Query提取任意查询参数,不管是some_query还是some_other_query都能正常获取。

测试验证

  • 请求PUT /dir/sub/sub3/?some_query=value → 触发handle_put_directory
  • 请求PUT /dir/sub/file.txt?some_other_query=value → 触发handle_put_file
  • 请求PUT /dir/sub/something → 触发handle_put_file(符合你定义的规则)

内容的提问来源于stack exchange,提问作者Xershy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 14:21:32