Skip to main content

wae_https/router/
mod.rs

1//! HTTP 路由模块
2//!
3//! 提供路由构建和管理工具。
4
5use crate::Router;
6use std::marker::PhantomData;
7
8/// 路由构建器
9pub struct RouterBuilder<S = ()> {
10    /// 内部路由实例
11    router: Router<S>,
12    /// 状态类型标记
13    _marker: PhantomData<S>,
14}
15
16impl<S> RouterBuilder<S>
17where
18    S: Clone + Send + Sync + 'static,
19{
20    /// 创建新的路由构建器
21    pub fn new() -> Self
22    where
23        S: Default,
24    {
25        Self::from_router(Router::with_state(S::default()))
26    }
27
28    /// 从现有路由创建构建器
29    pub fn from_router(router: Router<S>) -> Self {
30        Self { router, _marker: PhantomData }
31    }
32
33    /// 嵌套路由
34    pub fn nest<T>(mut self, prefix: &str, router: T) -> Self
35    where
36        T: Into<Router<S>>,
37    {
38        self.router = self.router.nest_service(prefix, router);
39        self
40    }
41
42    /// 合并路由
43    pub fn merge(mut self, other: Router<S>) -> Self {
44        self.router = self.router.merge(other);
45        self
46    }
47
48    /// 添加 GET 方法路由
49    pub fn get<H, T>(mut self, path: &str, handler: H) -> Self
50    where
51        H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
52        T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
53    {
54        self.router.add_route(http::Method::GET, path, handler);
55        self
56    }
57
58    /// 添加 POST 方法路由
59    pub fn post<H, T>(mut self, path: &str, handler: H) -> Self
60    where
61        H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
62        T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
63    {
64        self.router.add_route(http::Method::POST, path, handler);
65        self
66    }
67
68    /// 添加 PUT 方法路由
69    pub fn put<H, T>(mut self, path: &str, handler: H) -> Self
70    where
71        H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
72        T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
73    {
74        self.router.add_route(http::Method::PUT, path, handler);
75        self
76    }
77
78    /// 添加 DELETE 方法路由
79    pub fn delete<H, T>(mut self, path: &str, handler: H) -> Self
80    where
81        H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
82        T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
83    {
84        self.router.add_route(http::Method::DELETE, path, handler);
85        self
86    }
87
88    /// 添加 PATCH 方法路由
89    pub fn patch<H, T>(mut self, path: &str, handler: H) -> Self
90    where
91        H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
92        T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
93    {
94        self.router.add_route(http::Method::PATCH, path, handler);
95        self
96    }
97
98    /// 添加 OPTIONS 方法路由
99    pub fn options<H, T>(mut self, path: &str, handler: H) -> Self
100    where
101        H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
102        T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
103    {
104        self.router.add_route(http::Method::OPTIONS, path, handler);
105        self
106    }
107
108    /// 添加 HEAD 方法路由
109    pub fn head<H, T>(mut self, path: &str, handler: H) -> Self
110    where
111        H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
112        T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
113    {
114        self.router.add_route(http::Method::HEAD, path, handler);
115        self
116    }
117
118    /// 添加 TRACE 方法路由
119    pub fn trace<H, T>(mut self, path: &str, handler: H) -> Self
120    where
121        H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
122        T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
123    {
124        self.router.add_route(http::Method::TRACE, path, handler);
125        self
126    }
127
128    /// 构建路由
129    pub fn build(self) -> Router<S> {
130        self.router
131    }
132
133    /// 获取内部 Router 实例
134    pub fn into_inner(self) -> Router<S> {
135        self.build()
136    }
137}
138
139impl<S> Default for RouterBuilder<S>
140where
141    S: Clone + Send + Sync + 'static + Default,
142{
143    fn default() -> Self {
144        Self::new()
145    }
146}
147
148impl<S> From<Router<S>> for RouterBuilder<S>
149where
150    S: Clone + Send + Sync + 'static,
151{
152    fn from(router: Router<S>) -> Self {
153        Self::from_router(router)
154    }
155}
156
157impl<S> From<RouterBuilder<S>> for Router<S>
158where
159    S: Clone + Send + Sync + 'static,
160{
161    fn from(builder: RouterBuilder<S>) -> Self {
162        builder.build()
163    }
164}
165
166/// 方法路由类型
167pub struct MethodRouter<S = ()> {
168    /// 状态类型标记
169    _marker: PhantomData<S>,
170}
171
172impl<S> Clone for MethodRouter<S> {
173    fn clone(&self) -> Self {
174        Self { _marker: PhantomData }
175    }
176}
177
178impl<S> Default for MethodRouter<S> {
179    fn default() -> Self {
180        Self { _marker: PhantomData }
181    }
182}
183
184/// GET 方法路由
185pub fn get<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
186where
187    H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
188    T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
189    S: Clone + Send + Sync + 'static,
190{
191    move |router, path| {
192        router.add_route(http::Method::GET, path, handler);
193    }
194}
195
196/// POST 方法路由
197pub fn post<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
198where
199    H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
200    T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
201    S: Clone + Send + Sync + 'static,
202{
203    move |router, path| {
204        router.add_route(http::Method::POST, path, handler);
205    }
206}
207
208/// PUT 方法路由
209pub fn put<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
210where
211    H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
212    T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
213    S: Clone + Send + Sync + 'static,
214{
215    move |router, path| {
216        router.add_route(http::Method::PUT, path, handler);
217    }
218}
219
220/// DELETE 方法路由
221pub fn delete<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
222where
223    H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
224    T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
225    S: Clone + Send + Sync + 'static,
226{
227    move |router, path| {
228        router.add_route(http::Method::DELETE, path, handler);
229    }
230}
231
232/// PATCH 方法路由
233pub fn patch<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
234where
235    H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
236    T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
237    S: Clone + Send + Sync + 'static,
238{
239    move |router, path| {
240        router.add_route(http::Method::PATCH, path, handler);
241    }
242}
243
244/// OPTIONS 方法路由
245pub fn options<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
246where
247    H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
248    T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
249    S: Clone + Send + Sync + 'static,
250{
251    move |router, path| {
252        router.add_route(http::Method::OPTIONS, path, handler);
253    }
254}
255
256/// HEAD 方法路由
257pub fn head<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
258where
259    H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
260    T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
261    S: Clone + Send + Sync + 'static,
262{
263    move |router, path| {
264        router.add_route(http::Method::HEAD, path, handler);
265    }
266}
267
268/// TRACE 方法路由
269pub fn trace<H, T, S>(handler: H) -> impl FnOnce(&mut Router<S>, &str)
270where
271    H: Fn(T) -> crate::Response<crate::Body> + Clone + Send + Sync + 'static,
272    T: crate::extract::FromRequestParts<S, Error = crate::extract::ExtractorError> + 'static,
273    S: Clone + Send + Sync + 'static,
274{
275    move |router, path| {
276        router.add_route(http::Method::TRACE, path, handler);
277    }
278}
279
280/// 健康检查路由
281pub fn health_check_router() -> Router {
282    Router::new()
283}