This is an automated email from the ASF dual-hosted git repository. xuehuilang pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/shenyu-client-rust.git
The following commit(s) were added to refs/heads/main by this push: new 1cfba8f Update README_CN.md 1cfba8f is described below commit 1cfba8fbfd3477f420e291bdbf6148dfd7084131 Author: DamonXue <damonx...@gmail.com> AuthorDate: Tue Oct 1 20:49:05 2024 +0800 Update README_CN.md --- README_CN.md | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/README_CN.md b/README_CN.md index b0b5e1b..58c6d6e 100644 --- a/README_CN.md +++ b/README_CN.md @@ -30,7 +30,9 @@ use axum::{routing::get, Router}; use shenyu_client_rust::axum_impl::ShenYuRouter; use shenyu_client_rust::config::ShenYuConfig; use shenyu_client_rust::{core::ShenyuClient, IRouter}; -use tokio::signal; + +mod ci; +use crate::ci::_CI_CTRL_C; async fn health_handler() -> &'static str { "OK" @@ -42,37 +44,44 @@ async fn create_user_handler() -> &'static str { #[tokio::main] async fn main() { + // Spawn a thread to listen for Ctrl-C events and shutdown the server + std::thread::spawn(_CI_CTRL_C); + // Initialize tracing + tracing_subscriber::fmt::init(); + let app = ShenYuRouter::<()>::new("shenyu_client_app") .nest("/api", ShenYuRouter::new("api")) - .route("/health", get(health_handler)) - .route("/users", post(create_user_handler)); + .route("/health", "get", get(health_handler)) + .route("/users", "post", post(create_user_handler)); let config = ShenYuConfig::from_yaml_file("examples/config.yml").unwrap(); - let client = ShenyuClient::from(config, app.app_name(), app.uri_infos(), 3000) - .await - .unwrap(); + let client = ShenyuClient::from(config, app.app_name(), app.uri_infos(), 3000).unwrap(); let axum_app: Router = app.into(); - client.register().await.expect("TODO: panic message"); + client.register().expect("TODO: panic message"); // Start Axum server let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, axum_app) .with_graceful_shutdown(async move { - signal::ctrl_c().await.expect("failed to listen for event"); - client.offline_register().await; + tokio::signal::ctrl_c() + .await + .expect("failed to listen for event"); + client.offline_register(); }) .await .unwrap(); } ``` - ```rust #![cfg(feature = "actix-web")] use actix_web::{middleware, App, HttpServer, Responder}; use shenyu_client_rust::actix_web_impl::ShenYuRouter; use shenyu_client_rust::config::ShenYuConfig; -use shenyu_client_rust::{core::ShenyuClient, register_once, shenyu_router, IRouter}; +use shenyu_client_rust::{register_once, shenyu_router}; + +mod ci; +use crate::ci::_CI_CTRL_C; async fn health_handler() -> impl Responder { "OK" @@ -88,6 +97,11 @@ async fn index() -> impl Responder { #[actix_web::main] async fn main() -> std::io::Result<()> { + // Spawn a thread to listen for Ctrl-C events and shutdown the server + std::thread::spawn(_CI_CTRL_C); + // Initialize tracing + tracing_subscriber::fmt::init(); + HttpServer::new(move || { let mut router = ShenYuRouter::new("shenyu_client_app"); let mut app = App::new().wrap(middleware::Logger::default()); @@ -109,10 +123,12 @@ async fn main() -> std::io::Result<()> { .await } + ``` + 此示例演示了如何使用`ShenYuRouter`设置基本的Axum服务并将其注册到ShenYu网关。`health_handler`和`create_user_handler`是处理HTTP请求的简单异步函数。 ## 许可证 -此项目根据Apache许可证2.0版获得许可。有关更多详细信息,请参阅[LICENSE](LICENSE)文件。 \ No newline at end of file +此项目根据Apache许可证2.0版获得许可。有关更多详细信息,请参阅[LICENSE](LICENSE)文件。