Skip to content

Commit

Permalink
add the routing status code
Browse files Browse the repository at this point in the history
  • Loading branch information
yaojianpin committed Oct 8, 2023
1 parent bfd580d commit 2a316dd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn proxy(db: &Database) -> Router {
if !id.starts_with(":") {
path.insert("id".to_string(), id.to_string());
}
let res = match method {
let mut res = match method {
&Method::GET => get_data(path, db, wrap).await.into_response(),
&Method::POST => post_data(path, body, db, wrap).await.into_response(),
&Method::PUT => put_data(path, body, db, wrap).await.into_response(),
Expand All @@ -125,6 +125,10 @@ pub fn proxy(db: &Database) -> Router {
.into_response(),
};

if let Some(code) = routing_value.status {
*res.status_mut() = StatusCode::from_u16(code).unwrap()
}

return Ok(res);
}

Expand All @@ -136,13 +140,17 @@ pub fn proxy(db: &Database) -> Router {
if !id.starts_with(":") {
path.insert("id".to_string(), id.to_string());
}
let res = match method {
let mut res = match method {
&Method::GET => get_file(path, db).await.into_response(),
&Method::POST => get_file(path, db).await.into_response(),
_ => (StatusCode::METHOD_NOT_ALLOWED, "method not support")
.into_response(),
};

if let Some(code) = routing_value.status {
*res.status_mut() = StatusCode::from_u16(code).unwrap()
}

return Ok(res);
}

Expand Down
1 change: 1 addition & 0 deletions src/models/data_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct RoutingValue {
pub query: Option<HashMap<String, String>>,
pub wrapping: Option<HashMap<String, Value>>,
pub rules: Option<Vec<RoutingRule>>,
pub status: Option<u16>,
}

#[derive(Debug, Default)]
Expand Down
3 changes: 2 additions & 1 deletion static/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"to": "/api/data/:id"
},
"/a/b/data2": {
"to": "/api/data2"
"to": "/api/data2",
"status": 500
},
"/c/data3": {
"to": "/api/data3"
Expand Down

0 comments on commit 2a316dd

Please sign in to comment.