diff --git a/examples/hello-world/src/main.rs b/examples/hello-world/src/main.rs index a42cd56f..466caceb 100644 --- a/examples/hello-world/src/main.rs +++ b/examples/hello-world/src/main.rs @@ -4,58 +4,23 @@ //! cargo run -p example-hello-world //! ``` +use axum::{response::Html, routing::get, Router}; +use std::net::SocketAddr; + #[tokio::main] async fn main() { - use axum::async_trait; - use axum::http::StatusCode; - use axum::{ - extract::{extractor_middleware, FromRequest, RequestParts}, - routing::{get, post}, - Router, - }; + // build our application with a route + let app = Router::new().route("/", get(handler)); - // An extractor that performs authorization. - struct RequireAuth; - - #[async_trait] - impl FromRequest for RequireAuth - where - B: Send, - { - type Rejection = StatusCode; - - async fn from_request(req: &mut RequestParts) -> Result { - let auth_header = req - .headers() - .and_then(|headers| headers.get(axum::http::header::AUTHORIZATION)) - .and_then(|value| value.to_str().ok()); - - if let Some(value) = auth_header { - if value == "secret" { - return Ok(Self); - } - } - - Err(StatusCode::UNAUTHORIZED) - } - } - - async fn handler() { - // If we get here the request has been authorized - } - - async fn other_handler() { - // If we get here the request has been authorized - } - - let app = Router::new() - .route("/", get(handler)) - .route("/foo", post(other_handler)) - // The extractor will run before all routes - .layer(extractor_middleware::()); - - axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) + // run it + let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + println!("listening on {}", addr); + axum::Server::bind(&addr) .serve(app.into_make_service()) .await .unwrap(); } + +async fn handler() -> Html<&'static str> { + Html("

Hello, World!

") +}