IT JUST FOR MY STUDIES AND LEARNINGS WITH RUST.
An RESTfull API designed in Rust, using Hyper, Tokio and serde_json.
Simple, fast and perfect for who want learn how works the basic flow of GET, POST, PUT, PATCH and DELETE using sharing state with Arc<RwLock<>>.
- Rust
- Tokio (async runtime)
- Hyper (HTTP server)
- serde / serde_json (JSON serialization)
- Arc + RwLock (safe share state between threads)
src/
└── body.rs
├── handler.rs
├── main.rs
├── model.rs
└── routes.rs
├── state.rs
body.rs->handler.rs->model.rs-> Structs likebookroutes.rs-> GET, POST, PATCH and DELETE functionsstate.rs->AppStateusingArc<RwLock<Vec<Book>>>
#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct Book {
pub id: u64,
pub name: String,
pub author: String,
pub pages: u64
}| Método | Rota | Descrição |
|---|---|---|
| GET | / |
Return all books |
| POST | / |
Create a new book |
| GET | /{id} |
Return a specific book |
| PUT | /{id} |
Edit a book |
| DELETE | /{id} |
Remove a book |
- Install Rust (nightly or stable)
- Run:
cargo runThe API rises in:
http://127.0.0.1:3002/Response:
[
{
"id": 1,
"name": "Silmarillion",
"author": "JRR Tolkien",
"pages": 500
}
]POST http://127.0.0.1:3000/
Content-Type: application/json
{
"id": 2,
"name": "Hyper API from scratch",
"author": "Client",
"pages": 0
}
DELETE http://127.0.0.1:3000/2
{
"status": "removed",
"was": {
"id": 2,
"name": "Hyper API from scratch",
"author": "Client",
"pages": 0
}
}
- How uses
Arc<RwLock<T>>with Hyper. - How to deal with futures async without block a thread.
- How to parser dynamic endpoints (ex:
/123). - How to assembly JSON responses with Hyper.
- Data Validation
- Uses traits for handlers
- Add tests with
tokio::test - Add internal documentation