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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
use std::convert::Infallible;
use axum::{extract::Request, response::IntoResponse, routing::Route};
use tower::{Layer, Service};
use super::describe;
use crate::app::AppContext;
#[derive(Clone, Default, Debug)]
pub struct Routes {
pub prefix: Option<String>,
pub handlers: Vec<Handler>,
// pub version: Option<String>,
}
#[derive(Clone, Default, Debug)]
pub struct Handler {
pub uri: String,
pub method: axum::routing::MethodRouter<AppContext>,
pub actions: Vec<axum::http::Method>,
}
impl Routes {
/// Creates a new [`Routes`] instance with default settings.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Set a prefix for the routes. this prefix will be a prefix for all the
/// routes.
///
/// # Example
///
/// In the following example the we are adding `status` as a prefix to the
/// _ping endpoint HOST/status/_ping.
///
/// ```rust
/// use pipi::prelude::*;
/// use serde::Serialize;;
///
/// #[derive(Serialize)]
/// struct Health {
/// pub ok: bool,
/// }
///
/// async fn ping() -> Result<Response> {
/// format::json(Health { ok: true })
/// }
/// Routes::at("status").add("/_ping", get(ping));
///
/// ````
#[must_use]
pub fn at(prefix: &str) -> Self {
Self {
prefix: Some(prefix.to_string()),
..Self::default()
}
}
/// Adding new router
///
/// # Example
///
/// This example preset how to add a get endpoint int the Router.
///
/// ```rust
/// use pipi::prelude::*;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Health {
/// pub ok: bool,
/// }
///
/// async fn ping() -> Result<Response> {
/// format::json(Health { ok: true })
/// }
/// Routes::new().add("/_ping", get(ping));
/// ````
#[must_use]
pub fn add(mut self, uri: &str, method: axum::routing::MethodRouter<AppContext>) -> Self {
describe::method_action(&method);
self.handlers.push(Handler {
uri: uri.to_owned(),
actions: describe::method_action(&method),
method,
});
self
}
/// Merge another Routes instance into this one.
///
/// This method allows you to combine multiple Routes instances into a single
/// Routes struct. All handlers from the other Routes will be added to this one.
/// This is useful for collecting routes from different controllers before
/// nesting them under a common prefix.
///
/// # Example
///
/// ```rust
/// use pipi::prelude::*;
/// use axum::routing::{get, post};
///
/// async fn list_users() -> Result<Response> {
/// format::json("users list")
/// }
///
/// async fn create_user() -> Result<Response> {
/// format::json("user created")
/// }
///
/// async fn list_products() -> Result<Response> {
/// format::json("products list")
/// }
///
/// async fn create_product() -> Result<Response> {
/// format::json("product created")
/// }
///
/// // Create separate route groups
/// let user_routes = Routes::new()
/// .add("/users", get(list_users))
/// .add("/users", post(create_user));
///
/// let product_routes = Routes::new()
/// .add("/products", get(list_products))
/// .add("/products", post(create_product));
///
/// // Merge them into a single Routes instance
/// let api_routes = Routes::new()
/// .merge(user_routes)
/// .merge(product_routes);
///
/// // Now nest the combined routes under /api
/// let app_routes = Routes::new()
/// .add("/health", get(|| async { "ok" }))
/// .nest("/api", api_routes);
///
/// // This will result in routes:
/// // - GET /health
/// // - GET /api/users
/// // - POST /api/users
/// // - GET /api/products
/// // - POST /api/products
/// ```
#[must_use]
pub fn merge(mut self, other: Self) -> Self {
// Extend the handlers vector with all handlers from the other Routes
self.handlers.extend(other.handlers);
self
}
/// Merge multiple Routes instances into this one.
///
/// This is a convenience method that allows you to merge multiple Routes
/// instances at once, which is particularly useful when setting up `AppRoutes`
/// and you want to collect routes from different controllers before nesting them.
///
/// # Example
///
/// ```rust
/// use pipi::prelude::*;
/// use axum::routing::{get, post};
///
/// async fn list_users() -> Result<Response> {
/// format::json("users list")
/// }
///
/// async fn list_products() -> Result<Response> {
/// format::json("products list")
/// }
///
/// async fn list_orders() -> Result<Response> {
/// format::json("orders list")
/// }
///
/// // Create separate route groups from different controllers
/// let user_routes = Routes::new().add("/users", get(list_users));
/// let product_routes = Routes::new().add("/products", get(list_products));
/// let order_routes = Routes::new().add("/orders", get(list_orders));
///
/// // Merge all of them at once
/// let api_routes = Routes::new().merge_all(vec![user_routes, product_routes, order_routes]);
///
/// // Now nest the combined routes under /api
/// let app_routes = Routes::new()
/// .add("/health", get(|| async { "ok" }))
/// .nest("/api", api_routes);
///
/// // This will result in routes:
/// // - GET /health
/// // - GET /api/users
/// // - GET /api/products
/// // - GET /api/orders
/// ```
#[must_use]
pub fn merge_all(mut self, others: Vec<Self>) -> Self {
// Extend the handlers vector with all handlers from all Routes
for other in others {
self.handlers.extend(other.handlers);
}
self
}
/// Set a prefix for the routes. this prefix will be a prefix for all the
/// routes.
///
/// # Example
///
/// In the following example the we are adding `status` as a prefix to the
/// _ping endpoint HOST/status/_ping.
///
/// ```rust
/// use pipi::prelude::*;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Health {
/// pub ok: bool,
/// }
///
/// async fn ping() -> Result<Response> {
/// format::json(Health { ok: true })
/// }
/// Routes::new().prefix("status").add("/_ping", get(ping));
/// ````
#[must_use]
pub fn prefix(mut self, uri: &str) -> Self {
self.prefix = Some(uri.to_owned());
self
}
/// Set a layer for the routes. this layer will be a layer for all the
/// routes.
///
/// # Example
///
/// In the following example, we are adding a layer to the routes.
///
/// ```rust
/// use pipi::prelude::*;
/// use tower::{Layer, Service};
/// use tower_http::timeout::TimeoutLayer;
/// async fn ping() -> Result<Response> {
/// format::json("Ok")
/// }
/// Routes::new().prefix("status").add("/_ping", get(ping)).layer(TimeoutLayer::new(std::time::Duration::from_secs(5)));
/// ```
#[allow(clippy::needless_pass_by_value)]
#[must_use]
pub fn layer<L>(self, layer: L) -> Self
where
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,
{
Self {
prefix: self.prefix,
handlers: self
.handlers
.iter()
.map(|handler| Handler {
uri: handler.uri.clone(),
actions: handler.actions.clone(),
method: handler.method.clone().layer(layer.clone()),
})
.collect(),
}
}
/// Nest another Routes instance under a prefix path.
///
/// This method allows you to nest a group of routes under a specific path prefix,
/// similar to Axum's `nest` method. The nested routes will have their URIs
/// prefixed with the given path.
///
/// # Example
///
/// ```rust
/// use pipi::prelude::*;
/// use axum::routing::{get, post, delete, patch};
///
/// // Define user-related handlers
/// async fn list_users() -> Result<Response> {
/// format::json("users list")
/// }
///
/// async fn get_user() -> Result<Response> {
/// format::json("user detail")
/// }
///
/// async fn create_user() -> Result<Response> {
/// format::json("user created")
/// }
///
/// async fn update_user() -> Result<Response> {
/// format::json("user updated")
/// }
///
/// async fn delete_user() -> Result<Response> {
/// format::json("user deleted")
/// }
///
/// // Create API routes for users
/// let user_routes = Routes::new()
/// .add("/users", get(list_users))
/// .add("/users", post(create_user))
/// .add("/users/{id}", get(get_user))
/// .add("/users/{id}", patch(update_user))
/// .add("/users/{id}", delete(delete_user));
///
/// // Create the main application routes
/// let app_routes = Routes::new()
/// .add("/health", get(|| async { "ok" }))
/// .nest("/api/v1", user_routes);
///
/// // This will result in routes:
/// // - GET /health
/// // - GET /api/v1/users
/// // - POST /api/v1/users
/// // - GET /api/v1/users/{id}
/// // - PATCH /api/v1/users/{id}
/// // - DELETE /api/v1/users/{id}
/// ```
#[must_use]
pub fn nest(mut self, path: &str, nested_routes: Self) -> Self {
// Normalize the path to ensure it starts with / and doesn't end with /
let mut normalized_path = path.to_string();
if !normalized_path.starts_with('/') {
normalized_path.insert(0, '/');
}
if normalized_path.ends_with('/') && normalized_path != "/" {
normalized_path.pop();
}
// Process each handler from the nested routes
for handler in nested_routes.handlers {
// Combine the path prefix with the handler's URI
let combined_uri = if handler.uri == "/" {
normalized_path.clone()
} else {
format!("{}{}", normalized_path, handler.uri)
};
// Create a new handler with the combined URI
let new_handler = Handler {
uri: combined_uri,
method: handler.method,
actions: handler.actions,
};
self.handlers.push(new_handler);
}
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::*;
use axum::routing::get;
async fn users() -> Result<Response> {
format::json("users list")
}
async fn user_detail() -> Result<Response> {
format::json("user detail")
}
async fn ping() -> Result<Response> {
format::json("pong")
}
#[test]
fn test_nest_method() {
// Create nested routes
let api_routes = Routes::new()
.add("/users", get(users))
.add("/users/{id}", get(user_detail));
// Nest them under /api
let app_routes = Routes::new()
.add("/ping", get(ping))
.nest("/api", api_routes);
// Verify the routes are correctly nested
assert_eq!(app_routes.handlers.len(), 3);
// Check that the ping route is unchanged
let ping_handler = &app_routes.handlers[0];
assert_eq!(ping_handler.uri, "/ping");
// Check that the nested routes have the correct prefixes
let users_handler = &app_routes.handlers[1];
assert_eq!(users_handler.uri, "/api/users");
let user_detail_handler = &app_routes.handlers[2];
assert_eq!(user_detail_handler.uri, "/api/users/{id}");
}
#[test]
fn test_nest_method_with_root_path() {
// Create nested routes with a root path
let api_routes = Routes::new()
.add("/", get(users))
.add("/users", get(user_detail));
// Nest them under /api
let app_routes = Routes::new().nest("/api", api_routes);
// Verify the routes are correctly nested
assert_eq!(app_routes.handlers.len(), 2);
// Check that the root path is handled correctly
let root_handler = &app_routes.handlers[0];
assert_eq!(root_handler.uri, "/api");
let users_handler = &app_routes.handlers[1];
assert_eq!(users_handler.uri, "/api/users");
}
#[test]
fn test_nest_method_with_trailing_slash() {
// Create nested routes
let api_routes = Routes::new().add("/users", get(users));
// Nest them under /api/ (with trailing slash)
let app_routes = Routes::new().nest("/api/", api_routes);
// Verify the routes are correctly nested (trailing slash should be removed)
assert_eq!(app_routes.handlers.len(), 1);
let users_handler = &app_routes.handlers[0];
assert_eq!(users_handler.uri, "/api/users");
}
#[test]
fn test_nest_method_without_leading_slash() {
// Create nested routes
let api_routes = Routes::new().add("/users", get(users));
// Nest them under api (without leading slash)
let app_routes = Routes::new().nest("api", api_routes);
// Verify the routes are correctly nested (leading slash should be added)
assert_eq!(app_routes.handlers.len(), 1);
let users_handler = &app_routes.handlers[0];
assert_eq!(users_handler.uri, "/api/users");
}
#[test]
fn test_merge_method() {
// Create separate route groups
let user_routes = Routes::new()
.add("/users", get(users))
.add("/users/{id}", get(user_detail));
let product_routes = Routes::new()
.add("/products", get(users))
.add("/products/{id}", get(user_detail));
// Merge them into a single Routes instance
let merged_routes = Routes::new().merge(user_routes).merge(product_routes);
// Verify all routes are present
assert_eq!(merged_routes.handlers.len(), 4);
// Check user routes
let user_list_handler = &merged_routes.handlers[0];
assert_eq!(user_list_handler.uri, "/users");
let user_detail_handler = &merged_routes.handlers[1];
assert_eq!(user_detail_handler.uri, "/users/{id}");
// Check product routes
let product_list_handler = &merged_routes.handlers[2];
assert_eq!(product_list_handler.uri, "/products");
let product_detail_handler = &merged_routes.handlers[3];
assert_eq!(product_detail_handler.uri, "/products/{id}");
}
#[test]
fn test_merge_and_nest_combination() {
// Create separate route groups
let user_routes = Routes::new()
.add("/users", get(users))
.add("/users/{id}", get(user_detail));
let product_routes = Routes::new()
.add("/products", get(users))
.add("/products/{id}", get(user_detail));
// Merge them and then nest under /api
let api_routes = Routes::new().merge(user_routes).merge(product_routes);
let app_routes = Routes::new()
.add("/health", get(ping))
.nest("/api", api_routes);
// Verify the final structure
assert_eq!(app_routes.handlers.len(), 5);
// Check health route is at root level
let health_handler = &app_routes.handlers[0];
assert_eq!(health_handler.uri, "/health");
// Check nested user routes
let user_list_handler = &app_routes.handlers[1];
assert_eq!(user_list_handler.uri, "/api/users");
let user_detail_handler = &app_routes.handlers[2];
assert_eq!(user_detail_handler.uri, "/api/users/{id}");
// Check nested product routes
let product_list_handler = &app_routes.handlers[3];
assert_eq!(product_list_handler.uri, "/api/products");
let product_detail_handler = &app_routes.handlers[4];
assert_eq!(product_detail_handler.uri, "/api/products/{id}");
}
#[test]
fn test_merge_all_method() {
// Create separate route groups
let user_routes = Routes::new().add("/users", get(users));
let product_routes = Routes::new().add("/products", get(users));
let order_routes = Routes::new().add("/orders", get(users));
// Merge all of them at once
let merged_routes =
Routes::new().merge_all(vec![user_routes, product_routes, order_routes]);
// Verify all routes are present
assert_eq!(merged_routes.handlers.len(), 3);
// Check all routes are present
let user_handler = &merged_routes.handlers[0];
assert_eq!(user_handler.uri, "/users");
let product_handler = &merged_routes.handlers[1];
assert_eq!(product_handler.uri, "/products");
let order_handler = &merged_routes.handlers[2];
assert_eq!(order_handler.uri, "/orders");
}
#[test]
fn test_merge_all_and_nest_combination() {
// Create separate route groups from different controllers
let user_routes = Routes::new().add("/users", get(users));
let product_routes = Routes::new().add("/products", get(users));
let order_routes = Routes::new().add("/orders", get(users));
// Merge all and then nest under /api
let api_routes = Routes::new().merge_all(vec![user_routes, product_routes, order_routes]);
let app_routes = Routes::new()
.add("/health", get(ping))
.nest("/api", api_routes);
// Verify the final structure
assert_eq!(app_routes.handlers.len(), 4);
// Check health route is at root level
let health_handler = &app_routes.handlers[0];
assert_eq!(health_handler.uri, "/health");
// Check nested routes
let user_handler = &app_routes.handlers[1];
assert_eq!(user_handler.uri, "/api/users");
let product_handler = &app_routes.handlers[2];
assert_eq!(product_handler.uri, "/api/products");
let order_handler = &app_routes.handlers[3];
assert_eq!(order_handler.uri, "/api/orders");
}
}