A simple REST API for managing a personal book library, built with Rust and Axum.
- CRUD operations for books
- In-memory storage
- Track book availability
cargo runThe server will start on http://localhost:3000
GET /health- Health checkGET /books- List all books (with optional filters and pagination)POST /books- Add a new bookGET /books/{id}- Get a book by IDPUT /books/{id}- Update a bookDELETE /books/{id}- Delete a book
Add a book:
curl -X POST http://localhost:3000/books \
-H "Content-Type: application/json" \
-d '{
"title": "Clean Code",
"author": "Robert C. Martin",
"year": 2008,
"isbn": "978-0132350884"
}'List all books:
curl http://localhost:3000/booksFilter books:
# Filter by availability
curl http://localhost:3000/books?available=true
# Filter by author (case-insensitive search)
curl http://localhost:3000/books?author=martin
# Filter by publication year
curl http://localhost:3000/books?year=2008
# Combine multiple filters
curl "http://localhost:3000/books?available=true&author=martin&year=2008"Paginate books:
# Get the second page with 5 books per page
curl "http://localhost:3000/books?page=2&limit=5"
# Combine pagination with filters
curl "http://localhost:3000/books?available=true&page=1&limit=20"The response includes a pagination metadata object alongside the data array:
{
"data": [...],
"pagination": {
"page": 1,
"limit": 10,
"total_items": 42,
"total_pages": 5
}
}
pagedefaults to1andlimitdefaults to10(max100).
Update book availability:
curl -X PUT http://localhost:3000/books/1 \
-H "Content-Type: application/json" \
-d '{"available": false}'Delete a book:
curl -X DELETE http://localhost:3000/books/1{
"id": 1,
"title": "Book Title",
"author": "Author Name",
"year": 2024,
"isbn": "978-1234567890",
"available": true
}When adding a new book, the following validations are enforced:
- Title: Must not be empty
- Author: Must not be empty
- Year: Must be between 1000 and the current year
- ISBN: Must be a valid ISBN-13 format (13 digits, hyphens allowed)
Invalid requests will return 400 Bad Request with an error message.
MIT