dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//!
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//!     http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.

#![allow(non_snake_case)]

//! # Routing Module
//! 
//! This module provides a flexible routing system for the Ri gateway, using a Radix Tree
//! for O(k) route lookup performance, where k is the path length.
//! 
//! ## Key Components
//! 
//! - **RiRouteHandler**: Type alias for route handler functions
//! - **RiRoute**: Represents a single API route with method, path, handler, and middleware
//! - **RiRouter**: Manages routes using radix trees for efficient O(k) lookup
//! 
//! ## Design Principles
//! 
//! 1. **O(k) Performance**: Uses radix tree for route matching independent of route count
//! 2. **Type Safety**: Uses type aliases for clear handler signatures
//! 3. **Middleware Support**: Allows attaching middleware to individual routes
//! 4. **Route Caching**: Caches route matches for improved performance
//! 5. **Flexible Path Matching**: Supports exact paths, wildcards (`*path`), and path parameters (`:param`)
//! 6. **Method Support**: Supports all HTTP methods (GET, POST, PUT, DELETE, PATCH, OPTIONS)
//! 7. **Route Mounting**: Allows mounting routers with prefixes for modularity
//! 8. **Thread Safe**: Uses RwLock for safe operation in multi-threaded environments
//! 9. **Async Compatibility**: Built with async/await patterns for modern Rust applications
//! 
//! ## Path Pattern Syntax
//! 
//! - `/users/:id` - Matches `/users/123`, extracts `id = "123"`
//! - `/files/*path` - Matches `/files/docs/readme.txt`, extracts `path = "docs/readme.txt"`
//! - `/api/v1/users` - Exact match for static paths
//! 
//! ## Usage
//! 
//! ```rust
//! use ri::prelude::*;
//! use std::sync::Arc;
//! 
//! async fn example() -> RiResult<()> {
//!     // Create a router
//!     let router = Arc::new(RiRouter::new());
//!     
//!     // Create a simple handler
//!     let hello_handler = Arc::new(|req| {
//!         Box::pin(async move {
//!             Ok(RiGatewayResponse {
//!                 status_code: 200,
//!                 headers: FxHashMap::default(),
//!                 body: "Hello, Ri!".as_bytes().to_vec(),
//!             })
//!         })
//!     });
//!     
//!     // Add routes with O(k) lookup performance
//!     router.get("/hello", hello_handler.clone());
//!     router.post("/api/v1/users", hello_handler.clone());
//!     
//!     // Add route with path parameter
//!     router.get("/users/:id", hello_handler.clone());
//!     
//!     // Add route with wildcard
//!     router.get("/files/*path", hello_handler.clone());
//!     
//!     // Add route with middleware
//!     let auth_middleware = Arc::new(RiAuthMiddleware::new("Authorization".to_string()));
//!     let protected_route = RiRoute::new("GET".to_string(), "/api/v1/protected".to_string(), hello_handler)
//!         .with_middleware(auth_middleware);
//!     router.add_route(protected_route);
//!     
//!     Ok(())
//! }
//! ```

use super::{RiGatewayRequest, RiGatewayResponse};
use super::radix_tree::RiRadixTree;
use crate::core::RiResult;
use crate::core::lock::RwLockExtensions;
use crate::gateway::middleware::RiMiddleware;
use std::collections::HashMap as FxHashMap;
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

#[cfg(feature = "pyo3")]
use pyo3::PyResult;

/// Type alias for route handler functions.
/// 
/// This type represents an asynchronous function that takes a gateway request and returns
/// a gateway response. It is wrapped in an Arc to allow safe sharing across threads.
pub type RiRouteHandler = Arc<
    dyn Fn(RiGatewayRequest) -> Pin<Box<dyn Future<Output = RiResult<RiGatewayResponse>> + Send>>
        + Send
        + Sync,
>;

/// Represents a single API route with method, path, handler, and middleware.
/// 
/// This struct encapsulates all the information needed for a single API endpoint,
/// including the HTTP method, path pattern, request handler, and attached middleware.
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Clone)]
pub struct RiRoute {
    /// HTTP method for this route (GET, POST, PUT, DELETE, PATCH, OPTIONS)
    pub method: String,
    /// Path pattern for this route (e.g., "/api/v1/users", "/users/:id")
    pub path: String,
    /// Request handler for this route
    pub handler: RiRouteHandler,
    /// List of middleware attached to this route
    pub middleware: Vec<Arc<dyn RiMiddleware>>,
}

impl fmt::Debug for RiRoute {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RiRoute")
            .field("method", &self.method)
            .field("path", &self.path)
            .field("handler", &"<handler>")
            .field("middleware_count", &self.middleware.len())
            .finish()
    }
}

