
Create a rust rest api project with cargo by naming the package bookstore, initialize a git repo and gitignore, and verify rust 1.68.0 compatibility.
Rocket website: rocket.rs
Add the following to your Cargo.toml under [dependencies]:
rocket = { version = "^0.5.0-rc.2", features = ["json"] }
main.rs
#[macro_use] extern crate rocket;
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
}
Explore how Rocket handles routing, validation, processing, and response in a rest api, and connect with a MySQL database using an orm with migration support.
Configure a database app.config from environment variables, create a connection pool, and run migrations with the migrator to ensure a successful Rocket database startup.
Implement sign in by locating the user by email, verifying the bcrypt password, and issuing a jwt with sub, role, and four-hour expiry.
Set up the Postman collection for the Rust rest api, configure token-based authentication with a jwt token variable, apply inherited auth for all requests, and complete the basic setup.
Implement an author list endpoint and the response representation of a single author in the author controller, build index function, test it, and confirm an empty list with total zero.
Launch a docker image for the bookstore server and recap rocket, crm, and json web tokens authentication with sign up and sign in, plus full author and book CRUD.
In this course, we'll create a REST API server in Rust to represent a bookstore. This bookstore would allow us to add books and authors.
Using this system, we'll be able to:
Create, list, update and delete authors
Create, list, update and delete books
Associate and disassociate books and authors
List all books by a particular author
We'll learn how to:
Create a new Rust project using cargo
Build and run our Rust project
Add crates and enable crate features
We'll learn about basic API concepts such:
Routing and HTTP methods
Extracting data from HTTP requests
Interacting with the database to query and insert data
Creating user accounts
Authentication using JWT
Creating and using relationships between models to query and list associated data
Along with these concepts, we'll learn how to:
Handle incoming (request) and outgoing (response) data in a type safe way
Handle CORS
Create and run database migrations
Create one-to-many database relationships
Create entities from database tables
We'll have a bonus lecture at the end to learn how to Dockerize our API server to deploy and run it anywhere.
We'll be using the following crates:
Rocket: a web framework for Rust that makes it simple to write fast, secure web applications without sacrificing flexibility, usability, or type safety.
SeaORM: is a relational ORM to help you build web services in Rust.
jsonwebtoken to create and decode JWTs in a strongly typed way.
serde_json for serializing and deserializing Rust data structures efficiently and generically.