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
pub use ServiceAdapter;
pub use Shared;
use delegate;
use crateMiddleware;
use crate;
/// Configure routes and define shared global state.
///
/// # Example
///
/// ```no_run
/// use std::process::ExitCode;
/// use via::{Error, Next, Request, Server, Shared};
///
/// /// A mock database pool.
/// #[derive(Debug)]
/// struct DatabasePool {
/// url: String,
/// }
///
/// /// Shared global state. Named after our application.
/// struct Unicorn {
/// pool: DatabasePool,
/// }
///
/// impl Unicorn {
/// fn pool(&self) -> &DatabasePool {
/// &self.pool
/// }
/// }
///
/// #[tokio::main]
/// async fn main() -> Result<ExitCode, Error> {
/// // Pass our shared state struct containing a database pool to the App
/// // constructor so it can be used to serve each request.
/// let mut app = via::app(Unicorn {
/// pool: DatabasePool {
/// url: std::env::var("DATABASE_URL")?,
/// },
/// });
///
/// // We can access our database in middleware with `request.app()`.
/// app.uses(async |request: Request<Unicorn>, next: Next<Unicorn>| {
/// // Print the debug output of our mock database pool to stdout.
/// println!("{:?}", request.app().pool());
///
/// // Delegate to the next middleware to get a response.
/// next.call(request).await
/// });
///
/// // Start serving our application from http://localhost:8080/.
/// Server::new(app).listen(("127.0.0.1", 8080)).await
/// }
/// ```
///
/// Create a new app with the provided state argument.
///
/// # Example
///
/// ```
/// # struct DatabasePool { url: String }
/// # struct Unicorn { pool: DatabasePool }
/// #
/// let mut app = via::app(Unicorn {
/// pool: DatabasePool {
/// url: "postgres://unicorn@localhost/unicorn".to_owned(),
/// },
/// });
/// ```
///