🌐
Hands-on Project

HTTP Web Server

Create a multi-threaded web server from scratch

Rust Web Server Project

A simple REST API web server built with Axum, demonstrating practical Rust concepts for web development.

Concepts Covered

This project demonstrates several important Rust concepts for web development:

  1. Web Framework Usage with Axum
  2. Async Programming with Tokio
  3. REST API Design and HTTP methods
  4. Serialization and deserialization with serde and serde_json
  5. Middleware with Tower HTTP layers
  6. Error handling in web applications
  7. Routing and path parameters
  8. JSON request/response handling

Project Structure

web-server/
ā”œā”€ā”€ Cargo.toml
└── src/
    └── main.rs

Setup

  1. Make sure you have Rust and Cargo installed:

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  2. Navigate to the project directory:

    cd rust-tutorial/projects/web-server
  3. Build and run the project:

    cargo run

API Endpoints

Once the server is running, you can interact with the following endpoints:

Health Check

GET /health

Returns a simple status message to verify the server is running.

Get All Users

GET /users

Returns a JSON array of all users.

Get User by ID

GET /users/:id

Returns a JSON object of a specific user by ID.

Create User

POST /users

Creates a new user. Send JSON payload with name and email.

Update User

PUT /users/:id

Updates an existing user. Send JSON payload with name and email.

Delete User

DELETE /users/:id

Deletes a user by ID.

Usage Examples

Check server health

curl http://localhost:3000/health

Get all users

curl http://localhost:3000/users

Get user by ID

curl http://localhost:3000/users/1

Create a new user

curl -X POST http://localhost:3000/users \
  -H "Content-Type: application/json" \
  -d '{"name": "Charlie", "email": "charlie@example.com"}'

Update a user

curl -X PUT http://localhost:3000/users/1 \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice Smith", "email": "alice.smith@example.com"}'

Delete a user

curl -X DELETE http://localhost:3000/users/1

How It Works

The web server uses Axum as the web framework and Tokio as the async runtime. It stores users in memory (in a real application, you would use a database).

Key Components

  1. User struct - Represents a user with ID, name, and email
  2. Request structs - Define the expected JSON format for create/update operations
  3. Route handlers - Async functions that handle HTTP requests
  4. Router - Maps HTTP endpoints to handler functions
  5. Middleware - Adds CORS support and request tracing

Error Handling

The server returns appropriate HTTP status codes for different scenarios:

  • 200 OK for successful GET and PUT requests
  • 201 Created for successful POST requests
  • 204 No Content for successful DELETE requests
  • 404 Not Found when a user cannot be found

Running Tests

To run the tests for this project:

cargo test

Building for Release

To build an optimized version for release:

cargo build --release

The executable will be in target/release/rust-tutorial-web-server.

Extending the Project

Possible extensions for this project:

  1. Add database persistence with SQLite or PostgreSQL
  2. Implement user authentication and authorization
  3. Add input validation for request payloads
  4. Implement pagination for the users list
  5. Add logging with the tracing crate
  6. Add rate limiting middleware
  7. Implement unit and integration tests

šŸ¦€ Rust Programming Tutorial

Learn from Zero to Advanced

Built with Next.js and Tailwind CSS • Open Source