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
//! Route registration and HTTP-method builder shorthands.
use std::sync::Arc;
use http::Method;
use super::Router;
use crate::handler::BoxHandler;
use crate::handler::Handler;
use crate::route::Route;
impl Router {
/// Registers a new route with the router.
///
/// Associates an HTTP method and path pattern with a handler function. The path
/// can contain dynamic segments using curly braces (e.g., `/users/{id}`), which
/// are extracted as parameters during request processing.
///
/// # Panics
///
/// Panics if a route with the same method and path pattern is already registered.
///
/// # Examples
///
/// ```rust
/// use tako::{router::Router, Method, responder::Responder, types::Request};
///
/// async fn get_user(_req: Request) -> impl Responder {
/// "User details"
/// }
///
/// async fn create_user(_req: Request) -> impl Responder {
/// "User created"
/// }
///
/// let mut router = Router::new();
/// router.route(Method::GET, "/users/{id}", get_user);
/// router.route(Method::POST, "/users", create_user);
/// router.route(Method::GET, "/health", |_req| async { "OK" });
/// ```
pub fn route<H, T>(&mut self, method: Method, path: &str, handler: H) -> Arc<Route>
where
H: Handler<T> + Clone + 'static,
{
let final_path = self.apply_pending_prefix(path);
let route = Arc::new(Route::new(
final_path.clone(),
method.clone(),
BoxHandler::new::<H, T>(handler),
None,
));
if let Err(err) = self
.inner
.get_or_default_mut(&method)
.insert(final_path, route.clone())
{
panic!("Failed to register route: {err}");
}
self
.routes
.get_or_default_mut(&method)
.push(Arc::downgrade(&route));
route
}
/// Returns `path` with the active `pending_prefix` (if any) prepended.
/// Cold path; only runs at registration time.
pub(crate) fn apply_pending_prefix(&self, path: &str) -> String {
match &self.pending_prefix {
None => path.to_string(),
Some(prefix) => {
let prefix = prefix.trim_end_matches('/');
if path.is_empty() || path == "/" {
if prefix.is_empty() {
"/".to_string()
} else {
prefix.to_string()
}
} else if path.starts_with('/') {
let mut s = String::with_capacity(prefix.len() + path.len());
s.push_str(prefix);
s.push_str(path);
s
} else {
let mut s = String::with_capacity(prefix.len() + 1 + path.len());
s.push_str(prefix);
s.push('/');
s.push_str(path);
s
}
}
}
}
/// Registers a `GET` route. Shorthand for [`Router::route`] with [`Method::GET`].
#[inline]
pub fn get<H, T>(&mut self, path: &str, handler: H) -> Arc<Route>
where
H: Handler<T> + Clone + 'static,
{
self.route(Method::GET, path, handler)
}
/// Registers a `POST` route. Shorthand for [`Router::route`] with [`Method::POST`].
#[inline]
pub fn post<H, T>(&mut self, path: &str, handler: H) -> Arc<Route>
where
H: Handler<T> + Clone + 'static,
{
self.route(Method::POST, path, handler)
}
/// Registers a `PUT` route. Shorthand for [`Router::route`] with [`Method::PUT`].
#[inline]
pub fn put<H, T>(&mut self, path: &str, handler: H) -> Arc<Route>
where
H: Handler<T> + Clone + 'static,
{
self.route(Method::PUT, path, handler)
}
/// Registers a `DELETE` route. Shorthand for [`Router::route`] with [`Method::DELETE`].
#[inline]
pub fn delete<H, T>(&mut self, path: &str, handler: H) -> Arc<Route>
where
H: Handler<T> + Clone + 'static,
{
self.route(Method::DELETE, path, handler)
}
/// Registers a `PATCH` route. Shorthand for [`Router::route`] with [`Method::PATCH`].
#[inline]
pub fn patch<H, T>(&mut self, path: &str, handler: H) -> Arc<Route>
where
H: Handler<T> + Clone + 'static,
{
self.route(Method::PATCH, path, handler)
}
/// Registers a `HEAD` route. Shorthand for [`Router::route`] with [`Method::HEAD`].
#[inline]
pub fn head<H, T>(&mut self, path: &str, handler: H) -> Arc<Route>
where
H: Handler<T> + Clone + 'static,
{
self.route(Method::HEAD, path, handler)
}
/// Registers an `OPTIONS` route. Shorthand for [`Router::route`] with [`Method::OPTIONS`].
#[inline]
pub fn options<H, T>(&mut self, path: &str, handler: H) -> Arc<Route>
where
H: Handler<T> + Clone + 'static,
{
self.route(Method::OPTIONS, path, handler)
}
/// Registers a route with trailing slash redirection enabled.
///
/// When TSR is enabled, requests to paths with or without trailing slashes
/// are automatically redirected to the canonical version. This helps maintain
/// consistent URLs and prevents duplicate content issues.
///
/// # Panics
///
/// - Panics if called with the root path (`"/"`) since TSR is not applicable.
/// - Panics if a route with the same method and path pattern is already registered.
///
/// # Examples
///
/// ```rust
/// use tako::{router::Router, Method, responder::Responder, types::Request};
///
/// async fn api_handler(_req: Request) -> impl Responder {
/// "API endpoint"
/// }
///
/// let mut router = Router::new();
/// // Both "/api" and "/api/" will redirect to the canonical form
/// router.route_with_tsr(Method::GET, "/api", api_handler);
/// ```
pub fn route_with_tsr<H, T>(&mut self, method: Method, path: &str, handler: H) -> Arc<Route>
where
H: Handler<T> + Clone + 'static,
{
assert!(path != "/", "Cannot route with TSR for root path");
let final_path = self.apply_pending_prefix(path);
let route = Arc::new(Route::new(
final_path.clone(),
method.clone(),
BoxHandler::new::<H, T>(handler),
Some(true),
));
if let Err(err) = self
.inner
.get_or_default_mut(&method)
.insert(final_path, route.clone())
{
panic!("Failed to register route: {err}");
}
self
.routes
.get_or_default_mut(&method)
.push(Arc::downgrade(&route));
route
}
}