From c8ab89292ef2c9cf0deff1408c06b8699af8ec35 Mon Sep 17 00:00:00 2001 From: houseme Date: Tue, 22 Apr 2025 21:40:20 +0800 Subject: [PATCH] feat: improve webhook server and run script integration - Enhance webhook example with proper shutdown handling using tokio::select! - Update run.sh to automatically start webhook server alongside main service - Add event notification configuration to run.sh using environment variables - Set proper port bindings to ensure webhook server starts on port 3000 - Improve console output for better debugging experience - Fix race condition during service startup and shutdown This change ensures proper integration between the webhook server and the main rustfs service, providing a seamless development experience with automatic service discovery and clean termination. --- crates/event-notifier/examples/webhook.rs | 14 ++++++++++- deploy/config/event.example.toml | 29 +++++++++++++++++++++++ scripts/run.sh | 7 +++++- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 deploy/config/event.example.toml diff --git a/crates/event-notifier/examples/webhook.rs b/crates/event-notifier/examples/webhook.rs index b78af94d..5363e574 100644 --- a/crates/event-notifier/examples/webhook.rs +++ b/crates/event-notifier/examples/webhook.rs @@ -8,7 +8,19 @@ async fn main() { let app = Router::new().route("/webhook", post(receive_webhook)); // 启动服务器 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); - axum::serve(listener, app).await.unwrap(); + println!("Server running on http://0.0.0.0:3000"); + + // 创建关闭信号处理 + tokio::select! { + result = axum::serve(listener, app) => { + if let Err(e) = result { + eprintln!("Server error: {}", e); + } + } + _ = tokio::signal::ctrl_c() => { + println!("Shutting down server..."); + } + } } async fn receive_webhook(Json(payload): Json) -> StatusCode { diff --git a/deploy/config/event.example.toml b/deploy/config/event.example.toml new file mode 100644 index 00000000..809fe581 --- /dev/null +++ b/deploy/config/event.example.toml @@ -0,0 +1,29 @@ +# config.toml +store_path = "./deploy/logs/event_store" +channel_capacity = 5000 + +[[adapters]] +type = "Webhook" +endpoint = "http://127.0.0.1:3000/webhook" +auth_token = "your-auth-token" +max_retries = 3 +timeout = 50 + +[adapters.custom_headers] +custom_server = "value_server" +custom_client = "value_client" + +#[[adapters]] +#type = "Kafka" +#brokers = "localhost:9092" +#topic = "notifications" +#max_retries = 3 +#timeout = 60 +# +#[[adapters]] +#type = "Mqtt" +#broker = "mqtt.example.com" +#port = 1883 +#client_id = "event-notifier" +#topic = "events" +#max_retries = 3 \ No newline at end of file diff --git a/scripts/run.sh b/scripts/run.sh index 497c2a68..2cbd115a 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -57,9 +57,14 @@ export RUSTFS__SINKS__KAFKA__BOOTSTRAP_SERVERS="" export RUSTFS__SINKS__KAFKA__TOPIC="" export RUSTFS__LOGGER__QUEUE_CAPACITY=10 +# 事件消息配置 +export RUSTFS_EVENT_CONFIG="./deploy/config/event.example.toml" + if [ -n "$1" ]; then export RUSTFS_VOLUMES="$1" fi - +# 启动 webhook 服务器 +cargo run --example webhook -p rustfs-event-notifier & +# 启动主服务 cargo run --bin rustfs \ No newline at end of file