maw 0.26.4

A simple and efficient web framework for Rust.
Documentation
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
use std::{
    collections::HashSet,
    net,
    sync::{Arc, RwLock},
};

use http::StatusCode;
use hyper::{Request as HyperRequest, body::Incoming as IncomingBody};
use hyper_util::rt::{TokioExecutor, TokioIo};
use smol_str::SmolStr;
use tokio::net::TcpListener;
use tokio_util::sync::CancellationToken;

#[cfg(feature = "minijinja")]
mod jinja;
#[cfg(feature = "minijinja")]
pub use jinja::Jinja;

use crate::{
    ALL,
    any_map::{AnyMap, SerializableAny},
    error::Error,
    request::Request,
    response::{HttpBody, Response},
    router::{self, MatchRouter},
};

type HttpResponse = http::Response<HttpBody>;

pub struct App<S = ()> {
    pub state: Arc<S>,
    pub(crate) router: router::Router,
    #[cfg(feature = "minijinja")]
    pub jinja: Jinja,
    pub(crate) locals: RwLock<AnyMap<dyn SerializableAny>>,
    pub(crate) built_router: MatchRouter,
    pub(crate) shutdown: CancellationToken,
    pub(crate) shutdown_timeout: std::time::Duration,
    dump_routes: bool,
    /// Max body size that the server accepts.
    ///
    /// Default: 4MB
    pub(crate) body_limit: usize,
    /// ProxyHeader will enable c.req.ip() to return the value of the given header key
    /// By default c.req.ip() will return the Remote IP from the TCP connection
    /// This property can be useful if you are behind a load balancer: X-Forwarded-*
    /// NOTE: headers are easily spoofed and the detected IP addresses are unreliable.
    ///
    /// Default: None
    pub(crate) proxy_header: Option<String>,
}

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

impl App {
    pub fn new() -> Self {
        App {
            state: Arc::new(()),
            router: router::Router::new(),
            #[cfg(feature = "minijinja")]
            jinja: Jinja::default(),
            locals: RwLock::new(AnyMap::new()),
            built_router: MatchRouter::default(),
            shutdown: CancellationToken::new(),
            shutdown_timeout: std::time::Duration::from_secs(10),
            dump_routes: false,
            body_limit: 4 * 1024 * 1024,
            proxy_header: None,
        }
    }

    pub fn with_state<S>(self, state: S) -> App<S> {
        App {
            state: Arc::new(state),
            router: self.router,
            #[cfg(feature = "minijinja")]
            jinja: self.jinja,
            locals: self.locals,
            built_router: self.built_router,
            shutdown: self.shutdown,
            shutdown_timeout: self.shutdown_timeout,
            dump_routes: self.dump_routes,
            body_limit: self.body_limit,
            proxy_header: self.proxy_header,
        }
    }

    /// Set the maximum body size that the server accepts
    ///
    /// Default: 4MB
    pub fn body_limit(mut self, limit: usize) -> Self {
        self.body_limit = limit;
        self
    }

    /// ProxyHeader will enable c.req.ip() to return the value of the given header key
    /// By default c.req.ip() will return the Remote IP from the TCP connection
    /// This property can be useful if you are behind a load balancer: X-Forwarded-*
    /// NOTE: headers are easily spoofed and the detected IP addresses are unreliable.
    ///
    /// Default: None (disabled)
    pub fn proxy_header(mut self, header: impl Into<String>) -> Self {
        self.proxy_header = Some(header.into());
        self
    }

    /// Sets the router for the application.
    ///
    /// Changes to the router after the server has started will not take effect.
    pub fn router(mut self, router: router::Router) -> Self {
        self.router = router;
        self
    }

    /// Logs the complete route table at startup for debugging.
    ///
    /// When enabled, prints all registered routes with their HTTP methods,
    /// paths, and handler chains. Useful for verifying route configuration
    /// and debugging routing issues during development.
    pub fn dump_routes(mut self, enable: bool) -> Self {
        self.dump_routes = enable;
        self
    }

