英文:
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存储库中关于"*path"的引用,但找不到通配符的文档或示例。我想使用上面的模式,因为需要我的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 = "/: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());
...
...
...
}
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 = "/:namespace<.+(?=/)>/:id", method = "get")]
async fn get(&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 = "/: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
...
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论