
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.
Explore the rust project structure and setup, including source and target directories, cargo files, and gitignore, then build and run a rest api server with cargo and cargo run.
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.
Install the cli with cargo, initialize migrations, and set a rust module; learn the Migrator trait and up and down functions, including the iDEN-derived enum for naming.
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.
Create a Postman collection for a bookstore rest api with folders for user, author, and book, including sign in, sign up, and author/book endpoints.
Develop request and response data types for the sign up request handler in Rocket, returning sign up status string and paving the way for signing in to obtain a JWT.
Implement sign up in the authentication controller, verify email uniqueness, hash passwords with bcrypt, and insert a secure user record using the active model; test with postman.
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.
Convert sign in and me route responses to json by serializing structs, returning json payload with jwt token and user details (id, first name, last name, email) using resume struct.
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.
Create an author by defining a request data type and using rocket's deserialize macro to parse the body, then insert an active model and return json.
Update authors and books using the same request payload, load the model, handle 404s, update fields including updated_at, and return the updated item as JSON.
Delete books and authors by fetching them from the database, performing the deletion, returning a success message, and verifying the deletions by attempting to retrieve by ID.
Dockerize the rust api server with a multi-stage build. Build in a rust builder image, copy to a debian bullseye slim runtime, install libssl-dev, expose port 80, and run ./bookstore.
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.