    /// Sets the graceful shutdown timeout duration.
    ///
    /// This is how long the server will wait for existing connections to close
    /// before forcing shutdown. Default is 10 seconds.
    pub fn shutdown_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.shutdown_timeout = timeout;
        self
    }

    /// Returns a clone of the server shutdown token.
    ///
    /// Long-lived tasks like SSE streams can use this to stop immediately
    /// when application shutdown is triggered.
    pub fn shutdown_token(&self) -> CancellationToken {
        self.shutdown.clone()
    }

    /// Provides access to the application locals.
    pub fn locals<F>(&self, f: F) -> &Self
    where
        F: FnOnce(&AnyMap<dyn SerializableAny>),
    {
        let locals = self.locals.read().unwrap();
        f(&locals);
        self
    }

    /// Provides mutable access to the application locals.
    pub fn locals_mut<F>(&self, f: F) -> &Self
    where
        F: FnOnce(&mut AnyMap<dyn SerializableAny>),
    {
        let mut locals = self.locals.write().unwrap();
        f(&mut locals);
        self
    }

    /// Sets application locals.
    pub fn with_locals(self, f: impl FnOnce(&mut AnyMap<dyn SerializableAny>)) -> Self {
        self.locals_mut(f);
        self
    }

    /// set views path
    #[cfg(feature = "minijinja")]
    pub fn views(mut self, path: impl AsRef<std::path::Path>) -> Self {
        self.jinja = Jinja::new(path);
        self
    }

    #[cfg(feature = "minijinja")]
    pub fn views_with(
        mut self,
        path: impl AsRef<std::path::Path>,
        f: impl FnOnce(&mut minijinja::Environment<'static>),
    ) -> Self {
        self.jinja = Jinja::new(path);
        self.jinja.with(f);
        self
    }
}

impl App {
    /// Listen with ctrl+c shutdown
    pub async fn listen<A>(self, addr: A) -> Result<(), Error>
    where
        A: net::ToSocketAddrs + std::fmt::Debug + 'static,
    {
        let token = CancellationToken::new();
        let t = token.clone();
        tokio::spawn(async move {
            tokio::signal::ctrl_c()
                .await
                .expect("failed to install CTRL+C signal handler");
            t.cancel();
        });
        self.listen_shutdown(addr, token).await
    }

    /// Listen with custom shutdown signal
    pub async fn listen_shutdown<A>(
        mut self,
        addr: A,
        shutdown: CancellationToken,
    ) -> Result<(), Error>
    where
        A: net::ToSocketAddrs + std::fmt::Debug + 'static,
    {
        if self.dump_routes {
            tracing::info!("App Router: {:#?}", self.router);
        }

        self.built_router = self.router.build()?;

        self.shutdown = shutdown.clone();

        let middlewares: Vec<_> = {
            let mut called = HashSet::new();
            self.router
                .flatten_routers()
                .iter()
                .flat_map(|(_, m)| m.values())
                .flat_map(|h| h.iter())
                .filter(|h| called.insert(h.type_id()))
                .cloned()
                .collect()
        };

        for h in &middlewares {
            h.on_app_listen_mut(&mut self);
        }

        let arc_app = Arc::new(self);
        for h in &middlewares {
            h.on_app_listen_arc(&arc_app);
        }

        let addr = addr
            .to_socket_addrs()?
            .next()
            .ok_or(Error::FailedToParseAddr)?;

        let listener = {
            #[cfg(feature = "listenfd")]
            {
                let mut listenfd = listenfd::ListenFd::from_env();
                if let Ok(Some(std_listener)) = listenfd.take_tcp_listener(0) {
                    std_listener.set_nonblocking(true)?;
                    TcpListener::from_std(std_listener)?
                } else {
                    TcpListener::bind(addr).await?
                }
            }
            #[cfg(not(feature = "listenfd"))]
            {
                TcpListener::bind(addr).await?
            }
        };
        tracing::info!("Http app listening on http://{}", addr);

        let server = hyper_util::server::conn::auto::Builder::new(TokioExecutor::new());
        let graceful = hyper_util::server::graceful::GracefulShutdown::new();

        let _ = shutdown
            .run_until_cancelled(async {
                loop {
                    let Ok((stream, peer_addr)) = listener.accept().await else {
                        continue;
                    };
                    let io = TokioIo::new(stream);
                    let app = arc_app.clone();
                    let service = hyper::service::service_fn(move |req| {
                        handle_request(req, app.clone(), peer_addr)
                    });

                    let conn = server.serve_connection_with_upgrades(io, service);
                    let fut = graceful.watch(conn.into_owned());
                    tokio::spawn(async move {
                        if let Err(e) = fut.await {
                            tracing::trace!("connection failed: {e:?}");
                        }
                    });
                }
            })
            .await;

        tracing::info!("Shutdown signal received!");

        tracing::info!(
            "Waiting for connections to close (timeout: {:?})...",
            arc_app.shutdown_timeout
        );

        match tokio::time::timeout(arc_app.shutdown_timeout, graceful.shutdown()).await {
            Ok(_) => tracing::info!("All connections closed!"),
            Err(_) => tracing::info!("Shutdown timed out!"),
        }

        Ok(())
    }
}

