๐Ÿ“• BookStore

April 4, 2023 ยท View on GitHub

BookStore Management REST API Server in Rust with Rocket, SeaORM and JWT.


Udemy Course

Learn about creating this in my Udemy course: Creating an API server in Rust with Rocket, SeaORM and JWT by Afaan Bilal.


Author: Afaan Bilal


Run with Docker Docker

docker run --rm -it -p 8000:8000 \
    -e ROCKET_ADDRESS=0.0.0.0 \
    -e BOOKSTORE_JWT_SECRET=[RANDOM SECRET] \
    -e BOOKSTORE_DB_HOST=host.docker.internal \
    -e BOOKSTORE_DB_PORT=3306 \
    -e BOOKSTORE_DB_USERNAME=[DB USERNAME] \
    -e BOOKSTORE_DB_PASSWORD=[DB PASSWORD] \
    afaanbilal/bookstore

Configuration

The following environment variables configure the server.

Environment VariableDefault valueDescription
BOOKSTORE_JWT_SECRET[none]The JWT signing secret. Must be set.
BOOKSTORE_DB_HOSTlocalhostDatabase Host
BOOKSTORE_DB_PORT3306Database Port
BOOKSTORE_DB_USERNAMErootDatabase Username
BOOKSTORE_DB_PASSWORD[blank]Database Password
BOOKSTORE_DB_DATABASEbookstoreDatabase Name
ROCKET_ADDRESS127.0.0.1HTTP Server Bind Address
ROCKET_PORT8000HTTP Server Port

Run

cargo run

API

API

MethodPathAuth?Description
GET/โฌœIndex. Returns Hello, World!.
POST/auth/sign-upโฌœCreate a new account.
POST/auth/sign-inโฌœSign in. Returns a JWT on success.
GET/auth/meโœ…Get the authenticated user's details.
GET/authorsโœ…Get a list of authors.
POST/authorsโœ…Create an author.
GET/authors/{id}โœ…Get the author with matching the id.
PUT/authors/{id}โœ…Update the author matching the id.
DELETE/authors/{id}โœ…Delete the author matching the id.
GET/authors/{id}/booksโœ…Get a list of books by the author matching the id.
GET/booksโœ…Get a list of books.
POST/booksโœ…Create a book.
GET/books/{id}โœ…Get the book with matching the id.
PUT/books/{id}โœ…Update the book matching the id.
DELETE/books/{id}โœ…Delete the book matching the id.

Authentication Flow

Authentication Flow

  • All auth required requests: Add header token with the JWT as the value.
  • Token lifetime: 4 hours.

Database Structure

Database Structure


Test

cargo test

Entity Generation

sea generate entity -o src/entities -u mysql://root:@localhost:3306/bookstore

Contributing

All contributions are welcome. Please create an issue first for any feature request or bug. Then fork the repository, create a branch and make any changes to fix the bug or add the feature and create a pull request. That's it! Thanks!


License

Bookstore is released under the MIT License. Check out the full license here.