mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-06 00:00:17 +02:00
@@ -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.
|
||||||
@@ -7,8 +7,6 @@ use tokio_postgres::NoTls;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
tracing_subscriber::fmt::init();
|
|
||||||
|
|
||||||
// setup connection pool
|
// setup connection pool
|
||||||
let manager =
|
let manager =
|
||||||
PostgresConnectionManager::new_from_stringlike("host=localhost user=postgres", NoTls)
|
PostgresConnectionManager::new_from_stringlike("host=localhost user=postgres", NoTls)
|
||||||
|
|||||||
@@ -14,13 +14,10 @@ use http::StatusCode;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::{net::SocketAddr, sync::Arc};
|
use std::{net::SocketAddr, sync::Arc};
|
||||||
use tower_http::trace::TraceLayer;
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
tracing_subscriber::fmt::init();
|
|
||||||
|
|
||||||
// Inject a `UserRepo` into our handlers via a trait object. This could be
|
// Inject a `UserRepo` into our handlers via a trait object. This could be
|
||||||
// the live implementation or just a mock for testing.
|
// the live implementation or just a mock for testing.
|
||||||
let user_repo = Arc::new(ExampleUserRepo) as DynUserRepo;
|
let user_repo = Arc::new(ExampleUserRepo) as DynUserRepo;
|
||||||
@@ -30,9 +27,7 @@ async fn main() {
|
|||||||
.route("/users", post(users_create))
|
.route("/users", post(users_create))
|
||||||
// Add our `user_repo` to all request's extensions so handlers can access
|
// Add our `user_repo` to all request's extensions so handlers can access
|
||||||
// it.
|
// it.
|
||||||
.layer(AddExtensionLayer::new(user_repo))
|
.layer(AddExtensionLayer::new(user_repo));
|
||||||
// Add tracing because why not.
|
|
||||||
.layer(TraceLayer::new_for_http());
|
|
||||||
|
|
||||||
// Run our application
|
// Run our application
|
||||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||||
|
|||||||
+1
-4
@@ -4,11 +4,8 @@ use std::net::SocketAddr;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
tracing_subscriber::fmt::init();
|
|
||||||
|
|
||||||
// build our application with some routes
|
// build our application with some routes
|
||||||
let app = route("/", get(show_form).post(accept_form))
|
let app = route("/", get(show_form).post(accept_form));
|
||||||
.layer(tower_http::trace::TraceLayer::new_for_http());
|
|
||||||
|
|
||||||
// run it with hyper
|
// run it with hyper
|
||||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||||
|
|||||||
+2
-14
@@ -1,13 +1,10 @@
|
|||||||
use awebframework::prelude::*;
|
use awebframework::prelude::*;
|
||||||
use http::StatusCode;
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
tracing_subscriber::fmt::init();
|
// build our application with a route
|
||||||
|
let app = route("/", get(handler));
|
||||||
// build our application with some routes
|
|
||||||
let app = route("/", get(handler)).route("/greet/:name", get(greet));
|
|
||||||
|
|
||||||
// run it
|
// run it
|
||||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||||
@@ -21,12 +18,3 @@ async fn main() {
|
|||||||
async fn handler() -> response::Html<&'static str> {
|
async fn handler() -> response::Html<&'static str> {
|
||||||
response::Html("<h1>Hello, World!</h1>")
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ use std::net::SocketAddr;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
tracing_subscriber::fmt::init();
|
|
||||||
|
|
||||||
// build our application with some routes
|
// build our application with some routes
|
||||||
let app = route("/greet/:name", get(greet));
|
let app = route("/greet/:name", get(greet));
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ use std::net::SocketAddr;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
tracing_subscriber::fmt::init();
|
|
||||||
|
|
||||||
// build our application with some routes
|
// build our application with some routes
|
||||||
let app = route("/:version/foo", get(handler));
|
let app = route("/:version/foo", get(handler));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user