impl RiRoute {
    /// Creates a new route with the specified method, path, and handler.
    /// 
    /// # Parameters
    /// 
    /// - `method`: HTTP method for this route
    /// - `path`: Path pattern for this route
    /// - `handler`: Request handler for this route
    /// 
    /// # Returns
    /// 
    /// A new `RiRoute` instance with no middleware attached
    pub fn new(
        method: String,
        path: String,
        handler: RiRouteHandler,
    ) -> Self {
        Self {
            method,
            path,
            handler,
            middleware: Vec::new(),
        }
    }

    /// Attaches middleware to this route.
    /// 
    /// This method returns a new route instance with the middleware added to the list.
    /// 
    /// # Parameters
    /// 
    /// - `middleware`: Middleware to attach to this route
    /// 
    /// # Returns
    /// 
    /// A new `RiRoute` instance with the middleware attached
    pub fn with_middleware(mut self, middleware: Arc<dyn RiMiddleware>) -> Self {
        self.middleware.push(middleware);
        self
    }
}

#[cfg(feature = "pyo3")]
/// Python bindings for RiRoute
#[pyo3::prelude::pymethods]
impl RiRoute {
    #[new]
    fn py_new(method: String, path: String) -> PyResult<Self> {
        use crate::gateway::{RiGatewayRequest, RiGatewayResponse};
        
        // Create a simple default handler for Python usage
        let handler = Arc::new(|_req: RiGatewayRequest| -> Pin<Box<dyn Future<Output = Result<RiGatewayResponse, crate::core::RiError>> + Send>> {
            Box::pin(async move {
                Ok(RiGatewayResponse {
                    status_code: 200,
                    headers: FxHashMap::default(),
                    body: b"Hello from Ri Python!".to_vec(),
                    request_id: String::new(),
                })
            })
        });
        
        Ok(Self {
            method,
            path,
            handler,
            middleware: Vec::new(),
        })
    }
}

/// Router for managing API routes and matching requests to handlers.
/// 
/// This struct maintains a collection of routes organized by HTTP method using
/// radix trees for O(k) lookup performance, where k is the path length.
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiRouter {
    /// Radix trees for each HTTP method
    trees: std::sync::RwLock<FxHashMap<String, RiRadixTree>>,
    /// Vector of registered routes for backward compatibility and introspection
    routes: std::sync::RwLock<Vec<RiRoute>>,
    /// Cache of route matches for improved performance
    route_cache: std::sync::RwLock<FxHashMap<String, RiRoute>>,
}

impl Default for RiRouter {
    fn default() -> Self {
        Self::new()
    }
}

impl RiRouter {
    /// Creates a new router with no routes.
    /// 
    /// # Returns
    /// 
    /// A new `RiRouter` instance with empty routes and cache
    pub fn new() -> Self {
        Self {
            trees: std::sync::RwLock::new(FxHashMap::default()),
            routes: std::sync::RwLock::new(Vec::new()),
            route_cache: std::sync::RwLock::new(FxHashMap::default()),
        }
    }

    /// Gets or creates a radix tree for the given HTTP method.
    /// 
    /// # Parameters
    /// 
    /// - `method`: The HTTP method
    /// 
    /// # Returns
    /// 
    /// A reference to the radix tree for the method
    #[allow(dead_code)]
    fn get_or_create_tree(&self, method: &str) -> RiRadixTree {
        let trees = match self.trees.read_safe("trees for get_or_create") {
            Ok(t) => t,
            Err(_) => return RiRadixTree::new(),
        };
        
        if let Some(_tree) = trees.get(method) {
            return RiRadixTree::new();
        }
        
        drop(trees);
        
        let mut trees_mut = match self.trees.write_safe("trees for create") {
            Ok(t) => t,
            Err(_) => return RiRadixTree::new(),
        };
        
        let tree = RiRadixTree::new();
        trees_mut.insert(method.to_string(), RiRadixTree::new());
        tree
    }

    /// Adds a route to the router.
    /// 
    /// This method adds a route to the router's radix tree and clears the route cache.
    /// 
    /// # Parameters
    /// 
    /// - `route`: The route to add to the router
    pub fn add_route(&self, route: RiRoute) {
        let method = route.method.clone();
        
        let mut trees = match self.trees.write_safe("trees for add_route") {
            Ok(t) => t,
            Err(e) => {
                log::error!("Failed to acquire trees write lock: {}", e);
                return;
            }
        };
        
        let tree = trees.entry(method).or_insert_with(RiRadixTree::new);
        tree.insert(route.clone());
        
        drop(trees);
        
        let mut routes = match self.routes.write_safe("routes for add_route") {
            Ok(r) => r,
            Err(e) => {
                log::error!("Failed to acquire routes write lock: {}", e);
                return;
            }
        };
        routes.push(route);
        
        let mut cache = match self.route_cache.write_safe("cache for add_route") {
            Ok(c) => c,
            Err(e) => {
                log::error!("Failed to acquire cache write lock: {}", e);
                return;
            }
        };
        cache.clear();
    }
}

