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:
- Web Framework Usage with Axum
- Async Programming with Tokio
- REST API Design and HTTP methods
- Serialization and deserialization with
serdeandserde_json - Middleware with Tower HTTP layers
- Error handling in web applications
- Routing and path parameters
- JSON request/response handling
Project Structure
web-server/
āāā Cargo.toml
āāā src/
āāā main.rsSetup
Make sure you have Rust and Cargo installed:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shNavigate to the project directory:
cd rust-tutorial/projects/web-serverBuild and run the project:
cargo run
API Endpoints
Once the server is running, you can interact with the following endpoints:
Health Check
GET /healthReturns a simple status message to verify the server is running.
Get All Users
GET /usersReturns a JSON array of all users.
Get User by ID
GET /users/:idReturns a JSON object of a specific user by ID.
Create User
POST /usersCreates a new user. Send JSON payload with name and email.
Update User
PUT /users/:idUpdates an existing user. Send JSON payload with name and email.
Delete User
DELETE /users/:idDeletes a user by ID.
Usage Examples
Check server health
curl http://localhost:3000/healthGet all users
curl http://localhost:3000/usersGet user by ID
curl http://localhost:3000/users/1Create 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/1How 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
- User struct - Represents a user with ID, name, and email
- Request structs - Define the expected JSON format for create/update operations
- Route handlers - Async functions that handle HTTP requests
- Router - Maps HTTP endpoints to handler functions
- Middleware - Adds CORS support and request tracing
Error Handling
The server returns appropriate HTTP status codes for different scenarios:
200 OKfor successful GET and PUT requests201 Createdfor successful POST requests204 No Contentfor successful DELETE requests404 Not Foundwhen a user cannot be found
Running Tests
To run the tests for this project:
cargo testBuilding for Release
To build an optimized version for release:
cargo build --releaseThe executable will be in target/release/rust-tutorial-web-server.
Extending the Project
Possible extensions for this project:
- Add database persistence with SQLite or PostgreSQL
- Implement user authentication and authorization
- Add input validation for request payloads
- Implement pagination for the users list
- Add logging with the
tracingcrate - Add rate limiting middleware
- Implement unit and integration tests