This is an experimental project that I created because I wanted to try making an API using only pure Rust, without using external crates like Axum or Actix, like Golang.
A simple RESTful API server built with Rust for managing Todo items. This API supports creating, reading, updating, and deleting (CRUD) operations, with robust data validation and comprehensive error logging.
- Create Todos: Add new Todo items with titles.
- Read Todos: Retrieve all Todos or a specific Todo by ID.
- Update Todos: Modify existing Todos' titles and completion status.
- Delete Todos: Remove Todos by ID.
- Data Validation: Ensures that titles are not empty and the
completedfield is a boolean. - Error Logging: Logs detailed error messages with timestamps to
error.log. - Thread Pool: Efficiently handles multiple incoming connections using a thread pool.
- Rust: Programming language used for building the API.
- Serde: Serialization and deserialization framework.
- TCP Networking: Utilizes
TcpListenerandTcpStreamfor handling HTTP requests. - Threading: Implements a thread pool for managing concurrent connections.
- Chrono: Handles date and time for logging purposes.
- Rust: Ensure Rust is installed. If not, install via rustup.
git clone https://github.com/yourusername/rust-todo-api.git
cd rust-todo-apicargo build --release
cargo run --release
The server will start and listen on http://127.0.0.1:8080
- URL:
/todos - Method:
GET - Response: JSON array of Todo items.
- URL:
/todos/{id} - Method:
GET - Response: JSON object of the Todo item.
- URL:
/todos - Method:
POST - Headers:
- Content-Type: application/json
- Content-Length: {length}
- Body:
{
"title": "Learn Rust"
}- Response: JSON object of the created Todo item.
- URL:
/todos/{id} - Method:
PUT - Headers:
Content-Type: application/jsonContent-Length: {length}
- Body:
{
"title": "Learn Advanced Rust",
"completed": true
}- Response: JSON object of the updated Todo item.
- URL:
/todos/{id} - Method:
DELETE - Response: Message indicating successful deletion.
Running Tests Execute the test suite using the following command:
cargo test
All errors are logged to error.log with timestamps for easy troubleshooting. The API provides detailed error messages to clients, ensuring clarity on what went wrong.