From 8cfcea719a45b8d36b979d2e7cccd2ae0c711efa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=BF=90=E5=AE=B6?= Date: Mon, 23 Sep 2024 17:30:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96router=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 1 + library/Cargo.toml | 1 + system/src/controller/mod.rs | 28 +++++++++++++++++- system/src/lib.rs | 34 +++++++++++++++++++-- system/src/router.rs | 57 ------------------------------------ 5 files changed, 61 insertions(+), 60 deletions(-) delete mode 100644 system/src/router.rs diff --git a/Cargo.lock b/Cargo.lock index 31dba14..7f6f617 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1102,6 +1102,7 @@ dependencies = [ "tokio", "tokio-cron-scheduler", "toml", + "tower-http", "tracing", "tracing-appender", "tracing-subscriber", diff --git a/library/Cargo.toml b/library/Cargo.toml index c550e19..a403d7c 100644 --- a/library/Cargo.toml +++ b/library/Cargo.toml @@ -32,6 +32,7 @@ hmac = { workspace = true } sha2 = { workspace = true } hex-literal = { workspace = true } tokio-cron-scheduler = { workspace = true } +tower-http = { workspace = true, features = ["trace"] } domain = { path = "../domain" } i18n = { path = "../i18n" } \ No newline at end of file diff --git a/system/src/controller/mod.rs b/system/src/controller/mod.rs index 8ef3856..0e87be0 100644 --- a/system/src/controller/mod.rs +++ b/system/src/controller/mod.rs @@ -1,2 +1,28 @@ +use axum::{routing::post, Router}; + pub mod account_controller; -pub mod feedback_controller; \ No newline at end of file +pub mod feedback_controller; + +pub fn init() -> Router { + Router::new() + .route( + "/account/google", + post(account_controller::authenticate_google), + ) + .route( + "/account/sys", + post(account_controller::authenticate_with_password), + ) + .route( + "/account/refresh-token", + post(account_controller::refresh_token) + ) + .route( + "/feedback", + post(feedback_controller::add_feedback) + .get(feedback_controller::get_feedback_list_by_page), + ) + .layer(axum::middleware::from_fn( + library::middleware::req_ctx::authenticate_ctx, + )) +} \ No newline at end of file diff --git a/system/src/lib.rs b/system/src/lib.rs index 400cbaa..c753417 100644 --- a/system/src/lib.rs +++ b/system/src/lib.rs @@ -1,8 +1,9 @@ +use axum::{body::Body, extract::Request, routing::get, Router}; use library::{config, task}; use tasks::get_tasks; +use tower_http::trace::TraceLayer; mod controller; -mod router; mod service; mod tasks; @@ -15,5 +16,34 @@ pub async fn serve() { tracing::info!("服务监听地址: {}", addr); task::start(get_tasks()).await; - axum::serve(listener, router::init()).await.unwrap(); + axum::serve(listener, init()).await.unwrap(); } + +// 初始化router,包括router中间件和数据 +fn init() -> Router { + let trace_layer = TraceLayer::new_for_http().make_span_with(|request: &Request| { + let req_id = match request + .headers() + .get("x-request-id") + .and_then(|value| value.to_str().ok()) + { + Some(v) => v.to_string(), + None => String::from("unknown"), + }; + tracing::error_span!("request_id", id = req_id) + }); + + let auth: Router = controller::init(); + + Router::new() + .route("/", get(|| async { "hello" })) + .nest("/gm/v1", auth) + .layer(axum::middleware::from_fn( + library::middleware::req_log::handle, + )) + .layer(axum::middleware::from_fn(library::middleware::cors::handle)) + .layer(trace_layer) + .layer(axum::middleware::from_fn( + library::middleware::req_id::handle, + )) +} \ No newline at end of file diff --git a/system/src/router.rs b/system/src/router.rs deleted file mode 100644 index 3748c3d..0000000 --- a/system/src/router.rs +++ /dev/null @@ -1,57 +0,0 @@ -use crate::controller; -use axum::body::Body; -use axum::http::Request; -use axum::routing::{get, post}; -use axum::Router; -use tower_http::trace::TraceLayer; - -pub(crate) fn init() -> Router { - let open = Router::new().route("/", get(|| async { "hello" })); - - let auth: Router = Router::new() - .route( - "/account/google", - post(controller::account_controller::authenticate_google), - ) - .route( - "/account/sys", - post(controller::account_controller::authenticate_with_password), - ) - .route( - "/account/refresh-token", - post(controller::account_controller::refresh_token) - ) - .route( - "/feedback", - post(controller::feedback_controller::add_feedback) - .get(controller::feedback_controller::get_feedback_list_by_page), - ) - .layer(axum::middleware::from_fn( - library::middleware::req_ctx::authenticate_ctx, - )); - - Router::new() - .nest("/", open) - .nest("/gm/v1", auth) - .layer(axum::middleware::from_fn( - library::middleware::req_log::handle, - )) - .layer(axum::middleware::from_fn(library::middleware::cors::handle)) - .layer( - TraceLayer::new_for_http().make_span_with(|request: &Request| { - let req_id = match request - .headers() - .get("x-request-id") - .and_then(|value| value.to_str().ok()) - { - Some(v) => v.to_string(), - None => String::from("unknown"), - }; - - tracing::error_span!("request_id", id = req_id) - }), - ) - .layer(axum::middleware::from_fn( - library::middleware::req_id::handle, - )) -}