#[cfg(feature = "pyo3")]
/// Python bindings for RiRouter
#[pyo3::prelude::pymethods]
impl RiRouter {
    #[new]
    fn py_new() -> Self {
        Self::new()
    }
    
    /// Adds a GET route to the router from Python
    fn add_get_route(&self, path: String) {
        let route = RiRoute::py_new("GET".to_string(), path).expect("Failed to create route");
        self.add_route(route);
    }
    
    /// Adds a POST route to the router from Python
    fn add_post_route(&self, path: String) {
        let route = RiRoute::py_new("POST".to_string(), path).expect("Failed to create route");
        self.add_route(route);
    }
    
    /// Adds a PUT route to the router from Python
    fn add_put_route(&self, path: String) {
        let route = RiRoute::py_new("PUT".to_string(), path).expect("Failed to create route");
        self.add_route(route);
    }
    
    /// Adds a DELETE route to the router from Python
    fn add_delete_route(&self, path: String) {
        let route = RiRoute::py_new("DELETE".to_string(), path).expect("Failed to create route");
        self.add_route(route);
    }
    
    /// Adds a PATCH route to the router from Python
    fn add_patch_route(&self, path: String) {
        let route = RiRoute::py_new("PATCH".to_string(), path).expect("Failed to create route");
        self.add_route(route);
    }
    
    /// Adds an OPTIONS route to the router from Python
    fn add_options_route(&self, path: String) {
        let route = RiRoute::py_new("OPTIONS".to_string(), path).expect("Failed to create route");
        self.add_route(route);
    }
    
    /// Adds a custom route to the router from Python
    fn add_custom_route(&self, method: String, path: String) {
        let route = RiRoute::py_new(method, path).expect("Failed to create route");
        self.add_route(route);
    }
    
    /// Gets the number of routes registered in the router
    fn get_route_count(&self) -> usize {
        self.route_count()
    }
    
    /// Clears all routes from the router
    fn clear_all_routes(&self) {
        self.clear_routes();
    }
}

impl RiRouter {

    /// Adds a GET route to the router.
    /// 
    /// # Parameters
    /// 
    /// - `path`: Path pattern for the route
    /// - `handler`: Request handler for the route
    pub fn get(&self, path: &str, handler: RiRouteHandler) {
        let route = RiRoute::new("GET".to_string(), path.to_string(), handler);
        self.add_route(route);
    }

    /// Adds a POST route to the router.
    /// 
    /// # Parameters
    /// 
    /// - `path`: Path pattern for the route
    /// - `handler`: Request handler for the route
    pub fn post(&self, path: &str, handler: RiRouteHandler) {
        let route = RiRoute::new("POST".to_string(), path.to_string(), handler);
        self.add_route(route);
    }

    /// Adds a PUT route to the router.
    /// 
    /// # Parameters
    /// 
    /// - `path`: Path pattern for the route
    /// - `handler`: Request handler for the route
    pub fn put(&self, path: &str, handler: RiRouteHandler) {
        let route = RiRoute::new("PUT".to_string(), path.to_string(), handler);
        self.add_route(route);
    }

    /// Adds a DELETE route to the router.
    /// 
    /// # Parameters
    /// 
    /// - `path`: Path pattern for the route
    /// - `handler`: Request handler for the route
    pub fn delete(&self, path: &str, handler: RiRouteHandler) {
        let route = RiRoute::new("DELETE".to_string(), path.to_string(), handler);
        self.add_route(route);
    }

    /// Adds a PATCH route to the router.
    /// 
    /// # Parameters
    /// 
    /// - `path`: Path pattern for the route
    /// - `handler`: Request handler for the route
    pub fn patch(&self, path: &str, handler: RiRouteHandler) {
        let route = RiRoute::new("PATCH".to_string(), path.to_string(), handler);
        self.add_route(route);
    }

    /// Adds an OPTIONS route to the router.
    /// 
    /// # Parameters
    /// 
    /// - `path`: Path pattern for the route
    /// - `handler`: Request handler for the route
    pub fn options(&self, path: &str, handler: RiRouteHandler) {
        let route = RiRoute::new("OPTIONS".to_string(), path.to_string(), handler);
        self.add_route(route);
    }

