8000 GitHub - FrostPrice/zero2prod
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

FrostPrice/zero2prod

Repository files navigation

Zero2Prod

This repo is a companion to the book Zero To Production In Rust. It contains the code samples and exercises discussed in the book.

Getting Started

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.

Run tests

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

Docker

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

Scripts

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

Optmizations

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.

Rust Development Tools

Run Migrations

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

Measure Code Coverage

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 Linting - Your new best friend

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

Rust Formatting

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

Rust Crates Security

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

Remove unused dependencies

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

Cargo Chef

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

Tips

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published
3BEB

Packages

No packages published
0