Skip to main content

framework_web_axum/
lib.rs

1mod context;
2mod cors;
3mod dispatch;
4mod router;
5mod server;
6
7pub use context::{AxumContext, scope_axum, use_axum};
8pub use cors::AxumCors;
9pub use router::{WebRouteResultFuture, WebRouteWrapper, WebRouter};
10pub use server::{AxumBuilder, AxumServer, axum_builder};
11
12use crate::dispatch::{AxumState, dispatch};
13use axum::Router;
14use axum::routing::any;
15use framework_web::WebContext;
16use std::sync::Arc;
17
18/// CORS 解析函数:按请求上下文返回本次响应的 CORS 配置。
19pub type AxumCorsFn = Arc<dyn Fn(&WebContext) -> AxumCors + Send + Sync>;
20
21/// 构建 WebRouter:内部创建 WebRouter,并挂载统一的 fallback 分发。
22pub fn build_router(wrapper: Option<WebRouteWrapper>) -> WebRouter {
23    WebRouter::new(wrapper)
24}
25
26/// 使用已有 WebRouter 组装 axum Router。
27pub(crate) fn build_router_with(
28    router: Arc<WebRouter>,
29    context: AxumContext,
30    cors: AxumCorsFn,
31) -> Router {
32    Router::new().fallback(any(dispatch)).with_state(AxumState {
33        router,
34        context,
35        cors,
36    })
37}