mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-13 08:36:54 +00:00
fix
This commit is contained in:
@@ -16,40 +16,37 @@ struct ResetParams {
|
||||
reason: Option<String>,
|
||||
}
|
||||
|
||||
// 定义一个全局变量 统计接受到数据条数
|
||||
// Define a global variable and count the number of data received
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
static WEBHOOK_COUNT: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// 构建应用
|
||||
// Build an application
|
||||
let app = Router::new()
|
||||
.route("/webhook", post(receive_webhook))
|
||||
.route(
|
||||
"/webhook/reset/{reason}",
|
||||
get(reset_webhook_count_with_path),
|
||||
)
|
||||
.route("/webhook/reset/{reason}", get(reset_webhook_count_with_path))
|
||||
.route("/webhook/reset", get(reset_webhook_count))
|
||||
.route("/webhook", get(receive_webhook));
|
||||
// 启动服务器
|
||||
// Start the server
|
||||
let addr = "0.0.0.0:3020";
|
||||
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||||
println!("Server running on {}", addr);
|
||||
|
||||
// 服务启动后进行自检
|
||||
// Self-checking after the service is started
|
||||
tokio::spawn(async move {
|
||||
// 给服务器一点时间启动
|
||||
// Give the server some time to start
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
|
||||
match is_service_active(addr).await {
|
||||
Ok(true) => println!("服务健康检查:成功 - 服务正常运行"),
|
||||
Ok(false) => eprintln!("服务健康检查:失败 - 服务未响应"),
|
||||
Err(e) => eprintln!("服务健康检查错误:{}", e),
|
||||
Ok(true) => println!("Service health check: Successful - Service is running normally"),
|
||||
Ok(false) => eprintln!("Service Health Check: Failed - Service Not Responded"),
|
||||
Err(e) => eprintln!("Service health check errors:{}", e),
|
||||
}
|
||||
});
|
||||
|
||||
// 创建关闭信号处理
|
||||
// Create a shutdown signal processing
|
||||
tokio::select! {
|
||||
result = axum::serve(listener, app) => {
|
||||
if let Err(e) = result {
|
||||
@@ -62,16 +59,14 @@ async fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
/// 创建一个方法重置 WEBHOOK_COUNT 的值
|
||||
async fn reset_webhook_count_with_path(
|
||||
axum::extract::Path(reason): axum::extract::Path<String>,
|
||||
) -> Response<String> {
|
||||
// 输出当前计数器的值
|
||||
/// Create a method to reset the value of WEBHOOK_COUNT
|
||||
async fn reset_webhook_count_with_path(axum::extract::Path(reason): axum::extract::Path<String>) -> Response<String> {
|
||||
// Output the value of the current counter
|
||||
let current_count = WEBHOOK_COUNT.load(Ordering::SeqCst);
|
||||
println!("Current webhook count: {}", current_count);
|
||||
|
||||
println!("Reset webhook count, reason: {}", reason);
|
||||
// 将计数器重置为 0
|
||||
// Reset the counter to 0
|
||||
WEBHOOK_COUNT.store(0, Ordering::SeqCst);
|
||||
println!("Webhook count has been reset to 0.");
|
||||
|
||||
@@ -85,17 +80,14 @@ async fn reset_webhook_count_with_path(
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// 创建一个方法重置 WEBHOOK_COUNT 的值
|
||||
/// 可以通过调用此方法来重置计数器
|
||||
async fn reset_webhook_count(
|
||||
Query(params): Query<ResetParams>,
|
||||
headers: HeaderMap,
|
||||
) -> Response<String> {
|
||||
// 输出当前计数器的值
|
||||
/// Create a method to reset the value of WEBHOOK_COUNT
|
||||
/// You can reset the counter by calling this method
|
||||
async fn reset_webhook_count(Query(params): Query<ResetParams>, headers: HeaderMap) -> Response<String> {
|
||||
// Output the value of the current counter
|
||||
let current_count = WEBHOOK_COUNT.load(Ordering::SeqCst);
|
||||
println!("Current webhook count: {}", current_count);
|
||||
|
||||
let reason = params.reason.unwrap_or_else(|| "未提供原因".to_string());
|
||||
let reason = params.reason.unwrap_or_else(|| "Reason not provided".to_string());
|
||||
println!("Reset webhook count, reason: {}", reason);
|
||||
|
||||
for header in headers {
|
||||
@@ -104,51 +96,41 @@ async fn reset_webhook_count(
|
||||
}
|
||||
|
||||
println!("Reset webhook count printed headers");
|
||||
// 将计数器重置为 0
|
||||
// Reset the counter to 0
|
||||
WEBHOOK_COUNT.store(0, Ordering::SeqCst);
|
||||
println!("Webhook count has been reset to 0.");
|
||||
Response::builder()
|
||||
.header("Foo", "Bar")
|
||||
.status(StatusCode::OK)
|
||||
.body(format!(
|
||||
"Webhook count reset successfully current_count:{}",
|
||||
current_count
|
||||
))
|
||||
.body(format!("Webhook count reset successfully current_count:{}", current_count))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn is_service_active(addr: &str) -> Result<bool, String> {
|
||||
let socket_addr = tokio::net::lookup_host(addr)
|
||||
.await
|
||||
.map_err(|e| format!("无法解析主机:{}", e))?
|
||||
.map_err(|e| format!("Unable to resolve host:{}", e))?
|
||||
.next()
|
||||
.ok_or_else(|| "未找到地址".to_string())?;
|
||||
.ok_or_else(|| "Address not found".to_string())?;
|
||||
|
||||
println!("正在检查服务状态:{}", socket_addr);
|
||||
println!("Checking service status:{}", socket_addr);
|
||||
|
||||
match tokio::time::timeout(
|
||||
std::time::Duration::from_secs(5),
|
||||
tokio::net::TcpStream::connect(socket_addr),
|
||||
)
|
||||
.await
|
||||
{
|
||||
match tokio::time::timeout(std::time::Duration::from_secs(5), tokio::net::TcpStream::connect(socket_addr)).await {
|
||||
Ok(Ok(_)) => Ok(true),
|
||||
Ok(Err(e)) => {
|
||||
if e.kind() == std::io::ErrorKind::ConnectionRefused {
|
||||
Ok(false)
|
||||
} else {
|
||||
Err(format!("连接失败:{}", e))
|
||||
Err(format!("Connection failed:{}", e))
|
||||
}
|
||||
}
|
||||
Err(_) => Err("连接超时".to_string()),
|
||||
Err(_) => Err("Connection timeout".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn receive_webhook(Json(payload): Json<Value>) -> StatusCode {
|
||||
let start = SystemTime::now();
|
||||
let since_the_epoch = start
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("Time went backwards");
|
||||
let since_the_epoch = start.duration_since(UNIX_EPOCH).expect("Time went backwards");
|
||||
|
||||
// get the number of seconds since the unix era
|
||||
let seconds = since_the_epoch.as_secs();
|
||||
@@ -157,20 +139,14 @@ async fn receive_webhook(Json(payload): Json<Value>) -> StatusCode {
|
||||
let (year, month, day, hour, minute, second) = convert_seconds_to_date(seconds);
|
||||
|
||||
// output result
|
||||
println!(
|
||||
"current time:{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
|
||||
year, month, day, hour, minute, second
|
||||
);
|
||||
println!("current time:{:04}-{:02}-{:02} {:02}:{:02}:{:02}", year, month, day, hour, minute, second);
|
||||
println!(
|
||||
"received a webhook request time:{} content:\n {}",
|
||||
seconds,
|
||||
serde_json::to_string_pretty(&payload).unwrap()
|
||||
);
|
||||
WEBHOOK_COUNT.fetch_add(1, Ordering::SeqCst);
|
||||
println!(
|
||||
"Total webhook requests received: {}",
|
||||
WEBHOOK_COUNT.load(Ordering::SeqCst)
|
||||
);
|
||||
println!("Total webhook requests received: {}", WEBHOOK_COUNT.load(Ordering::SeqCst));
|
||||
StatusCode::OK
|
||||
}
|
||||
|
||||
@@ -221,12 +197,5 @@ fn convert_seconds_to_date(seconds: u64) -> (u32, u32, u32, u32, u32, u32) {
|
||||
// calculate the number of seconds
|
||||
second += total_seconds;
|
||||
|
||||
(
|
||||
year as u32,
|
||||
month as u32,
|
||||
day as u32,
|
||||
hour as u32,
|
||||
minute as u32,
|
||||
second as u32,
|
||||
)
|
||||
(year as u32, month as u32, day as u32, hour as u32, minute as u32, second as u32)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user