Skip to content

Commit

Permalink
Rename serve to run, add asynchronous serve
Browse files Browse the repository at this point in the history
  • Loading branch information
tirr-c committed May 15, 2019
1 parent be577ef commit f4473d6
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Ecosystem WG, and **not ready for production use yet**.
fn main() -> Result<(), std::io::Error> {
let mut app = tide::App::new();
app.at("/").get(async move |_| "Hello, world!");
Ok(app.serve("127.0.0.1:8000")?)
Ok(app.run("127.0.0.1:8000")?)
}
```

Expand Down
2 changes: 1 addition & 1 deletion examples/src/body_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ pub fn main() {
app.at("/echo/json").post(echo_json);
app.at("/echo/form").post(echo_form);

app.serve("127.0.0.1:8000").unwrap();
app.run("127.0.0.1:8000").unwrap();
}
2 changes: 1 addition & 1 deletion examples/src/catch_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ async fn echo_path(cx: Context<()>) -> String {
pub fn main() {
let mut app = tide::App::new();
app.at("/echo_path/*path").get(echo_path);
app.serve("127.0.0.1:8000").unwrap();
app.run("127.0.0.1:8000").unwrap();
}
2 changes: 1 addition & 1 deletion examples/src/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ pub fn main() {
app.at("/").get(retrieve_cookie);
app.at("/set").get(set_cookie);
app.at("/remove").get(remove_cookie);
app.serve("127.0.0.1:8000").unwrap();
app.run("127.0.0.1:8000").unwrap();
}
2 changes: 1 addition & 1 deletion examples/src/default_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ pub fn main() {

app.at("/").get(async move |_| "Hello, world!");

app.serve("127.0.0.1:8000").unwrap();
app.run("127.0.0.1:8000").unwrap();
}
2 changes: 1 addition & 1 deletion examples/src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ async fn handle_graphql(mut cx: Context<Data>) -> EndpointResult {
pub fn main() {
let mut app = App::with_state(Data::default());
app.at("/graphql").post(handle_graphql);
app.serve("127.0.0.1:8000").unwrap();
app.run("127.0.0.1:8000").unwrap();
}
2 changes: 1 addition & 1 deletion examples/src/hello.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub fn main() {
let mut app = tide::App::new();
app.at("/").get(async move |_| "Hello, world!");
app.serve("127.0.0.1:8000").unwrap();
app.run("127.0.0.1:8000").unwrap();
}
2 changes: 1 addition & 1 deletion examples/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ pub fn main() {
let mut app = App::with_state(Database::default());
app.at("/message").post(new_message);
app.at("/message/:id").get(get_message).post(set_message);
app.serve("127.0.0.1:8000").unwrap();
app.run("127.0.0.1:8000").unwrap();
}
2 changes: 1 addition & 1 deletion examples/src/multipart_form/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async fn upload_file(mut cx: Context<()>) -> EndpointResult {
pub fn run() {
let mut app = App::new();
app.at("/upload_file").post(upload_file);
app.serve("127.0.0.1:8000").unwrap();
app.run("127.0.0.1:8000").unwrap();
}

// Test with:
Expand Down
2 changes: 1 addition & 1 deletion examples/src/staticfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,5 @@ async fn handle_path(ctx: Context<StaticFile>) -> EndpointResult {
pub fn main() {
let mut app = App::with_state(StaticFile::new("./"));
app.at("/*").get(handle_path);
app.serve("127.0.0.1:8000").unwrap();
app.run("127.0.0.1:8000").unwrap();
}
23 changes: 18 additions & 5 deletions tide/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{
///
/// let mut app = tide::App::new();
/// app.at("/hello").get(async move |_| "Hello, world!");
/// app.serve("127.0.0.1:8000");
/// app.run("127.0.0.1:8000");
/// ```
///
/// # Routing and parameters
Expand Down Expand Up @@ -67,7 +67,7 @@ use crate::{
/// "Use /hello/{your name} or /goodbye/{your name}"
/// });
///
/// app.serve("127.0.0.1:8000");
/// app.run("127.0.0.1:8000");
/// ```
///
/// You can learn more about routing in the [`App::at`] documentation.
Expand Down Expand Up @@ -123,7 +123,7 @@ use crate::{
/// let mut app = App::with_state(Database::default());
/// app.at("/message").post(new_message);
/// app.at("/message/:id").get(get_message);
/// app.serve("127.0.0.1:8000").unwrap();
/// app.run("127.0.0.1:8000").unwrap();
/// }
/// ```

Expand Down Expand Up @@ -232,11 +232,11 @@ impl<State: Send + Sync + 'static> App<State> {
}
}

/// Start serving the app at the given address.
/// Run the app at the given address.
///
/// Blocks the calling thread indefinitely.
#[cfg(feature = "hyper")]
pub fn serve(self, addr: impl std::net::ToSocketAddrs) -> std::io::Result<()> {
pub fn run(self, addr: impl std::net::ToSocketAddrs) -> std::io::Result<()> {
let addr = addr
.to_socket_addrs()?
.next()
Expand All @@ -246,6 +246,19 @@ impl<State: Send + Sync + 'static> App<State> {
http_service_hyper::run(self.into_http_service(), addr);
Ok(())
}

/// Asynchronously serve the app at the given address.
#[cfg(feature = "hyper")]
pub async fn serve(self, addr: impl std::net::ToSocketAddrs) -> std::io::Result<()> {
let addr = addr
.to_socket_addrs()?
.next()
.ok_or(std::io::ErrorKind::InvalidInput)?;

// TODO: propagate the error from hyper
http_service_hyper::serve(self.into_http_service(), addr).await.ok();
Ok(())
}
}

/// An instantiated Tide server.
Expand Down
4 changes: 2 additions & 2 deletions tide/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{response::IntoResponse, Context, Response};
/// fn main() {
/// let mut app = tide::App::new();
/// app.at("/hello").get(hello);
/// app.serve("127.0.0.1:8000").unwrap()
/// app.run("127.0.0.1:8000").unwrap()
/// }
/// ```
///
Expand All @@ -43,7 +43,7 @@ use crate::{response::IntoResponse, Context, Response};
/// fn main() {
/// let mut app = tide::App::new();
/// app.at("/hello").get(hello);
/// app.serve("127.0.0.1:8000").unwrap()
/// app.run("127.0.0.1:8000").unwrap()
/// }
/// ```
///
Expand Down

0 comments on commit f4473d6

Please sign in to comment.