Zero To Production in Rust is a book by Luca Palmieri where he details his progress implementing a web backend using Rust. It's a wonderful book, with lots of resources, tips and tricks. It has fostered lots of projects, and so its useful to browse through github and look at how developers have taken the book's initial code and choices and made changes.
This is such a project, in which I have
- relied heavily on the work of Luca Palmieri for the structure and process,
- incorporated bits and pieces in similar projects,
- used my own experience to make a couple of departures
| DIFFERENCE FROM ORIGINAL | MOTIVATION |
|---|---|
| s / actix-web / axum / g | Motivation |
| cucumber / gherkin for integration | Motivation |
| anyhow > /dev/null | Motivation |
| 'generic' database executor | Motivation |
| Using xtask | Description |
| Traits for improved testing | Motivation |
| s / claim / speculoos / g | claim does not seem to be maintained |
| Maybe opentelemetry? | |
| frontend | vue.js |
This project implements the backend and the frontend of a newsletter website
This is a rust application, so we need to have the following setup:
- The rust toolchain
- git
- docker
- openssl (generate certificates)
- npm (build the frontend)
To install this website, we need to setup 3 components:
- A database, where we store all the details.
- The backend, which is webserver providing a REST API.
- The frontend, which is a client-side rendering webapplication
Clone the repository:
git clone https://github.com/crocme10/zero2prodThe source for zero2prod uses sqlx, which makes compiles time checks against a running database. So we have the choice,
- either use sqlx's offline mode (requires to run
cargo sqlx prepareahead of the compilation) - or start the database before compiling the project.
Let's go with the second option. We use
cargo xtask to add automation tasks to
this project. The TLDR is cargo install cargo-xtask. Then the following
command will start a docker container for postgres, and run the migrations to
setup the database.
cargo xtask postgresThis will take some time to build a part of the project (the common workspace)
which does not depend on the database (otherwise, it would fail because the
database is not there yet), and the xtask workspace. The database is configured
with the files found in ./config/database. It will launch a docker container
with postgres, and run the migrations to setup the database.
Now we should have a running database, we can compile the rest of the project.
sqlx knows about the database through the environment variable DATABASE_URL,
whose value can be found at the last line of the previous command:
for bash users:
export DATABASE_URL="postgres://postgres:password@localhost:5462/newsletter"or for fish users:
set DATABASE_URL "postgres://postgres:password@localhost:5462/newsletter"The frontend will compile into a directory, that will be served by the frontend.
cargo xtask frontendThe database is a prerequisite for compiling the backend, because sqlx makes
compile time checks against the database. So with the DATABASE_URL set, we
can compile the application:
cargo build --releaseYou may also need to generate certificates:
cargo xtask certificateAt the root of the project:
docker build -t zero2prod:latest .When you build the server (debug or release), you get a binary in target/{debug, release}/zero2prod
This binay can be configured with configuration files found in ./config
The binary requires a path to a configuration directory where all configuration is found.
This configuration can be overriden with local configuration files, and with environment variables.
Environment variables use __ to separate sections, and ZERO2PROD for the
prefix. So to modify the database's name, which is in the database section, we
should set ZERO2PROD\_\_DATABASE\_\_DATABASE_NAME=newsletter
The server takes two commands:
- config: to display the configuration as JSON
- run: to run the server.
./target/debug/zero2prod -c ./config runIf you want to modify configuration, you can easily do that on the command line:
./target/debug/zero2prod -c ./config -s database.require_ssl=false -s application.http=8080 -s application.https=8443 runcurl --header "Content-Type: application/json" --request POST --data '{"username": "alice", "email": "alice@acme.inc"}' http://localhost:8082/subscriptionsStart by deploying a postgres docker container:
cargo xtask postgresThen set your DATABASE_URL variable accordingly
And then build run the tests.
cargo xtask ci
zero2prod/ ├─ services/ │ ├─ zero2prod_backend/ backend server │ ├─ zero2prod_frontend/ frontend wasm │ ├─ zero2prod_common/ structures shared by backend and frontend │ ├─ zero2prod_fakeemail/ simple server to mock external email service. ├─ config/ configuration ├─ docker/ Dockerfiles and entrypoints ├─ documentation/ Additional documentation ├─ common/ code shared between xtask and services ├─ xtask/ tasks implementation. ├─ dev.sh/ script to start services ├─ spec.yaml/ digital ocean deployment
-
0.0.4
-
0.0.1
- Work in progress
Distributed under the MIT license. See LICENSE for more information.
https://github.com/crocme10/zero2prod
- Fork it (https://github.com/crocme10/zero2prod/fork)
- Create your feature branch (
git checkout -b feature/fooBar) - Commit your changes (
git commit -am 'Add some fooBar') - Push to the branch (
git push origin feature/fooBar) - Create a new Pull Request