Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Serve A Hello, World Web Page In Rust With axum

Creating a rust server app with a single hard coded web page doesn't require much code at all.

Code

[package]
name = "axum_hello_world"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = {version = "1", features = ["full"] }
axum = "0.6"

Code

use axum::response::IntoResponse;
use axum::routing::get;
use axum::{response::Html, Router};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
    let index_route = Router::new().route("/", get(index_handler));
    let addr = SocketAddr::from(([127, 0, 0, 1], 8181));
    axum::Server::bind(&addr)
        .serve(index_route.into_make_service())
        .await
        .unwrap();
}

async fn index_handler() -> impl IntoResponse {
    println!("Got request for /");
    Html("Hello, world")
}

References