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
//! Pre-defined endpoints.
//!
//! This module defines a few endpoints that might be useful for a given HTTP
//! application. Their use should be as simple as this:
//!
//! ```rust
//! # use under::*;
//! # #[tokio::main] async fn main() -> Result<(), anyhow::Error> {
//! # let mut http = under::http();
//! http.at("/home").get(under::endpoints::simple(|| {
//! Response::text("hello, there!")
//! }));
//! # Ok(())
//! # }
//! ```
pub use ;
pub use SyncEndpoint;
use crateIntoResponse;
use crate::;
/// Creates an endpoint that synchronously generates a response.
///
/// This does not spawn a blocking task; so any endpoint that uses this should
/// not block the task in its processing. This is useful for endpoints that
/// quickly generate a response, or otherwise do not use futures.
///
/// # Examples
///
/// ```rust
/// # #[tokio::main] async fn main() -> Result<(), anyhow::Error> {
/// let mut http = under::http();
/// http.at("/404").get(under::endpoints::sync(|_| {
/// under::Response::json(&serde_json::json!({ "error": 404 }))
/// }));
/// # Ok(())
/// # }
/// ```
/// Creates an endpoint that synchronously, infallibly generates a response.
///
/// This is meant for a very basic operation that returns a specific response
/// regardless of the request. This is best paired with a
/// [`crate::Response::empty_404`]-like function.
///
/// This, like [`sync()`], does not spawn a blocking task; so any endpoint that
/// uses this should not block the task in its processing. This is useful for
/// endpoints that quickly generate a response, or otherwise do not use futures.
///
/// # Examples
///
/// ```rust
/// # #[tokio::main] async fn main() -> Result<(), anyhow::Error> {
/// let mut http = under::http();
/// http.at("/404").get(under::endpoints::simple(under::Response::empty_404));
/// # Ok(())
/// # }
/// ```
/// Creates an endpoint that serves files from the given directory.
///
/// The endpoint expects the path to use to be a part of the request fragment
/// from the route; i.e., the route must have a pattern in it, like
/// `/public/{:path}`. The name itself does not matter, as the endpoint
/// retrieves the first match. Thus, `/users/{id}/files/{:path}` will not work
/// with this endpoint. If there is a use case for this pattern, please file
/// a github ticket.
///
/// The endpoint will guess the Content-Type based off of the extension, or
/// default to `application/octet-stream` if it cannot be guessed.
///
/// If the router pattern is misconfigured, it will 404; if the file path
/// contains any segment consisting of `".."`, it will 404; if the file path
/// contains any backslashes, it will 404; f the requested file refers to
/// a directory, but does not contain a terminating slash, it will permanently
/// redirect to the URL with the terminating slash; if the requested file
/// refers to a directory (and contains a terminating slash), it will attempt to
/// read `index.html` in that directory instead; if it cannot find the file,
/// it will 404; if it cannot read the file, it will 500; and finally, it will
/// attempt to stream the file with a 200.
///
/// # Examples
///
/// ```rust
/// # #[tokio::main] async fn main() -> Result<(), anyhow::Error> {
/// # let mut http = under::http();
/// http.at("/public/{:path}").get(under::endpoints::dir("public/"));
/// # Ok(())
/// # }
/// ```
/// Creates a builder for a [`ScopeEndpoint`].
///
/// A [`ScopeEndpoint`] is an endpoint with attentional middleware in front
/// of it. This middleware acts just as if it was a part of the normal
/// middleware stack just in front of the endpoint, but the middleware in
/// the scope endpoint will always execute _after_ the middleware of whatever
/// router the endpoint is in front of.
///
/// This could be useful for (for example) restricting a subset of routes to
/// being authorization-restricted.
///
/// # Examples
/// ```rust,no_run
/// # use under::*;
/// # #[tokio::main] async fn main() -> Result<(), anyhow::Error> {
/// let mut http = under::http();
///
/// async fn endpoint(request: Request) -> Response {
/// let target = request.state::<String>().map(|v| v.as_str()).unwrap_or("world");
/// Response::text(format!("hello, {}", target))
/// }
///
/// http.at("/foo").get(endpoint);
/// http.at("/bar").get(under::endpoints::scope()
/// .with(under::middleware::StateMiddleware::new("bar".to_string()))
/// .then(endpoint));
/// http.prepare();
/// let mut response = http.handle(Request::get("/foo")?).await?;
/// let body = response.data(512).into_text().await?;
/// assert_eq!(body, "hello, world");
/// let mut response = http.handle(Request::get("/bar")?).await?;
/// let body = response.data(512).into_text().await?;
/// assert_eq!(body, "hello, bar");
/// # Ok(())
/// # }
/// ```