1use crate::handler::{BoxFuture, Handler};
2use crate::request::Request;
3use crate::response::Response;
4use std::future::Future;
5use std::sync::Arc;
6
7pub type Next = Box<dyn FnOnce(Request) -> BoxFuture<Response> + Send>;
9
10pub type Middleware = Arc<dyn Fn(Request, Next) -> BoxFuture<Response> + Send + Sync>;
12
13#[derive(Clone)]
15pub struct MwEntry {
16 pub name: String,
17 pub mw: Middleware,
18}
19
20pub fn named(name: impl Into<String>, mw: impl IntoMiddleware) -> MwEntry {
22 MwEntry {
23 name: name.into(),
24 mw: mw.into_middleware(),
25 }
26}
27
28pub(crate) fn abbreviate_type_name(full: &str) -> String {
29 let trimmed = full
30 .trim_end_matches(">::{{closure}}")
31 .trim_end_matches("::{{closure}}")
32 .trim_end_matches("{{closure}}");
33 let base = trimmed.rsplit("::").next().unwrap_or(trimmed);
34 if base.is_empty() || base == "{{closure}}" {
35 "closure".into()
36 } else {
37 base.to_string()
38 }
39}
40
41pub trait IntoMiddleware {
42 fn into_middleware(self) -> Middleware;
43}
44
45pub trait IntoMwEntry {
47 fn into_mw_entry(self) -> MwEntry;
48}
49
50impl<F, Fut> IntoMiddleware for F
51where
52 F: Fn(Request, Next) -> Fut + Send + Sync + 'static,
53 Fut: Future<Output = Response> + Send + 'static,
54{
55 fn into_middleware(self) -> Middleware {
56 Arc::new(move |req, next| Box::pin(self(req, next)))
57 }
58}
59
60impl IntoMiddleware for Middleware {
61 fn into_middleware(self) -> Middleware {
62 self
63 }
64}
65
66impl IntoMwEntry for MwEntry {
67 fn into_mw_entry(self) -> MwEntry {
68 self
69 }
70}
71
72impl IntoMwEntry for Middleware {
73 fn into_mw_entry(self) -> MwEntry {
74 MwEntry {
75 name: "mw".into(),
76 mw: self,
77 }
78 }
79}
80
81impl<F, Fut> IntoMwEntry for F
82where
83 F: Fn(Request, Next) -> Fut + Send + Sync + 'static,
84 Fut: Future<Output = Response> + Send + 'static,
85{
86 fn into_mw_entry(self) -> MwEntry {
87 MwEntry {
88 name: abbreviate_type_name(std::any::type_name::<F>()),
89 mw: self.into_middleware(),
90 }
91 }
92}
93
94pub fn with_state<S, F, Fut>(state: S, f: F) -> Middleware
100where
101 S: Send + Sync + 'static,
102 F: Fn(Arc<S>, Request, Next) -> Fut + Send + Sync + 'static,
103 Fut: Future<Output = Response> + Send + 'static,
104{
105 let state = Arc::new(state);
106 Arc::new(move |req, next| Box::pin(f(Arc::clone(&state), req, next)))
107}
108
109pub fn with_leaked<S, F, Fut>(state: S, f: F) -> Middleware
114where
115 S: Send + Sync + 'static,
116 F: Fn(&'static S, Request, Next) -> Fut + Send + Sync + 'static,
117 Fut: Future<Output = Response> + Send + 'static,
118{
119 let state: &'static S = Box::leak(Box::new(state));
120 Arc::new(move |req, next| Box::pin(f(state, req, next)))
121}
122
123pub fn before<F, Fut>(name: impl Into<String>, f: F) -> MwEntry
125where
126 F: Fn(Request) -> Fut + Send + Sync + 'static,
127 Fut: Future<Output = Request> + Send + 'static,
128{
129 let f = Arc::new(f);
130 named(name, move |req, next: Next| {
131 let f = Arc::clone(&f);
132 async move {
133 let req = f(req).await;
134 next(req).await
135 }
136 })
137}
138
139pub fn after<F, Fut>(name: impl Into<String>, f: F) -> MwEntry
141where
142 F: Fn(Response) -> Fut + Send + Sync + 'static,
143 Fut: Future<Output = Response> + Send + 'static,
144{
145 let f = Arc::new(f);
146 named(name, move |req, next: Next| {
147 let f = Arc::clone(&f);
148 async move {
149 let res = next(req).await;
150 f(res).await
151 }
152 })
153}
154
155pub fn around<B, BF, A, AF>(name: impl Into<String>, before_fn: B, after_fn: A) -> MwEntry
157where
158 B: Fn(Request) -> BF + Send + Sync + 'static,
159 BF: Future<Output = Request> + Send + 'static,
160 A: Fn(Response) -> AF + Send + Sync + 'static,
161 AF: Future<Output = Response> + Send + 'static,
162{
163 let before_fn = Arc::new(before_fn);
164 let after_fn = Arc::new(after_fn);
165 named(name, move |req, next: Next| {
166 let before_fn = Arc::clone(&before_fn);
167 let after_fn = Arc::clone(&after_fn);
168 async move {
169 let req = before_fn(req).await;
170 let res = next(req).await;
171 after_fn(res).await
172 }
173 })
174}
175
176pub fn map_html<F>(name: impl Into<String>, transform: F) -> MwEntry
180where
181 F: Fn(&str) -> Option<String> + Send + Sync + 'static,
182{
183 let transform = Arc::new(transform);
184 after(name, move |mut res| {
185 let transform = Arc::clone(&transform);
186 async move {
187 res.map_buffered_html(|html| transform(html));
188 res
189 }
190 })
191}
192
193pub fn build_chain(middleware: &[Middleware], handler: Handler) -> Handler {
195 let mut next = handler;
196
197 for mw in middleware.iter().rev() {
198 let mw = Arc::clone(mw);
199 let inner = next;
200 next = Arc::new(move |req| {
201 let inner = Arc::clone(&inner);
202 let mw = Arc::clone(&mw);
203 mw(
204 req,
205 Box::new(move |r| {
206 let inner = Arc::clone(&inner);
207 inner(r)
208 }),
209 )
210 });
211 }
212
213 next
214}
215
216pub(crate) fn chain_from_entries(entries: &[MwEntry], handler: Handler) -> Handler {
217 let mws: Vec<Middleware> = entries.iter().map(|e| Arc::clone(&e.mw)).collect();
218 build_chain(&mws, handler)
219}
220
221pub fn logger() -> MwEntry {
226 named("logger", |req: Request, next: Next| async move {
227 let method = req.method.as_str().to_string();
228 let path = req.path.clone();
229 let request_id = req
230 .get::<crate::request_id::RequestId>()
231 .map(|r| r.0.clone())
232 .unwrap_or_default();
233 let start = std::time::Instant::now();
234 let res = next(req).await;
235 if logger_should_skip(&path) {
236 return res;
237 }
238 let status = res.status_code().as_u16();
239 let latency_ms = start.elapsed().as_millis() as u64;
240 if request_id.is_empty() {
241 tracing::info!(
242 method = %method,
243 path = %path,
244 status,
245 latency_ms,
246 "request"
247 );
248 } else {
249 tracing::info!(
250 request_id = %request_id,
251 method = %method,
252 path = %path,
253 status,
254 latency_ms,
255 "request"
256 );
257 }
258 res
259 })
260}
261
262static LOGGER_SKIP_PREFIXES: std::sync::OnceLock<std::sync::Mutex<Vec<String>>> =
263 std::sync::OnceLock::new();
264
265fn logger_skip_list() -> &'static std::sync::Mutex<Vec<String>> {
266 LOGGER_SKIP_PREFIXES.get_or_init(|| std::sync::Mutex::new(Vec::new()))
267}
268
269pub fn logger_skip_path(prefix: impl Into<String>) {
272 let p = prefix.into();
273 if p.is_empty() {
274 return;
275 }
276 let mut g = logger_skip_list().lock().unwrap();
277 if !g.iter().any(|x| x == &p) {
278 g.push(p);
279 }
280}
281
282pub fn logger_skip_paths(prefixes: impl IntoIterator<Item = impl Into<String>>) {
284 for p in prefixes {
285 logger_skip_path(p);
286 }
287}
288
289fn logger_should_skip(path: &str) -> bool {
290 let g = logger_skip_list().lock().unwrap();
291 g.iter().any(|p| path == p.as_str() || path.starts_with(&format!("{p}/")))
292}
293
294#[cfg(any(test, feature = "testing"))]
295pub fn logger_clear_skip_paths() {
296 logger_skip_list().lock().unwrap().clear();
297}
298
299#[cfg(test)]
300mod tests {
301 use super::*;
302 use http::Method;
303
304 fn ok_handler() -> Handler {
305 Arc::new(|_req: Request| Box::pin(async { Response::text("ok") }))
306 }
307
308 #[tokio::test]
309 async fn onion_order() {
310 let leaf = ok_handler();
311 let outer = |req: Request, next: Next| async move {
312 let mut res = next(req).await;
313 res = res.header("x-outer", "1");
314 res
315 };
316 let inner = |req: Request, next: Next| async move {
317 let mut res = next(req).await;
318 res = res.header("x-inner", "1");
319 res
320 };
321 let chain = build_chain(
322 &[outer.into_middleware(), inner.into_middleware()],
323 leaf,
324 );
325 let res = chain(Request::new(Method::GET, "/")).await;
326 assert_eq!(res.body_bytes(), Some(b"ok".as_slice()));
327 assert_eq!(res.headers.get("x-outer").map(|v| v.to_str().unwrap()), Some("1"));
328 assert_eq!(res.headers.get("x-inner").map(|v| v.to_str().unwrap()), Some("1"));
329 }
330
331 #[tokio::test]
332 async fn with_state_runs() {
333 let leaf = ok_handler();
334 let mw = with_state(7u32, |n, req, next| async move {
335 assert_eq!(*n, 7);
336 next(req).await
337 });
338 let chain = build_chain(&[mw], leaf);
339 assert_eq!(
340 chain(Request::new(Method::GET, "/")).await.body_bytes(),
341 Some(b"ok".as_slice())
342 );
343 }
344
345 #[test]
346 fn logger_skip_matches_prefixes() {
347 logger_clear_skip_paths();
348 logger_skip_path("/_devtools");
349 logger_skip_path("/healthz");
350 assert!(logger_should_skip("/_devtools"));
351 assert!(logger_should_skip("/_devtools/config"));
352 assert!(logger_should_skip("/_devtools/requests/dt-1"));
353 assert!(logger_should_skip("/healthz"));
354 assert!(!logger_should_skip("/api/users"));
355 assert!(!logger_should_skip("/health"));
356 logger_clear_skip_paths();
357 }
358
359 #[test]
360 fn abbreviate_closure() {
361 let n = abbreviate_type_name("foo::bar::{{closure}}");
362 assert_eq!(n, "bar");
363 }
364}