Examples readme (#25)

* Examples readme

* link?
This commit is contained in:
David Pedersen 2021-06-19 14:06:49 +02:00 committed by GitHub
parent 44577a0108
commit 8d8cc3ba3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 30 deletions

12
examples/README.md Normal file
View file

@ -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 `<form>`.
- [`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.

View file

@ -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)

View file

@ -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));

View file

@ -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));

View file

@ -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("<h1>Hello, World!</h1>")
}
async fn greet(params: extract::UrlParamsMap) -> Result<String, StatusCode> {
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)
}
}

View file

@ -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));

View file

@ -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));