Conflicting routes
#3039
-
Hi, I have two services, #[put("/{bucket_name}")]
pub async fn create_bucket( and #[put("/{bucket_name}/{object_name}")]
pub async fn put_object( However, the second doesn't work, as it is not found. What do you think I can do? |
Beta Was this translation helpful? Give feedback.
Answered by
oneslash
May 20, 2023
Replies: 1 comment
-
I have done a different way, created one handler which handles all #[put("/{tail}*")]
pub async fn handle_put(_: web::Path<String>, req: HttpRequest) -> Result<HttpResponse, Error> {
let mut path = Path::new(Url::new(req.uri().clone()));
let mut message = String::new();
let routes = [
("/{bucket}/{object_name}", "bucket and object"),
("/{bucket}", "bucket"),
];
for (pattern, description) in &routes {
if ResourceDef::new(*pattern).capture_match_info(&mut path) {
match (path.get("bucket"), path.get("object_name")) {
(Some(bucket), Some(object_name)) if *description == "bucket and object" => {
message = format!("bucket: {}, object_name: {}", bucket, object_name);
}
(Some(bucket), None) if *description == "bucket" => {
message = format!("bucket: {}", bucket);
}
_ => return Ok(HttpResponse::InternalServerError().finish()),
}
break;
}
}
if message.is_empty() {
Ok(HttpResponse::NotFound().finish())
} else {
Ok(HttpResponse::Ok().body(message))
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
oneslash
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have done a different way, created one handler which handles all
PUT /{}*
, and inside I match them myself.