1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use axum::Router as AxumRouter;
/// A wrapper around Axum's Router to provide clean integration with Rullst features
#[non_exhaustive]
pub struct Router {
inner: AxumRouter,
}
impl Default for Router {
fn default() -> Self {
Self::new()
}
}
impl Router {
/// Creates a new empty `Router` with no routes configured.
pub fn new() -> Self {
Router {
inner: AxumRouter::new(),
}
}
/// Registers an HTTP route at the given `path` with the given method router.
/// `method_router` is typically the result of `get(handler)`, `post(handler)`, etc.
pub fn route<T>(self, path: &str, method_router: T) -> Self
where
T: Into<axum::routing::MethodRouter>,
{
Router {
inner: self.inner.route(path, method_router.into()),
}
}
/// Registers a WebSocket upgrade route at the given `path`.
/// Internally maps to `GET path` since WebSocket upgrades happen over HTTP GET.
pub fn ws<H, T>(self, path: &str, handler: H) -> Self
where
T: 'static,
H: axum::handler::Handler<T, ()>,
{
Router {
inner: self.inner.route(path, axum::routing::get(handler)),
}
}
/// Unwraps the inner [`axum::Router`]. Use this to hand the router off to `axum::serve`
/// or to interoperate with Tower middleware that requires a raw Axum router.
pub fn into_axum(self) -> AxumRouter {
self.inner
}
/// Nests another Rullst `Router` under a path prefix.
/// All routes defined in `router` will be reachable under `path/...`.
pub fn nest(self, path: &str, router: Router) -> Self {
Router {
inner: self.inner.nest(path, router.inner),
}
}
/// Nests a raw [`axum::Router`] under a path prefix.
/// Useful for integrating third-party Axum routers (e.g. from `utoipa`, `aide`) without wrapping.
pub fn nest_axum(self, path: &str, router: AxumRouter) -> Self {
Router {
inner: self.inner.nest(path, router),
}
}
/// Merges a raw [`axum::Router`] into this router at the root.
///
/// Equivalent to `Router::merge` on the inner `axum::Router`. This is the
/// recommended way to compose a fully-built Axum router (for example, one
/// produced by `utoipa_axum::OpenApiRouter::split_for_parts`) into a Rullst
/// `Router`, since axum 0.8+ no longer allows nesting at the root path.
pub fn merge_axum(self, router: AxumRouter) -> Self {
Router {
inner: self.inner.merge(router),
}
}
/// Applies a Tower middleware layer to all routes in this router.
/// The layer is cloned once per request and must be `Send + Sync + 'static`.
pub fn layer<L>(self, layer: L) -> Self
where
L: tower_layer::Layer<axum::routing::Route> + Clone + Send + Sync + 'static,
L::Service: tower_service::Service<
axum::extract::Request,
Response = axum::response::Response,
Error = std::convert::Infallible,
> + Clone
+ Send
+ Sync
+ 'static,
<L::Service as tower_service::Service<axum::extract::Request>>::Future: Send + 'static,
{
Router {
inner: self.inner.layer(layer),
}
}
}
pub use axum::routing::delete;
pub use axum::routing::get;
pub use axum::routing::patch;
pub use axum::routing::post;
pub use axum::routing::put;
/// Returns an Axum `MethodRouter` that upgrades `GET` requests to a WebSocket connection.
/// Used inside `routes!` as `ws("/chat" => handler)` or directly on a `Router::ws()` call.
pub fn ws<H, T>(handler: H) -> axum::routing::MethodRouter
where
T: 'static,
H: axum::handler::Handler<T, ()>,
{
axum::routing::get(handler)
}
#[macro_export]
/// Declarative macro for building a [`Router`] from a list of HTTP route definitions.
///
/// # Example
/// ```rust,no_run
/// use rullst::{routes, routing::{get, post}};
///
/// async fn home_handler() -> &'static str { "home" }
/// async fn create_user() -> &'static str { "created" }
///
/// let router = routes![
/// get("/" => home_handler),
/// post("/users" => create_user),
/// ];
/// ```
macro_rules! routes {
( $($method:ident ( $path:expr => $handler:expr )),* $(,)? ) => {
{
let mut router = $crate::Router::new();
$(
router = router.route($path, $crate::routing::$method($handler));
)*
router
}
};
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use tower::ServiceExt;
async fn mock_handler() -> &'static str {
"hello"
}
#[tokio::test]
async fn test_router_routes() {
let router = Router::new()
.route("/", get(mock_handler))
.route("/post", post(mock_handler));
let app = router.into_axum();
let response = app
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
#[tokio::test]
async fn test_routes_macro() {
let router = routes![
get("/" => mock_handler),
post("/submit" => mock_handler),
];
let app = router.into_axum();
let response = app
.oneshot(
Request::builder()
.uri("/submit")
.method("POST")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
#[tokio::test]
async fn test_router_nesting() {
let api_router = Router::new().route("/users", get(mock_handler));
let router = Router::new().nest("/api", api_router);
let app = router.into_axum();
let response = app
.oneshot(
Request::builder()
.uri("/api/users")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
#[tokio::test]
async fn test_nest_axum() {
let axum_router = AxumRouter::new().route("/raw", axum::routing::get(mock_handler));
let router = Router::new().nest_axum("/api", axum_router);
let app = router.into_axum();
let response = app
.oneshot(
Request::builder()
.uri("/api/raw")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
#[tokio::test]
async fn test_ws_routing() {
let router = Router::new().ws("/chat", mock_handler);
let app = router.into_axum();
let response = app
.oneshot(Request::builder().uri("/chat").body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
#[tokio::test]
async fn test_merge_axum() {
let axum_router = AxumRouter::new().route("/merged", axum::routing::get(mock_handler));
let router = Router::new().merge_axum(axum_router);
let app = router.into_axum();
let response = app
.oneshot(
Request::builder()
.uri("/merged")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
}