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
//! Plugin system for extending framework functionality with composable modules.
//!
//! This module provides the core plugin infrastructure for Tako, allowing developers
//! to extend the framework with reusable components. Plugins can add middleware,
//! modify routing behavior, or integrate external services. The `TakoPlugin` trait
//! defines the interface all plugins must implement for registration and setup.
//!
//! Plugins can be applied at two levels:
//! - **Router-level**: Applied globally to all routes using `router.plugin()`
//! - **Route-level**: Applied to specific routes using `route.plugin()`
//!
//! # Examples
//!
//! ```rust
//! use tako::plugins::TakoPlugin;
//! use tako::router::Router;
//! use tako::Method;
//! use anyhow::Result;
//!
//! struct LoggingPlugin {
//! level: String,
//! }
//!
//! impl TakoPlugin for LoggingPlugin {
//! fn name(&self) -> &'static str {
//! "logging"
//! }
//!
//! fn setup(&self, _router: &Router) -> Result<()> {
//! println!("Setting up logging plugin with level: {}", self.level);
//! Ok(())
//! }
//! }
//!
//! async fn handler(_req: tako::types::Request) -> &'static str {
//! "Hello"
//! }
//!
//! // Router-level plugin (applied to all routes)
//! let mut router = Router::new();
//! router.plugin(LoggingPlugin { level: "info".to_string() });
//!
//! // Route-level plugin (applied to specific route only)
//! let route = router.route(Method::GET, "/api/data", handler);
//! route.plugin(LoggingPlugin { level: "debug".to_string() });
//! ```
use Result;
use crateRouter;
/// Compression plugin for automatic response compression.
/// CORS (Cross-Origin Resource Sharing) plugin for handling cross-origin requests.
/// Rate limiting plugin for controlling request frequency.
/// Metrics/tracing plugin for integrating with systems like Prometheus or OpenTelemetry.
/// Idempotency-Key based request de-duplication plugin.
/// Trait for implementing Tako framework plugins.
///
/// Plugins extend the framework's functionality by implementing this trait. They can
/// add middleware, modify routing behavior, register handlers, or integrate external
/// services. All plugins must be thread-safe and have a static lifetime.
///
/// Plugins can be applied at both router and route levels:
/// - **Router-level**: Use `router.plugin()` to apply globally
/// - **Route-level**: Use `route.plugin()` to apply to specific routes
///
/// # Examples
///
/// ```rust
/// use tako::plugins::TakoPlugin;
/// use tako::router::Router;
/// use tako::Method;
/// use anyhow::Result;
///
/// struct CachePlugin {
/// ttl_seconds: u64,
/// }
///
/// impl TakoPlugin for CachePlugin {
/// fn name(&self) -> &'static str {
/// "cache"
/// }
///
/// fn setup(&self, router: &Router) -> Result<()> {
/// // Add middleware to the router
/// router.middleware(|req, next| async move {
/// // Cache logic here
/// next.run(req).await
/// });
/// Ok(())
/// }
/// }
///
/// async fn handler(_req: tako::types::Request) -> &'static str {
/// "Hello"
/// }
///
/// // Router-level usage
/// let mut router = Router::new();
/// router.plugin(CachePlugin { ttl_seconds: 300 });
///
/// // Route-level usage
/// let route = router.route(Method::GET, "/api/data", handler);
/// route.plugin(CachePlugin { ttl_seconds: 600 });
/// ```