An example for writing a CRUD app using warp
and tokio-postgres
.
Inspired by this app.
This is a fictional project for laboratory study written in the Rust 🦀 programming language.
The project is an API with CRUD functionalities that uses a PostgreSQL database.
The data is stored in the public db schema in the book
table.
- Docker
- Rust
- Curl
- Postman or Insomnia - (both are optional)
- Tableplus or DBeaver - (both are optional)
For the application to run, a PostgreSQL database is needed.
Deploy a PostgreSQL instance locally with Docker:
docker run -it -d -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres --name postgres postgres:latest
☢️ You shall already have Rust and ToolChain installed on your workstation. ☢️
Enter the base directory of the project and run the command:
cargo build --release
This will build the app in release mode. After running the project:
cargo run
To test the endpoints, you can use Postman: the collections are in the [Rust Book API](Rust Book API.postman_collection.json) file. Alternatively, you can test the API via command line with curl.
Method | EndPoint | Parameter | Payload |
---|---|---|---|
POST |
/book |
not required | {"title": "Test title", "author": "Test author"} |
GET |
/book/ |
ID | not required |
PUT |
/book/ |
ID | {"title": "Another title", "author": "Another author"} |
GET |
/book |
not required | not required |
DELETE |
/book/ |
ID | not required |
Note: The commands below use
curl
.
Command:
curl -i -H "Content-Type: application/json" -X POST http://127.0.0.1:8080/book -d '{"title": "Test title", "author": "Test author"}'
curl -i -H "Content-Type: application/json" -X POST http://127.0.0.1:8080/book -d '{"title": "Another title", "author": "Another author"}'
Command:
curl -i -H "Content-Type: application/json" -X GET http://127.0.0.1:8080/book/1
Command:
curl -i -H "Content-Type: application/json" -X PUT http://127.0.0.1:8080/book/1 -d '{"title": "Another title", "author": "Another author"}'
Command:
curl -i -H "Content-Type: application/json" -X GET http://127.0.0.1:8080/book
Expected answer:
Command:
curl -i -H "Content-Type: application/json" -X DELETE http://127.0.0.1:8080/book/1
curl -i -H "Content-Type: application/json" -X DELETE http://127.0.0.1:8080/book/2
Enjoy! 🍹