< 返回版块

adm1n1str00t 发表于 2021-09-17 09:30

情景:根据一个数组,向axum中注册路由

数组结构: Vec<Vec>

let route_vec = [["get","/api/v1","test-v1"],["post","/api/v2","test-v2"]]

let empty_route = axum::Router::new()

// 此处为示例代码,应为程序根据数组route_vec自动实现链式调用
let filled_route = empty_route
    .route(&route_vec[0][1],get(||async {&route_vec[0][2]}))
    .route(&route_vec[1][1],post(||async {&route_vec[1][2]}))


大概就是想实现类似上面的功能,数组会不定,尝试过宏和迭代器,以失败告终。

// 这是一个简单版本的失败实现,route_vec实际从其他接口获取,二维数组数量不定。
let route_vec = [["/api/v1","test-v1"],["/api/v2","test-v2"]]
let empty_route = axum::Router::new();

route_vec.iter().for_each(|item| {
    let filled_route = empty_route.route(&item[0], get(|| async { &item[1] }));
});

axum::Server::bind(&addr)
        .serve(filled_route.into_make_service())
        .await
        .unwrap();

评论区

写评论
作者 adm1n1str00t 2021-09-17 10:56

就是这个样子,感谢,学习了。

--
👇
johnmave126: 这意思?

let route_vec = [["/api/v1", "test-v1"], ["/api/v2", "test-v2"]];
let empty_router = axum::Router::new();

let filled_route = route_vec.iter().fold(empty_router.boxed(), |router, item| {
    let response = item[1].to_owned();
    router
        .route(
            &item[0],
            get(move || {
                let resp = response.clone();
                async move { resp }
            }),
        )
        .boxed()
});
Bai-Jinlin 2021-09-17 10:52

学到了

--
👇
johnmave126: 这意思?

let route_vec = [["/api/v1", "test-v1"], ["/api/v2", "test-v2"]];
let empty_router = axum::Router::new();

let filled_route = route_vec.iter().fold(empty_router.boxed(), |router, item| {
    let response = item[1].to_owned();
    router
        .route(
            &item[0],
            get(move || {
                let resp = response.clone();
                async move { resp }
            }),
        )
        .boxed()
});
johnmave126 2021-09-17 10:46

这意思?

let route_vec = [["/api/v1", "test-v1"], ["/api/v2", "test-v2"]];
let empty_router = axum::Router::new();

let filled_route = route_vec.iter().fold(empty_router.boxed(), |router, item| {
    let response = item[1].to_owned();
    router
        .route(
            &item[0],
            get(move || {
                let resp = response.clone();
                async move { resp }
            }),
        )
        .boxed()
});
Bai-Jinlin 2021-09-17 10:31

我感觉不能,因为Router是模板参数嵌套的类型,最多用过程宏配合vec生成。

--
👇
adm1n1str00t: 前辈,请问假如数组是通过其他api获取的,我只能知道类型的话,可以根据二维数组数量自行去调用.route()

--
👇
Bai-Jinlin: 用数组能整,就是有点麻烦。

let route_vec = [
    ["get", "/api/v1", "test-v1"],
    ["post", "/api/v2", "test-v2"],
];

type State = RwLock<[[&'static str; 3]; 2]>;
let shared_state = Arc::new(RwLock::new(route_vec));
let app = axum::Router::new()
    .route(
        route_vec[0][1],
        get(|state: Extension<Arc<State>>| async move { state.read().await[0][2] }),
    )
    .route(
        route_vec[1][1],
        get(|state: Extension<Arc<State>>| async move { state.read().await[1][2] }),
    )
    .layer(AddExtensionLayer::new(shared_state));
作者 adm1n1str00t 2021-09-17 10:18

前辈,请问假如数组是通过其他api获取的,我只能知道类型的话,可以根据二维数组数量自行去调用.route()

--
👇
Bai-Jinlin: 用数组能整,就是有点麻烦。

let route_vec = [
    ["get", "/api/v1", "test-v1"],
    ["post", "/api/v2", "test-v2"],
];

type State = RwLock<[[&'static str; 3]; 2]>;
let shared_state = Arc::new(RwLock::new(route_vec));
let app = axum::Router::new()
    .route(
        route_vec[0][1],
        get(|state: Extension<Arc<State>>| async move { state.read().await[0][2] }),
    )
    .route(
        route_vec[1][1],
        get(|state: Extension<Arc<State>>| async move { state.read().await[1][2] }),
    )
    .layer(AddExtensionLayer::new(shared_state));
1 共 5 条评论, 1 页