diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..e40b8e94 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,12 @@ +# Examples + +- [`hello_world`](../examples/hello_world.rs) - Very small getting started app. +- [`key_value_store`](../examples/key_value_store.rs) - Slightly larger app with an in-memory key/value store. +- [`form`](../examples/form.rs) - Receiving data from an HTML `
`. +- [`static_file_server`](../examples/static_file_server.rs) - Serving static files from a directory. Could for example be the baseline for a single page app. +- [`templates`](../examples/templates.rs) - Rending HTML templates using [askama](https://crates.io/crates/askama). +- [`testing`](../examples/testing.rs) - How to test awebframework apps. +- [`versioning`](../examples/versioning.rs) - How one might version an API. +- [`websocket`](../examples/websocket.rs) - How to build an app that handles WebSocket connections. +- [`error_handling_and_dependency_injection`](../examples/error_handling_and_dependency_injection.rs) - How to handle errors and dependency injection using trait objects. +- [`bb8_connection_pool`](../examples/bb8_connection_pool.rs) - How to use a bb8 and tokio-postgres to query a database. diff --git a/examples/bb8_connection_pool.rs b/examples/bb8_connection_pool.rs index be40c8af..2d0fc9d9 100644 --- a/examples/bb8_connection_pool.rs +++ b/examples/bb8_connection_pool.rs @@ -7,8 +7,6 @@ use tokio_postgres::NoTls; #[tokio::main] async fn main() { - tracing_subscriber::fmt::init(); - // setup connection pool let manager = PostgresConnectionManager::new_from_stringlike("host=localhost user=postgres", NoTls) diff --git a/examples/error_handling_and_dependency_injection.rs b/examples/error_handling_and_dependency_injection.rs index 64d918c8..d857cba0 100644 --- a/examples/error_handling_and_dependency_injection.rs +++ b/examples/error_handling_and_dependency_injection.rs @@ -14,13 +14,10 @@ use http::StatusCode; use serde::{Deserialize, Serialize}; use serde_json::json; use std::{net::SocketAddr, sync::Arc}; -use tower_http::trace::TraceLayer; use uuid::Uuid; #[tokio::main] async fn main() { - tracing_subscriber::fmt::init(); - // Inject a `UserRepo` into our handlers via a trait object. This could be // the live implementation or just a mock for testing. let user_repo = Arc::new(ExampleUserRepo) as DynUserRepo; @@ -30,9 +27,7 @@ async fn main() { .route("/users", post(users_create)) // Add our `user_repo` to all request's extensions so handlers can access // it. - .layer(AddExtensionLayer::new(user_repo)) - // Add tracing because why not. - .layer(TraceLayer::new_for_http()); + .layer(AddExtensionLayer::new(user_repo)); // Run our application let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); diff --git a/examples/form.rs b/examples/form.rs index c9755cc2..ff3bf799 100644 --- a/examples/form.rs +++ b/examples/form.rs @@ -4,11 +4,8 @@ use std::net::SocketAddr; #[tokio::main] async fn main() { - tracing_subscriber::fmt::init(); - // build our application with some routes - let app = route("/", get(show_form).post(accept_form)) - .layer(tower_http::trace::TraceLayer::new_for_http()); + let app = route("/", get(show_form).post(accept_form)); // run it with hyper let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 07f9a13d..e2f26da0 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -1,13 +1,10 @@ use awebframework::prelude::*; -use http::StatusCode; use std::net::SocketAddr; #[tokio::main] async fn main() { - tracing_subscriber::fmt::init(); - - // build our application with some routes - let app = route("/", get(handler)).route("/greet/:name", get(greet)); + // build our application with a route + let app = route("/", get(handler)); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); @@ -21,12 +18,3 @@ async fn main() { async fn handler() -> response::Html<&'static str> { response::Html("

Hello, World!

") } - -async fn greet(params: extract::UrlParamsMap) -> Result { - if let Some(name) = params.get("name") { - Ok(format!("Hello {}!", name)) - } else { - // if the route matches "name" will be present - Err(StatusCode::INTERNAL_SERVER_ERROR) - } -} diff --git a/examples/templates.rs b/examples/templates.rs index 99a2ce54..900c2d12 100644 --- a/examples/templates.rs +++ b/examples/templates.rs @@ -5,8 +5,6 @@ use std::net::SocketAddr; #[tokio::main] async fn main() { - tracing_subscriber::fmt::init(); - // build our application with some routes let app = route("/greet/:name", get(greet)); diff --git a/examples/versioning.rs b/examples/versioning.rs index 208926b7..64fb6d34 100644 --- a/examples/versioning.rs +++ b/examples/versioning.rs @@ -6,8 +6,6 @@ use std::net::SocketAddr; #[tokio::main] async fn main() { - tracing_subscriber::fmt::init(); - // build our application with some routes let app = route("/:version/foo", get(handler));