pub struct RouterBuilder<S = ()> { /* private fields */ }Expand description
路由构建器
对 axum::Router 的薄封装,提供链式 API 和未来扩展点(如 PHP 风格的
Route::group() / Route::resource())。
§用法
ⓘ
use sz_rust_core::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>
impl<S> RouterBuilder<S>
Sourcepub fn with_router(inner: Router<S>) -> Self
pub fn with_router(inner: Router<S>) -> Self
从已有 Router 起步
Sourcepub fn with_state<S2>(self, state: S) -> RouterBuilder<S2>
pub fn with_state<S2>(self, state: S) -> RouterBuilder<S2>
Sourcepub fn get<H, T>(self, path: &str, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn get<H, T>(self, path: &str, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
注册 GET 路由
Sourcepub fn post<H, T>(self, path: &str, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn post<H, T>(self, path: &str, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
注册 POST 路由
Sourcepub fn put<H, T>(self, path: &str, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn put<H, T>(self, path: &str, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
注册 PUT 路由
Sourcepub fn delete<H, T>(self, path: &str, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn delete<H, T>(self, path: &str, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
注册 DELETE 路由
Sourcepub fn ws<H: WsHandler>(self, path: &str, handler: H) -> Self
pub fn ws<H: WsHandler>(self, path: &str, handler: H) -> Self
注册 WebSocket 路由(原生框架集成,无需独立端口)
在主 HTTP 端口上处理 WebSocket 升级请求(如 GET /ws/chat)。
对齐 PHP Workerman websocket:// 协议,但复用 HTTP 端口。
§用法
ⓘ
use sz_rust_core::router::RouterBuilder;
use sz_rust_core::websocket_route::EchoWsHandler;
let router = RouterBuilder::new()
.ws("/ws/echo", EchoWsHandler::new())
.build();Sourcepub fn layer<L>(self, layer: L) -> Selfwhere
L: Layer<Route> + Clone + Send + Sync + 'static,
L::Service: Service<Request> + Clone + Send + Sync + 'static,
<L::Service as Service<Request>>::Response: IntoResponse + 'static,
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
pub fn layer<L>(self, layer: L) -> Selfwhere
L: Layer<Route> + Clone + Send + Sync + 'static,
L::Service: Service<Request> + Clone + Send + Sync + 'static,
<L::Service as Service<Request>>::Response: IntoResponse + 'static,
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
应用一个 tower::Layer
约束与 axum::Router::layer 一致,便于直接链式调用任何 tower-http 中间件。
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian().Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.