Skip to content

elman23/rust-book-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Book App 🚀

An example for writing a CRUD app using warp and tokio-postgres.

Inspired by this app.

🏁 A simple API using Rust + PostgreSQL

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.

1. 💡 Prerequisites

2. 🏃 Running the database Docker

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

3. 🏗️ Build project

☢️ 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

4. 🧪 Testing the CRUD Endpoints

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.

4.1 📝 Creating a user

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"}'

4.2 📝 Checking created user with ID

Command:

curl -i -H "Content-Type: application/json" -X GET http://127.0.0.1:8080/book/1

4.3 📝 Updating user data

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"}'

4.4 📝 Checking all registered book

Command:

curl -i -H "Content-Type: application/json" -X GET http://127.0.0.1:8080/book

Expected answer:

4.5 📝 Deleting a user with ID

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! 🍹

Releases

No releases published

Packages

No packages published

Languages