首页 > 解决方案 > 资源正则表达式导致恐慌

问题描述

  1. 这段代码试图用 rust 编写的替换已经在生产环境中工作的服务,用 java 编写。
  2. 这个服务将作为一个 redis 集群的 sidecar 代理,暴露一个 api rest。它需要保持与当前 api 的兼容性。

路线是:

"/api/keys/{path:*}" 

在 path 中,我们可以输入 redis 的键名,并且可以包含以下任何格式:

/api/keys/users/41728391
/api/keys/users/1000/followers
/api/keys/users/{1234}/data

这是我的尝试

 HttpServer::new(move || App::new()
        .data(redis_config)
        .service(
            web::resource("/set/{path:*}").route(web::put().to(set_key))
        ) ).bind(("127.0.0.1", 8080))?
        .run()
        .await

我也试过这样:

#[get("/set/{path:*}")]...

但在两种情况下,我会收到此错误:

  .service(web::resource("/set/{path:*}").route(web::put().to(path_regex)))
   |                                                                         ^^^^^^^^^^ the trait `Factory<_, _, _>` is not implemented for `path_regex`



thread 'thread 'actix-rt:worker:1actix-rt:worker:0' panicked at '' panicked at 'Wrong path pattern: "/set/{path:*}" regex parse error:
    ^/set/(?P<path>*)$
                   ^
error: repetition operator missing expressionWrong path pattern: "/set/{path:*}" regex parse error:
    ^/set/(?P<path>*)$
               ^

我已阅读https://actix.rs/actix-web/actix_web/web/fn.resource.html

我的代码是:https ://github.com/rogeriob2br/enge-sidecar-redis

标签: regexrustactix-web

解决方案


推荐阅读