Skip to content

manuelmauro/zero2prod

Repository files navigation

Rust Backend Stack

Setup

Installing Rust and Cargo

Install Rust as described here.

Installing sqlx-cli

SQLx is an async, pure Rust SQL crate featuring compile-time checked queries without a DSL.

SQLx-CLI is SQLx's associated command-line utility for managing databases, migrations, and enabling "offline" mode with sqlx::query!() and friends. It is published on the Cargo crates registry as sqlx-cli and can be installed like so:

cargo install sqlx-cli --features postgres

Running Postgres

The following script will start the latest version of Postgres using Docker, create the database and run the migrations.

./scripts/init_db.sh

Preparing SQLx data

There are 3 steps to building with "offline mode":

  • Enable the SQLx's Cargo feature offline
    • E.g. in your Cargo.toml, sqlx = { features = [ "offline", ... ] }
  • Save query metadata for offline usage
    • cargo sqlx prepare
  • Build

Starting the Application

With everything else set up, all you need to do at this point is:

cargo run

If successful, the API server is now listening at port 8080.

Hot Reload

Use cargo-watch for hot reloading the server.

cargo watch -x run

Preparing Tailwind CSS

Install NodeJS using Volta

Follow the documentation here

curl https://get.volta.sh | bash

Install dependencies

npm run install

Build the project's CSS

npx tailwindcss -i ./input.css -o ./assets/output.css --watch

Quality Assurance

Testing

Run unit tests with:

cargo test

Formatting

Format with:

cargo fmt --check

Linting

Lint with:

cargo clippy -- -D warnings

Code Coverage

Check code coverage:

cargo tarpaulin --verbose --workspace

Deployment

Dependencies

This application relies on Digital Ocean's App Platform for its CD pipeline. Install Digital Ocean's CLI doctl to interact with platform.

First Deployment

To deploy a new instance of the application on Digital Ocean run:

doctl apps create --spec spec.yaml

then migrate the production database with:

DATABASE_URL=<connection-string> sqlx migrate run

License

Licensed under MIT license. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, shall be licensed as above, without any additional terms or conditions.