优化router初始化相关代码结构

This commit is contained in:
李运家 2024-09-23 17:30:54 +08:00
parent c358552582
commit 8cfcea719a
5 changed files with 61 additions and 60 deletions

1
Cargo.lock generated
View File

@ -1102,6 +1102,7 @@ dependencies = [
"tokio",
"tokio-cron-scheduler",
"toml",
"tower-http",
"tracing",
"tracing-appender",
"tracing-subscriber",

View File

@ -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" }

View File

@ -1,2 +1,28 @@
use axum::{routing::post, Router};
pub mod account_controller;
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,
))
}

View File

@ -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<Body>| {
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,
))
}

View File

@ -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<Body>| {
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,
))
}