impl Clone for App {
    fn clone(&self) -> Self {
        App {
            state: self.state.clone(),
            router: self.router.clone(),
            #[cfg(feature = "minijinja")]
            jinja: self.jinja.clone(),
            locals: RwLock::new(self.locals.read().unwrap().clone()),
            built_router: MatchRouter::default(),
            shutdown: self.shutdown.clone(),
            shutdown_timeout: self.shutdown_timeout,
            dump_routes: self.dump_routes,
            body_limit: self.body_limit,
            proxy_header: self.proxy_header.clone(),
        }
    }
}

async fn handle_request(
    request: HyperRequest<IncomingBody>,
    app: Arc<App>,
    peer_addr: net::SocketAddr,
) -> Result<HttpResponse, NoResponse> {
    let mut response = HttpResponse::new(HttpBody::default());

    let path = normalize_path(request.uri().path());
    let matched_route = match app.built_router.at(&path) {
        Ok(matched_route) => matched_route,
        Err(_) => {
            tracing::debug!("requested path not found: {path}");
            *response.status_mut() = StatusCode::NOT_FOUND;
            return Ok(response);
        }
    };

    let handlers = matched_route.value;
    let handlers = handlers
        .get(request.method())
        .or_else(|| {
            (request.method() == http::Method::HEAD)
                .then(|| handlers.get(&http::Method::GET))
                .flatten()
        })
        .or_else(|| handlers.get(&ALL));
    let Some(handlers) = handlers else {
        tracing::debug!(
            "requested method not allowed: {} {}",
            request.method(),
            path
        );
        *response.status_mut() = StatusCode::METHOD_NOT_ALLOWED;
        return Ok(response);
    };
    let handlers = handlers.clone();

    let params = matched_route
        .params
        .iter()
        .map(|(k, v)| (SmolStr::new(k), SmolStr::new(v)))
        .collect();

    let req = Request::new(app.clone(), request, params, peer_addr);
    let res = Response::from_response(app, response);

    let mut c = crate::ctx::Ctx::new(req, res, handlers);
    c.next().await;

    if c.is_closed() {
        return Err(NoResponse);
    }

    if c.req.method() == http::Method::HEAD {
        *c.res.inner.body_mut() = HttpBody::default();
    }

    Ok(c.res.inner)
}

fn normalize_path(s: &str) -> std::borrow::Cow<'_, str> {
    let mut result = None;

    for (i, ch) in s.char_indices() {
        if ch == '\\' {
            let mut owned = result.take().unwrap_or_else(|| {
                let mut buf = String::with_capacity(s.len());
                buf.push_str(&s[..i]);
                buf
            });
            owned.push('/');
            result = Some(owned);
        } else if let Some(ref mut owned) = result {
            owned.push(ch);
        }
    }

    match result {
        None => {
            if s.len() > 1 && s.ends_with('/') {
                std::borrow::Cow::Borrowed(&s[..s.len() - 1])
            } else {
                std::borrow::Cow::Borrowed(s)
            }
        }

        Some(mut owned) => {
            if owned.len() > 1 && owned.ends_with('/') {
                owned.pop();
            }
            std::borrow::Cow::Owned(owned)
        }
    }
}

#[derive(Debug)]
struct NoResponse;

impl std::fmt::Display for NoResponse {
    fn fmt(&self, _: &mut std::fmt::Formatter) -> std::fmt::Result {
        Ok(())
    }
}

impl std::error::Error for NoResponse {}