2021-08-10 10:03:29 +02:00
|
|
|
//! Run with
|
|
|
|
//!
|
|
|
|
//! ```not_rust
|
|
|
|
//! cargo run --example hello_world
|
|
|
|
//! ```
|
|
|
|
|
2021-08-18 00:04:15 +02:00
|
|
|
use axum::{handler::get, response::Html, route, routing::RoutingDsl};
|
2021-08-10 10:03:29 +02:00
|
|
|
use std::net::SocketAddr;
|
2021-05-31 10:20:07 +02:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2021-08-10 10:03:29 +02:00
|
|
|
// Set the RUST_LOG, if it hasn't been explicitly defined
|
|
|
|
if std::env::var("RUST_LOG").is_err() {
|
|
|
|
std::env::set_var("RUST_LOG", "hello_world=debug")
|
|
|
|
}
|
|
|
|
tracing_subscriber::fmt::init();
|
2021-08-01 22:01:33 +02:00
|
|
|
|
2021-08-10 10:03:29 +02:00
|
|
|
// build our application with a route
|
2021-08-17 19:00:24 +02:00
|
|
|
let app = route("/foo", get(handler)).or(route("/bar", get(handler)));
|
2021-05-31 10:20:07 +02:00
|
|
|
|
2021-08-10 10:03:29 +02:00
|
|
|
// run it
|
2021-05-31 10:20:07 +02:00
|
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
2021-08-10 10:03:29 +02:00
|
|
|
tracing::debug!("listening on {}", addr);
|
2021-08-04 15:38:51 +02:00
|
|
|
axum::Server::bind(&addr)
|
2021-06-19 12:50:33 +02:00
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-05-31 10:20:07 +02:00
|
|
|
}
|
|
|
|
|
2021-08-18 00:04:15 +02:00
|
|
|
async fn handler() -> Html<&'static str> {
|
|
|
|
Html("<h1>Hello, World!</h1>")
|
2021-05-31 10:20:07 +02:00
|
|
|
}
|