Getting started
Before starting building your HTTP server, ensure your system meets the requirements from the Prerequisites section.
Scaffold your project​
- Open a terminal
- Run the following command:
mkdir upb-rust-workshop && cd upb-rust-workshop
cargo init
Project structure​
upb-rust-workshop/
├─ src/
│ └─ main.rs # Entry point
├─ target/ # Build artifacts
├─ .gitignore # Git ignore file
├─ Cargo.lock # Dependency lock file
└─ Cargo.toml # Project manifest
Add dependencies​
Run the following commands:
cargo add axum
cargo add tokio --features=full
Next, check that your Cargo.toml
file looks like this:
Cargo.toml
[package]
name = "upb-rust-workshop"
version = "0.1.0"
edition = "2024"
[dependencies]
axum = "0.8.1"
tokio = { version = "1.44.1", features = ["full"] }
name, version, edition and dependencies versions may vary.
Scaffold your server​
Open the src/main.rs
file and add the following code:
src/main.rs
use axum::routing::get;
#[tokio::main]
async fn main() {
let app = axum::Router::new().route("/", get(async || "Hello, World!"));
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
Run in development mode​
Run the following command:
cargo run
Open your browser and navigate to http://localhost:8080. You should see the message Hello, World!
.
Build and run​
To build and run your server in release mode, run the following command:
cargo build --release
Then, run the binary:
./target/release/upb-rust-workshop
Open your browser and navigate to http://localhost:8080. You should see the message Hello, World!
.