    /// Finds a matching route for the given request using radix tree.
    /// 
    /// This method uses radix tree for O(k) route lookup where k is the path length.
    /// It checks the route cache first, then searches the radix tree for the matching route.
    /// 
    /// # Parameters
    /// 
    /// - `request`: The gateway request to find a route for
    /// 
    /// # Returns
    /// 
    /// A `RiResult<RiRouteHandler>` with the matching handler, or an error if no route is found
    pub async fn route(&self, request: &RiGatewayRequest) -> RiResult<RiRouteHandler> {
        let cache_key = format!("{}:{}", request.method, request.path);
        
        {
            let cache = match self.route_cache.read_safe("cache for route lookup") {
                Ok(c) => c,
                Err(_) => return Err(crate::core::RiError::InvalidState("Failed to acquire cache read lock".to_string())),
            };
            if let Some(cached_route) = cache.get(&cache_key) {
                return Ok(cached_route.handler.clone());
            }
        }

        let trees = match self.trees.read_safe("trees for route lookup") {
            Ok(t) => t,
            Err(_) => return Err(crate::core::RiError::InvalidState("Failed to acquire trees read lock".to_string())),
        };
        
        if let Some(tree) = trees.get(&request.method) {
            if let Some(route_match) = tree.find(&request.path) {
                let mut cache = match self.route_cache.write_safe("cache for route insert") {
                    Ok(c) => c,
                    Err(_) => return Ok(route_match.route.handler.clone()),
                };
                cache.insert(cache_key.clone(), route_match.route.clone());
                
                return Ok(route_match.route.handler.clone());
            }
        }

        Err(crate::core::RiError::Other(format!(
            "No route found for {} {}",
            request.method, request.path
        )))
    }

    /// Checks if a route matches a request.
    /// 
    /// This method implements route matching logic using the radix tree.
    /// 
    /// # Parameters
    /// 
    /// - `route_method`: HTTP method of the route
    /// - `route_path`: Path pattern of the route
    /// - `request_method`: HTTP method of the request
    /// - `request_path`: Path of the request
    /// 
    /// # Returns
    /// 
    /// `true` if the route matches the request, `false` otherwise
    #[allow(dead_code)]
    fn matches_route(&self, route_method: &str, route_path: &str, request_method: &str, request_path: &str) -> bool {
        if route_method != request_method {
            return false;
        }

        let trees = match self.trees.read_safe("trees for matches_route") {
            Ok(t) => t,
            Err(_) => return false,
        };
        
        if let Some(tree) = trees.get(route_method) {
            if let Some(route_match) = tree.find(request_path) {
                return route_match.route.path == route_path;
            }
        }

        false
    }

    /// Mounts another router's routes with a prefix.
    /// 
    /// This method adds all routes from another router to this router, prepending
    /// the specified prefix to each route's path.
    /// 
    /// # Parameters
    /// 
    /// - `prefix`: The prefix to prepend to all mounted routes
    /// - `router`: The router to mount
    pub fn mount(&self, prefix: &str, router: &RiRouter) {
        let routes = match router.routes.read_safe("routes for mount") {
            Ok(r) => r,
            Err(_) => return,
        };
        for route in routes.iter() {
            let mounted_path = if prefix.ends_with('/') && route.path.starts_with('/') {
                format!("{}{}", prefix, &route.path[1..])
            } else if !prefix.ends_with('/') && !route.path.starts_with('/') {
                format!("{}/{}", prefix, route.path)
            } else {
                format!("{}{}", prefix, route.path)
            };

            let mut mounted_route = route.clone();
            mounted_route.path = mounted_path;
            self.add_route(mounted_route);
        }
    }

    /// Clears all routes from the router.
    /// 
    /// This method removes all routes from the router, clears all radix trees, and clears the route cache.
    pub fn clear_routes(&self) {
        let trees = match self.trees.write_safe("trees for clear") {
            Ok(t) => t,
            Err(e) => {
                log::error!("Failed to acquire trees write lock: {}", e);
                return;
            }
        };
        for tree in trees.values() {
            tree.clear();
        }
        drop(trees);
        
        let mut routes = match self.routes.write_safe("routes for clear") {
            Ok(r) => r,
            Err(e) => {
                log::error!("Failed to acquire routes write lock: {}", e);
                return;
            }
        };
        let mut cache = match self.route_cache.write_safe("cache for clear") {
            Ok(c) => c,
            Err(e) => {
                log::error!("Failed to acquire cache write lock: {}", e);
                return;
            }
        };
        routes.clear();
        cache.clear();
    }

    /// Gets the number of routes registered in the router.
    /// 
    /// # Returns
    /// 
    /// The number of routes registered in the router
    pub fn route_count(&self) -> usize {
        match self.routes.read_safe("routes for count") {
            Ok(routes) => routes.len(),
            Err(_) => 0,
        }
    }
}