Part 1: Creating a project
In Django, you start with django-admin startproject and then
manage.py startapp. In rind, one command does both: rind new.
Installing the rind tool
From a checkout of the rind repository:
cargo install --path crates/rind-cli # or: make install-cli
This installs two programs, rind and cargo-rind, so rind … and
cargo rind … do the same thing. Check that it works:
rind --help
Creating the project
rind new library --domain catalog
library is the name of the project. --domain catalog creates a first
domain called catalog.
A domain is the rind word for a part of your application that holds
related models and endpoints: what Django calls an app. Our library
will have two domains: catalog (authors and books) and, later,
lending (who borrowed what).
The tool prints what it writes:
project library
wrote library/Cargo.toml
wrote library/docker-compose.yml
wrote library/.env.example
wrote library/.env
wrote library/.gitignore
wrote library/README.md
wrote library/app/Cargo.toml
wrote library/app/src/lib.rs
wrote library/app/src/main.rs
wrote library/app/tests/schema.rs
domain catalog
wrote library/crates/catalog/catalog-models/Cargo.toml
wrote library/crates/catalog/catalog-models/src/lib.rs
wrote library/crates/catalog/catalog-models/migrations/embedded.rs
…
wrote library/crates/catalog/catalog-api/Cargo.toml
wrote library/crates/catalog/catalog-api/src/lib.rs
…
What is in it
library/
Cargo.toml the Cargo workspace
docker-compose.yml a PostgreSQL server for development
.env DATABASE_URL, BIND, RIND_TEST_DATABASE_URL
app/
src/lib.rs app(): the application, described once
src/main.rs the command line (like manage.py)
tests/schema.rs a test that runs `rind check`
crates/
catalog/
catalog-models/ the models of the domain, and its migrations
catalog-api/ the resources (endpoints) of the domain
A few things are different from a Django project.
A domain is several crates. Each domain is split into layers, one
Rust crate per layer: catalog-models holds the models,
catalog-api the HTTP endpoints. Rust compiles a crate as a whole, so
small crates mean that a change recompiles only a small part of the
project. It also keeps the models free of any HTTP code. You can read the
full reasons in Organising an application; for now,
remember that models go in -models, endpoints in -api.
The application is a value. Open app/src/lib.rs:
#![allow(unused)]
fn main() {
pub fn app() -> rind::Application {
rind::Application::new("Library", env!("CARGO_PKG_VERSION"))
.models(catalog_models::migrations())
.api(catalog_api::api())
}
}
This function is the equivalent of Django’s INSTALLED_APPS and
ROOT_URLCONF together. Every domain gives its models (.models(..))
and its endpoints (.api(..)). Everything rind does — creating tables,
serving routes, writing the OpenAPI document — starts from this value.
The rind tool adds a line here each time you add a domain.
main.rs is one line. Open app/src/main.rs:
fn main() -> std::process::ExitCode {
rind::cli::main(app::app())
}
The commands of your application (migrate, serve, …) are rind’s,
like the commands of manage.py are Django’s. You will add a command of
your own in part 5.
Starting the database
The project comes with a docker-compose.yml for PostgreSQL, and its
.env points to it:
cd library
docker compose up -d
# .env
DATABASE_URL=postgres://library:library@127.0.0.1:5432/library
BIND=127.0.0.1:8080
RIND_TEST_DATABASE_URL=postgres://library:library@127.0.0.1:5432/library
If you already run a PostgreSQL server on port 5432, change the port in
both files, or point DATABASE_URL to a database of your own server.
The rind tool reads .env and passes it to your application as
environment variables. (Plain cargo run and cargo test do not read
it: remember this when you get to the tests.)
The first run
rind check
rind migrate
rind serve
rind checkcompiles the application and checks that the models and the migrations agree. The first time, compiling takes a few minutes.rind migrateapplies the migrations. There are none yet, but it creates the table where rind records what it has applied.rind servestarts the server:
Library (axum) listening on http://127.0.0.1:8080
The server is built on axum by default. rind new --web actix
builds it on actix-web instead; your own code is the same with both.
There is nothing to see yet: the application has no endpoint. Stop the
server with Ctrl-C, and let’s describe some data.
Coming from Django: there is no
settings.py. The configuration that changes between machines (the database, the address to listen on, secrets) comes from environment variables. Everything else is Rust code, inapp(), checked by the compiler.