This repo is a companion to the book Zero To Production In Rust. It contains the code samples and exercises discussed in the book.
To get started, clone the repo and navigate to the project directory, and run cargo-watch
to start developing.
cargo watch -q -c -w src/ -x check -x test -x run
To avoid recompiling when changing anything else than the source code, the cargo-watch command is configured to only watch the src/
directory.
To run the tests, use the following command:
cargo test
PS: On linux you may achieve a limit on file descriptors, to fix it, run the following command:
ulimit -n 8192
You may change the number of file descriptors to another value if needed.
To run tests with logging enabled, use the following command:
TEST_LOG=true cargo test health_check_works | bunyan
OBS: The bunyan
command is used to format the logs in a more readable way. You can install it using the following command:
npm install -g bunyan
or
cargo install bunyan
To build the docker image, use the following command:
docker build --tag zero2prod --file Dockerfile .
To run the docker image, use the following command:
docker run --rm -p 8000:8000 zero2prod
To run the database run the script in the folder scripts/init_db.sh
:
./scripts/init_db.sh
If the DB has already been deployed before, you the add the variable SKIP_DOCKER
to skip the docker deployment:
SKIP_DOCKER=true ./scripts/init_db.sh
To learn about symbol stripping, check the following link
The Docker image rust:alpine is used to build the final image. This image is very small, but it does not have the necessary tools to debug the application. To debug the application, you can use the image rust:slim.
Install sqlx-cli to run migrations:
cargo install --no-default-features \
--features rustls,postgres
If you want to use sqlx offline, you can use the following command:
cargo sqlx prepare --workspace
To get the logs from sqlx, run:
# sqlx logs are a bit spammy, cutting them out to reduce noise
export RUST_LOG="sqlx=error,info"
export TEST_LOG=true
# Runing the logs for tests
cargo t <TEST_NAME> | bunyan
First, install cargo-tarpaulin:
cargo install cargo-tarpaulin
To measure code coverage, run the following command:
cargo tarpaulin --ignore-tests
Check the tarpaullin documentation for more information here
Clippy is a collection of lints to catch common mistakes and improve your Rust code.
If Clippy is not installed, you can install it using the following command:
rustup component add clippy
To run clippy, use the following command:
cargo clippy
If in CI pipelines, you can fail in warning with the following command:
cargo clippy -- -D warnings
Clippy's documentation can be found here
Rustfmt is Rust's official formatting tool. It can be installed using the following command:
rustup component add rustfmt
To format your code, use the following command:
cargo fmt
If in CI pipelines, you can fail in warning with the following command:
cargo fmt -- --check
Rustfmt's documentation can be found here
To check for security vulnerabilities in your dependencies, you can use the cargo-audit tool. Cargo-deny is also another tool that can be used to check for security vulnerabilities.
First, install the tool using the following command:
cargo install cargo-audit
To check for vulnerabilities, use the following command:
cargo audit
To remove unused dependencies, you can use the cargo-udeps tool. First, install the tool using the following command:
cargo install cargo-udeps
To remove unused dependencies, use the following command:
cargo udeps
Allow to build the dependencies in a separate step, to speed up the build process.
First, install the tool using the following command:
cargo install cargo-chef
To prepare the dependencies, use the following command:
cargo chef prepare --recipe-path recipe.json
To build the dependencies, use the following command:
cargo chef cook --release --recipe-path recipe.json
These are some usefull crates:
- tracing
- tracing-bunyan-formatter
- tracing-log
- tracing-subscriber
- secrecy (Protect your logs from leaking sensitive information)
- fake (Generate random fake data)
- wiremock
- linkify (extract links from text)
- tera (template engine for emails)
- thiserror (better error handling)
- anyhow (better error handling, working together with thiserror)
- sha3 (hashing with SHA3)
- argon2 (hashing with Argon2)
Search about PHC string format for password hashing.
For example, the PHC string format for Argon2 is:
- Argon2:
${algorithm}${algorithm version}${,-separated algorithm parameters}${hash}${salt}
For working with HTML in Rust, you can use the following crates:
- htmlescape
- hmac
- urlencoding
For working with Actix-Web, you can use the following crates:
- actix-web
- actix-session
- actix-web-flash-messages