优化: 代理工具代码审查修复
This commit is contained in:
@@ -132,6 +132,21 @@ fn urlencoding(s: &str) -> String {
|
||||
'/' => result.push_str("%2F"),
|
||||
'?' => result.push_str("%3F"),
|
||||
'#' => result.push_str("%23"),
|
||||
'[' => result.push_str("%5B"),
|
||||
']' => result.push_str("%5D"),
|
||||
'!' => result.push_str("%21"),
|
||||
'$' => result.push_str("%24"),
|
||||
'&' => result.push_str("%26"),
|
||||
'\'' => result.push_str("%27"),
|
||||
'(' => result.push_str("%28"),
|
||||
')' => result.push_str("%29"),
|
||||
'*' => result.push_str("%2A"),
|
||||
'+' => result.push_str("%2B"),
|
||||
',' => result.push_str("%2C"),
|
||||
';' => result.push_str("%3B"),
|
||||
'=' => result.push_str("%3D"),
|
||||
'%' => result.push_str("%25"),
|
||||
' ' => result.push_str("%20"),
|
||||
_ => result.push(c),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,8 +182,11 @@ pub async fn find(
|
||||
let start = Instant::now();
|
||||
|
||||
let client = manager.get_client(&req.conn).await
|
||||
.map_err(|e| error_response_with_usage(&e.to_string(),
|
||||
&format!("Usage: POST /find {{\"conn\": \"name\", \"collection\": \"coll\", \"filter\": {{}}}}\nAvailable: {}", list_conn_names(manager))))?;
|
||||
.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/find", "http").with_conn(&req.conn).with_error(&e.to_string()));
|
||||
error_response_with_usage(&e.to_string(),
|
||||
&format!("Usage: POST /find {{\"conn\": \"name\", \"collection\": \"coll\", \"filter\": {{}}}}\nAvailable: {}", list_conn_names(manager)))
|
||||
})?;
|
||||
|
||||
let db = client.database(&get_database(manager, &req.conn));
|
||||
let coll = db.collection::<Document>(&req.collection);
|
||||
@@ -195,10 +198,16 @@ pub async fn find(
|
||||
if let Some(s) = req.skip { find = find.skip(s); }
|
||||
|
||||
let mut cursor = find.await
|
||||
.map_err(|e| error_response(&e.to_string()))?;
|
||||
.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/find", "http").with_conn(&req.conn).with_command(&format!("{}.{}", req.conn, req.collection)).with_error(&e.to_string()));
|
||||
error_response(&e.to_string())
|
||||
})?;
|
||||
|
||||
let mut documents = Vec::new();
|
||||
while let Some(result) = cursor.try_next().await.map_err(|e: mongodb::error::Error| error_response(&e.to_string()))? {
|
||||
while let Some(result) = cursor.try_next().await.map_err(|e: mongodb::error::Error| {
|
||||
logger.log(&LogEntry::new("/find", "http").with_conn(&req.conn).with_error(&e.to_string()));
|
||||
error_response(&e.to_string())
|
||||
})? {
|
||||
documents.push(bson_to_json(&result));
|
||||
}
|
||||
|
||||
@@ -222,14 +231,20 @@ pub async fn insert(
|
||||
let start = Instant::now();
|
||||
|
||||
let client = manager.get_client(&req.conn).await
|
||||
.map_err(|e| error_response_with_usage(&e.to_string(),
|
||||
&format!("Usage: POST /insert {{\"conn\": \"name\", \"collection\": \"coll\", \"documents\": [{{}}]}}")))?;
|
||||
.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/insert", "http").with_conn(&req.conn).with_error(&e.to_string()));
|
||||
error_response_with_usage(&e.to_string(),
|
||||
&format!("Usage: POST /insert {{\"conn\": \"name\", \"collection\": \"coll\", \"documents\": [{{}}]}}"))
|
||||
})?;
|
||||
|
||||
let db = client.database(&get_database(manager, &req.conn));
|
||||
let coll = db.collection::<Document>(&req.collection);
|
||||
|
||||
let result = coll.insert_many(req.documents).await
|
||||
.map_err(|e| error_response(&e.to_string()))?;
|
||||
.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/insert", "http").with_conn(&req.conn).with_command(&format!("{}.{}", req.conn, req.collection)).with_error(&e.to_string()));
|
||||
error_response(&e.to_string())
|
||||
})?;
|
||||
|
||||
let inserted_ids: Vec<String> = result.inserted_ids.iter()
|
||||
.map(|(k, v)| format!("{}:{}", k, v))
|
||||
@@ -254,8 +269,11 @@ pub async fn update(
|
||||
let start = Instant::now();
|
||||
|
||||
let client = manager.get_client(&req.conn).await
|
||||
.map_err(|e| error_response_with_usage(&e.to_string(),
|
||||
"Usage: POST /update {\"conn\": \"name\", \"collection\": \"coll\", \"filter\": {}, \"update\": {\"$set\": {}}}"))?;
|
||||
.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/update", "http").with_conn(&req.conn).with_error(&e.to_string()));
|
||||
error_response_with_usage(&e.to_string(),
|
||||
"Usage: POST /update {\"conn\": \"name\", \"collection\": \"coll\", \"filter\": {}, \"update\": {\"$set\": {}}}")
|
||||
})?;
|
||||
|
||||
let db = client.database(&get_database(manager, &req.conn));
|
||||
let coll = db.collection::<Document>(&req.collection);
|
||||
@@ -268,7 +286,10 @@ pub async fn update(
|
||||
let mut cmd = coll.update_one(req.filter, req.update);
|
||||
if req.upsert.unwrap_or(false) { cmd = cmd.upsert(true); }
|
||||
cmd.await
|
||||
}.map_err(|e| error_response(&e.to_string()))?;
|
||||
}.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/update", "http").with_conn(&req.conn).with_command(&format!("{}.{}", req.conn, req.collection)).with_error(&e.to_string()));
|
||||
error_response(&e.to_string())
|
||||
})?;
|
||||
|
||||
let upserted_id = result.upserted_id.map(|id| format!("{:?}", id));
|
||||
let duration = start.elapsed();
|
||||
@@ -294,8 +315,11 @@ pub async fn delete(
|
||||
let start = Instant::now();
|
||||
|
||||
let client = manager.get_client(&req.conn).await
|
||||
.map_err(|e| error_response_with_usage(&e.to_string(),
|
||||
"Usage: POST /delete {\"conn\": \"name\", \"collection\": \"coll\", \"filter\": {}}"))?;
|
||||
.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/delete", "http").with_conn(&req.conn).with_error(&e.to_string()));
|
||||
error_response_with_usage(&e.to_string(),
|
||||
"Usage: POST /delete {\"conn\": \"name\", \"collection\": \"coll\", \"filter\": {}}")
|
||||
})?;
|
||||
|
||||
let db = client.database(&get_database(manager, &req.conn));
|
||||
let coll = db.collection::<Document>(&req.collection);
|
||||
@@ -304,7 +328,10 @@ pub async fn delete(
|
||||
coll.delete_many(req.filter).await
|
||||
} else {
|
||||
coll.delete_one(req.filter).await
|
||||
}.map_err(|e| error_response(&e.to_string()))?;
|
||||
}.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/delete", "http").with_conn(&req.conn).with_command(&format!("{}.{}", req.conn, req.collection)).with_error(&e.to_string()));
|
||||
error_response(&e.to_string())
|
||||
})?;
|
||||
|
||||
let deleted_count = result.deleted_count;
|
||||
let duration = start.elapsed();
|
||||
@@ -325,17 +352,26 @@ pub async fn aggregate(
|
||||
let start = Instant::now();
|
||||
|
||||
let client = manager.get_client(&req.conn).await
|
||||
.map_err(|e| error_response_with_usage(&e.to_string(),
|
||||
"Usage: POST /aggregate {\"conn\": \"name\", \"collection\": \"coll\", \"pipeline\": [{\"$match\": {}}, ...]}"))?;
|
||||
.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/aggregate", "http").with_conn(&req.conn).with_error(&e.to_string()));
|
||||
error_response_with_usage(&e.to_string(),
|
||||
"Usage: POST /aggregate {\"conn\": \"name\", \"collection\": \"coll\", \"pipeline\": [{\"$match\": {}}, ...]}")
|
||||
})?;
|
||||
|
||||
let db = client.database(&get_database(manager, &req.conn));
|
||||
let coll = db.collection::<Document>(&req.collection);
|
||||
|
||||
let mut cursor = coll.aggregate(req.pipeline).await
|
||||
.map_err(|e| error_response(&e.to_string()))?;
|
||||
.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/aggregate", "http").with_conn(&req.conn).with_command(&format!("{}.{}", req.conn, req.collection)).with_error(&e.to_string()));
|
||||
error_response(&e.to_string())
|
||||
})?;
|
||||
|
||||
let mut documents = Vec::new();
|
||||
while let Some(result) = cursor.try_next().await.map_err(|e: mongodb::error::Error| error_response(&e.to_string()))? {
|
||||
while let Some(result) = cursor.try_next().await.map_err(|e: mongodb::error::Error| {
|
||||
logger.log(&LogEntry::new("/aggregate", "http").with_conn(&req.conn).with_error(&e.to_string()));
|
||||
error_response(&e.to_string())
|
||||
})? {
|
||||
documents.push(bson_to_json(&result));
|
||||
}
|
||||
|
||||
@@ -359,14 +395,20 @@ pub async fn count(
|
||||
let start = Instant::now();
|
||||
|
||||
let client = manager.get_client(&req.conn).await
|
||||
.map_err(|e| error_response_with_usage(&e.to_string(),
|
||||
"Usage: POST /count {\"conn\": \"name\", \"collection\": \"coll\", \"filter\": {}}"))?;
|
||||
.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/count", "http").with_conn(&req.conn).with_error(&e.to_string()));
|
||||
error_response_with_usage(&e.to_string(),
|
||||
"Usage: POST /count {\"conn\": \"name\", \"collection\": \"coll\", \"filter\": {}}")
|
||||
})?;
|
||||
|
||||
let db = client.database(&get_database(manager, &req.conn));
|
||||
let coll = db.collection::<Document>(&req.collection);
|
||||
|
||||
let count = coll.count_documents(req.filter.unwrap_or_default()).await
|
||||
.map_err(|e| error_response(&e.to_string()))?;
|
||||
.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/count", "http").with_conn(&req.conn).with_command(&format!("{}.{}", req.conn, req.collection)).with_error(&e.to_string()));
|
||||
error_response(&e.to_string())
|
||||
})?;
|
||||
|
||||
let duration = start.elapsed();
|
||||
|
||||
@@ -415,7 +457,10 @@ pub async fn add_connection(
|
||||
};
|
||||
|
||||
manager.add_connection(cfg).await
|
||||
.map_err(|e| error_response(&e.to_string()))?;
|
||||
.map_err(|e| {
|
||||
logger.log(&LogEntry::new("/connections/add", "http").with_conn(&req.name).with_error(&e.to_string()));
|
||||
error_response(&e.to_string())
|
||||
})?;
|
||||
|
||||
logger.log(&LogEntry::new("/connections/add", "http")
|
||||
.with_conn(&req.name)
|
||||
|
||||
Reference in New Issue
Block a user