rust poem route with variable path segments

huangapple go评论56阅读模式
英文:

rust poem route with variable path segments

问题

Sure, here is the translated code portion:

我在Rust中使用poem::openapi有一个HTTP端点。我想要一个处理可变段路径的单一处理程序。

#[OpenApi]
impl NvApi {
    #[oai(path = "/:namespace/:id", method = "get")]
    async fn get(
        &self,
        nv: Data<&SharedHandle>,
        namespace: Path<String>,
        id: Path<String>,
    ) -> Result<GetResponse, poem::Error> {
        log::debug!("get {}/{}", namespace.as_str(), id.as_str());
        // ...
        // ...
        // ...
    }
}

我希望上面的代码处理像`/mynamespace/mydomain/myid`和`/mynamespace/myid`这样的路径 - 将任意数量的段加载到namespace变量中,但始终将最后一个段加载到'id'变量中。

我看到了poem存储库中关于&quot;*path&quot;的引用,但找不到通配符的文档或示例。我想使用上面的模式,因为需要我的SharedHandle实现

Please note that the translation is provided as requested, and it only includes the translated code portion without any additional content.

英文:

I have an HTTP endpoint in rust using poem::openapi. I would like a single handler for a variable segment path.

#[OpenApi]
impl NvApi {
    #[oai(path = &quot;/:namespace/:id&quot;, method = &quot;get&quot;)]
    async fn get(
        &amp;self,
        nv: Data&lt;&amp;SharedHandle&gt;,
        namespace: Path&lt;String&gt;,
        id: Path&lt;String&gt;,
    ) -&gt; Result&lt;GetResponse, poem::Error&gt; {
        log::debug!(&quot;get {}/{}&quot;, namespace.as_str(), id.as_str());
...
...
...
}

I would like the code above to handle paths like /mynamespace/mydomain/myid as well as /mynamespace/myid - an arbitrary number of segments loaded into the namespace variable but always loading the trailing segment into the 'id' variable.

I've seen references to "*path" in the poem repo but can't find any docs or examples of wildcard. I'd like to use the pattern above as my SharedHandle impl is needed.

答案1

得分: 1

这不是直接可能的。

poem 中的路径解析器在遇到通配符标记后会中断,这意味着它不能用于在路径中间进行匹配。

最好的解决方法是使用正则表达式来匹配除最后一段路径段之外的所有路径段。在代码中,这可能如下所示:

    #[oai(path = "/:namespace<.+(?=/)>/:id", method = "get")]
    async fn get(&self, ...) ...

但是 poem 使用 regex crate,它不支持前瞻断言(需要忽略最后一个斜杠)。

相反,您将不得不使用没有前瞻断言的正则表达式,并手动删除尾随斜杠:

#[OpenApi]
impl Api {
    #[oai(path = "/:namespace<.+/>:id", method = "get")]
    async fn get(
        &self,
        ...
        namespace: Path<String>,
        id: Path<String>,
    ) -> ... {
        // GET /a/b/c
        println!("{}", namespace.to_string()); // a/b/
        println!("{}", id.to_string());        // c
        ...
    }
}
英文:

This is not directly possible.

The path parser in poem breaks after encountering a wildcard token which means it cannot be used to match in the middle of a path.

The best workaround would be to use a regex that matches all path segments except the final segment. In code, that would hypothetically appear as:

    #[oai(path = &quot;/:namespace&lt;.+(?=/)&gt;/:id&quot;, method = &quot;get&quot;)]
    async fn get(&amp;self, ...) ...

but poem uses the regex crate which does not support lookaheads (needed to ignore the last slash).

Instead, you will have to use a regex with no lookaheads and manually remove the trailing slash:

#[OpenApi]
impl Api {
    #[oai(path = &quot;/:namespace&lt;.+/&gt;:id&quot;, method = &quot;get&quot;)]
    async fn get(
        &amp;self,
        ...
        namespace: Path&lt;String&gt;,
        id: Path&lt;String&gt;,
    ) -&gt; ... {
        // GET /a/b/c
        println!(&quot;{}&quot;, namespace.to_string()); // a/b/
        println!(&quot;{}&quot;, id.to_string());        // c
        ...
    }
}

huangapple
  • 本文由 发表于 2023年5月21日 07:08:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定