@@ -37,105 +37,104 @@ pub use route::Route;
37
37
/// - Middleware extends the base Tide framework with additional request or
38
38
/// response processing, such as compression, default headers, or logging. To
39
39
/// add middleware to an app, use the [`Server::middleware`] method.
40
- ///
41
- /// # Hello, world!
42
- ///
43
- /// You can start a simple Tide application that listens for `GET` requests at path `/hello`
44
- /// on `127.0.0.1:8000` with:
45
- ///
46
- /// ```rust, no_run
47
- ///
48
- /// let mut app = tide::Server::new();
49
- /// app.at("/hello").get(|_| async move {"Hello, world!"});
50
- /// app.run("127.0.0.1:8000").unwrap();
51
- /// ```
52
- ///
53
- /// # Routing and parameters
54
- ///
55
- /// Tide's routing system is simple and similar to many other frameworks. It
56
- /// uses `:foo` for "wildcard" URL segments, and `*foo` to match the rest of a
57
- /// URL (which may include multiple segments). Here's an example using wildcard
58
- /// segments as parameters to endpoints:
59
- ///
60
- /// ```rust, no_run
61
- /// use tide::error::ResultExt;
62
- ///
63
- /// async fn hello(cx: tide::Request<()>) -> tide::Result<String> {
64
- /// let user: String = cx.param("user").client_err()?;
65
- /// Ok(format!("Hello, {}!", user))
66
- /// }
67
- ///
68
- /// async fn goodbye(cx: tide::Request<()>) -> tide::Result<String> {
69
- /// let user: String = cx.param("user").client_err()?;
70
- /// Ok(format!("Goodbye, {}.", user))
71
- /// }
72
- ///
73
- /// let mut app = tide::Server::new();
74
- ///
75
- /// app.at("/hello/:user").get(hello);
76
- /// app.at("/goodbye/:user").get(goodbye);
77
- /// app.at("/").get(|_| async move {
78
- /// "Use /hello/{your name} or /goodbye/{your name}"
79
- /// });
80
- ///
81
- /// app.run("127.0.0.1:8000").unwrap();
82
- /// ```
83
- ///
84
- /// You can learn more about routing in the [`Server::at`] documentation.
85
- ///
86
- /// # Serverlication state
87
- ///
88
- /// ```rust, no_run
89
- ///
90
- /// use http::status::StatusCode;
91
- /// use serde::{Deserialize, Serialize};
92
- /// use std::sync::Mutex;
93
- /// use tide::{error::ResultExt, response, Server, Request, Result};
94
- ///
95
- /// #[derive(Default)]
96
- /// struct Database {
97
- /// contents: Mutex<Vec<Message>>,
98
- /// }
99
- ///
100
- /// #[derive(Serialize, Deserialize, Clone)]
101
- /// struct Message {
102
- /// author: Option<String>,
103
- /// contents: String,
104
- /// }
105
- ///
106
- /// impl Database {
107
- /// fn insert(&self, msg: Message) -> usize {
108
- /// let mut table = self.contents.lock().unwrap();
109
- /// table.push(msg);
110
- /// table.len() - 1
111
- /// }
112
- ///
113
- /// fn get(&self, id: usize) -> Option<Message> {
114
- /// self.contents.lock().unwrap().get(id).cloned()
115
- /// }
116
- /// }
117
- ///
118
- /// async fn new_message(mut cx: Request<Database>) -> Result<String> {
119
- /// let msg = cx.body_json().await.client_err()?;
120
- /// Ok(cx.state().insert(msg).to_string())
121
- /// }
122
- ///
123
- /// async fn get_message(cx: Request<Database>) -> Result {
124
- /// let id = cx.param("id").client_err()?;
125
- /// if let Some(msg) = cx.state().get(id) {
126
- /// Ok(response::json(msg))
127
- /// } else {
128
- /// Err(StatusCode::NOT_FOUND)?
129
- /// }
130
- /// }
131
- ///
132
- /// fn main() {
133
- /// let mut app = Server::with_state(Database::default());
134
- /// app.at("/message").post(new_message);
135
- /// app.at("/message/:id").get(get_message);
136
- /// app.run("127.0.0.1:8000").unwrap();
137
- /// }
138
- /// ```
40
+ /////
41
+ ///// # Hello, world!
42
+ /////
43
+ ///// You can start a simple Tide application that listens for `GET` requests at path `/hello`
44
+ ///// on `127.0.0.1:8000` with:
45
+ /////
46
+ ///// ```rust, no_run
47
+ /////
48
+ ///// let mut app = tide::Server::new();
49
+ ///// app.at("/hello").get(|_| async move {"Hello, world!"});
50
+ ///// // app.run("127.0.0.1:8000").unwrap();
51
+ ///// ```
52
+ /////
53
+ ///// # Routing and parameters
54
+ /////
55
+ ///// Tide's routing system is simple and similar to many other frameworks. It
56
+ ///// uses `:foo` for "wildcard" URL segments, and `*foo` to match the rest of a
57
+ ///// URL (which may include multiple segments). Here's an example using wildcard
58
+ ///// segments as parameters to endpoints:
59
+ /////
60
+ ///// ```no_run
61
+ ///// use tide::error::ResultExt;
62
+ /////
63
+ ///// async fn hello(cx: tide::Request<()>) -> tide::Result<String> {
64
+ ///// let user: String = cx.param("user")?;
65
+ ///// Ok(format!("Hello, {}!", user))
66
+ ///// }
67
+ /////
68
+ ///// async fn goodbye(cx: tide::Request<()>) -> tide::Result<String> {
69
+ ///// let user: String = cx.param("user")?;
70
+ ///// Ok(format!("Goodbye, {}.", user))
71
+ ///// }
72
+ /////
73
+ ///// let mut app = tide::Server::new();
74
+ /////
75
+ ///// app.at("/hello/:user").get(hello);
76
+ ///// app.at("/goodbye/:user").get(goodbye);
77
+ ///// app.at("/").get(|_| async move {
78
+ ///// "Use /hello/{your name} or /goodbye/{your name}"
79
+ ///// });
80
+ /////
81
+ ///// // app.run("127.0.0.1:8000").unwrap();
82
+ ///// ```
83
+ /////
84
+ ///// You can learn more about routing in the [`Server::at`] documentation.
85
+ /////
86
+ ///// # Serverlication state
87
+ /////
88
+ ///// ```rust,no_run
89
+ ///// use http::status::StatusCode;
90
+ ///// use serde::{Deserialize, Serialize};
91
+ ///// use std::sync::Mutex;
92
+ ///// use tide::{error::ResultExt, Server, Request, Result};
93
+ /////
94
+ ///// #[derive(Default)]
95
+ ///// struct Database {
96
+ ///// contents: Mutex<Vec<Message>>,
97
+ ///// }
98
+ /////
99
+ ///// #[derive(Serialize, Deserialize, Clone)]
100
+ ///// struct Message {
101
+ ///// author: Option<String>,
102
+ ///// contents: String,
103
+ ///// }
104
+ /////
105
+ ///// impl Database {
106
+ ///// fn insert(&self, msg: Message) -> usize {
107
+ ///// let mut table = self.contents.lock().unwrap();
108
+ ///// table.push(msg);
109
+ ///// table.len() - 1
110
+ ///// }
111
+ /////
112
+ ///// fn get(&self, id: usize) -> Option<Message> {
113
+ ///// self.contents.lock().unwrap().get(id).cloned()
114
+ ///// }
115
+ ///// }
116
+ /////
117
+ ///// async fn new_message(mut cx: Request<Database>) -> Result<String> {
118
+ ///// let msg = cx.body_json().await?;
119
+ ///// Ok(cx.state().insert(msg).to_string())
120
+ ///// }
121
+ /////
122
+ ///// async fn get_message(cx: Request<Database>) -> Result {
123
+ ///// let id = cx.param("id").unwrap();
124
+ ///// if let Some(msg) = cx.state().get(id) {
125
+ ///// Ok(response::json(msg))
126
+ ///// } else {
127
+ ///// Err(StatusCode::NOT_FOUND)?
128
+ ///// }
129
+ ///// }
130
+ /////
131
+ ///// fn main() {
132
+ ///// let mut app = Server::with_state(Database::default());
133
+ ///// app.at("/message").post(new_message);
134
+ ///// app.at("/message/:id").get(get_message);
135
+ ///// // app.run("127.0.0.1:8000").unwrap();
136
+ ///// }
137
+ ///// ```
139
138
#[ allow( missing_debug_implementations) ]
140
139
pub struct Server < State > {
141
140
router : Router < State > ,
0 commit comments