A clean, production-ready REST API built with Rust and Axum. This project demonstrates best practices for building maintainable, well-documented backend services.
- RESTful API - Clean REST endpoints following standard conventions
- Type Safety - Leverages Rust's type system for compile-time guarantees
- Error Handling - Comprehensive error handling with proper HTTP status codes
- Documentation - Extensive inline documentation and examples
- Testing - Unit and integration tests included
- CORS Support - Configured for cross-origin requests
- Structured Logging - Uses tracing for observability
- Framework: Axum - Modern, ergonomic web framework
- Async Runtime: Tokio - Async runtime for Rust
- Serialization: Serde - Serialization framework
- UUID: uuid - UUID generation and parsing
- Time: Chrono - Date and time handling
- Rust 1.70 or later (Install Rust)
- Cargo (comes with Rust)
- Clone the repository:
git clone https://github.com/yourusername/rust-api.git
cd rust-api- Build the project:
cargo build --release- Run the server:
cargo run --releaseThe API will be available at http://localhost:3000
Run in development mode with hot-reloading:
cargo runRun tests:
cargo testRun with verbose output:
RUST_LOG=debug cargo runGET /Returns the health status of the API.
Response:
{
"status": "healthy",
"service": "rust-api",
"timestamp": 1234567890
}GET /api/v1/usersRetrieves all users in the system.
Response:
{
"users": [
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "John Doe",
"email": "john@example.com",
"created_at": 1234567890,
"updated_at": 1234567890
}
],
"count": 1
}GET /api/v1/users/:idRetrieves a specific user by ID.
Response:
{
"user": {
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "John Doe",
"email": "john@example.com",
"created_at": 1234567890,
"updated_at": 1234567890
}
}Errors:
404 Not Found- User with the given ID does not exist
POST /api/v1/users
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com"
}Creates a new user in the system.
Response: 201 Created
{
"user": {
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "John Doe",
"email": "john@example.com",
"created_at": 1234567890,
"updated_at": 1234567890
}
}Errors:
400 Bad Request- Invalid input (empty name/email, invalid email format)409 Conflict- Email already exists
PUT /api/v1/users/:id
Content-Type: application/json
{
"name": "Jane Doe",
"email": "jane@example.com"
}Updates an existing user. All fields are optional.
Response:
{
"user": {
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "Jane Doe",
"email": "jane@example.com",
"created_at": 1234567890,
"updated_at": 1234567891
}
}Errors:
400 Bad Request- Invalid input404 Not Found- User with the given ID does not exist409 Conflict- Email already in use by another user
DELETE /api/v1/users/:idDeletes a user from the system.
Response: 204 No Content
Errors:
404 Not Found- User with the given ID does not exist
All error responses follow this format:
{
"error": {
"message": "Error description",
"status": 404
}
}rust-api/
├── src/
│ ├── main.rs # Application entry point and server setup
│ ├── handlers.rs # HTTP request handlers
│ ├── models.rs # Data models and storage
│ └── error.rs # Error types and handling
├── tests/
│ └── integration_test.rs # Integration tests
├── Cargo.toml # Project dependencies and metadata
├── rustfmt.toml # Code formatting configuration
├── clippy.toml # Linting configuration
└── README.md # This file
This project follows Rust best practices:
- Clippy - Strict linting enabled
- Rustfmt - Consistent code formatting
- Documentation - All public APIs documented
- Tests - Unit and integration tests included
- Error Handling - Comprehensive error types
Run linter:
cargo clippy -- -D warningsFormat code:
cargo fmtThis project is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.