Skip to main content

RouterBuilder

Struct RouterBuilder 

Source
pub struct RouterBuilder<S = ()> { /* private fields */ }
Expand description

路由构建器

对 axum::Router 的薄封装,提供链式 API 和未来扩展点(如 PHP 风格的 Route::group() / Route::resource())。

§用法

use sz_rust_router_facade::router::RouterBuilder;
use axum::Json;

let router = RouterBuilder::new()
    .get("/ping", || async { "pong" })
    .post("/echo", |Json(v): Json<serde_json::Value>| async move { Json(v) })
    .build();

路由构建器(泛型状态支持)

S 为应用共享状态类型,由首次注册的路由 handler 推断。 当 handler 使用 axum::extract::State<S> 时,S 自动确定为该状态类型。

§用法

// 无状态路由
let router = RouterBuilder::new()
    .get("/ping", ping_handler)
    .build();

// 有状态路由(S 由 handler 推断为 AppState)
let router = RouterBuilder::new()
    .get("/items", list_items)  // list_items 使用 State<AppState>
    .with_state(AppState::default())
    .build();

Implementations§

Source§

impl<S> RouterBuilder<S>
where S: Clone + Send + Sync + 'static,

Source

pub fn new() -> RouterBuilder<S>

创建空的 RouterBuilder

Source

pub fn with_router(inner: Router<S>) -> RouterBuilder<S>

从已有 Router 起步

Source

pub fn with_state<S2>(self, state: S) -> RouterBuilder<S2>

注入应用状态并转换状态类型

当注册的 handler 使用了 axum::extract::State<S> 时调用, 将 RouterBuilder<S> 转换为 RouterBuilder<S2>(通常 S2 = S)。

§用法
let builder = RouterBuilder::new()
    .get("/items", list_items)
    .with_state(AppState { db: pool });
Source

pub fn get<H, T>(self, path: &str, handler: H) -> RouterBuilder<S>
where H: Handler<T, S>, T: 'static,

注册 GET 路由

Source

pub fn post<H, T>(self, path: &str, handler: H) -> RouterBuilder<S>
where H: Handler<T, S>, T: 'static,

注册 POST 路由

Source

pub fn put<H, T>(self, path: &str, handler: H) -> RouterBuilder<S>
where H: Handler<T, S>, T: 'static,

注册 PUT 路由

Source

pub fn delete<H, T>(self, path: &str, handler: H) -> RouterBuilder<S>
where H: Handler<T, S>, T: 'static,

注册 DELETE 路由

Source

pub fn ws<H>(self, path: &str, handler: H) -> RouterBuilder<S>
where H: WsHandler,

注册 WebSocket 路由(原生框架集成,无需独立端口)

在主 HTTP 端口上处理 WebSocket 升级请求(如 GET /ws/chat)。 对齐 PHP Workerman websocket:// 协议,但复用 HTTP 端口。

§用法
use sz_rust_router_facade::router::RouterBuilder;
use sz_rust_router_facade::websocket_route::EchoWsHandler;

let router = RouterBuilder::new()
    .ws("/ws/echo", EchoWsHandler::new())
    .build();
Source

pub fn layer<L>(self, layer: L) -> RouterBuilder<S>
where L: Layer<Route> + Clone + Send + Sync + 'static, <L as Layer<Route>>::Service: Service<Request<Body>> + Clone + Send + Sync + 'static, <<L as Layer<Route>>::Service as Service<Request<Body>>>::Response: IntoResponse + 'static, <<L as Layer<Route>>::Service as Service<Request<Body>>>::Error: Into<Infallible> + 'static, <<L as Layer<Route>>::Service as Service<Request<Body>>>::Future: Send + 'static,

应用一个 tower::Layer

约束与 axum::Router::layer 一致,便于直接链式调用任何 tower-http 中间件。

Source

pub fn merge(self, other: Router<S>) -> RouterBuilder<S>

合并另一个 Router

Source

pub fn build(self) -> Router<S>

构建最终的 axum::Router

Trait Implementations§

Source§

impl Default for RouterBuilder

Source§

fn default() -> RouterBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<S = ()> !RefUnwindSafe for RouterBuilder<S>

§

impl<S = ()> !UnwindSafe for RouterBuilder<S>

§

impl<S> Freeze for RouterBuilder<S>

§

impl<S> Send for RouterBuilder<S>

§

impl<S> Sync for RouterBuilder<S>

§

impl<S> Unpin for RouterBuilder<S>

§

impl<S> UnsafeUnpin for RouterBuilder<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more