Easily get started building your backend with Rust and PostgreSQL.
Building a backend in Rust is not so hard if you know where to start.
This is a not-so-feature-rich template so you can build right away without any distractions.
The template uses the popular inversion of control scheme, which is used in many backends.
The template contains:
- Create a user endpoint.
- Get user details endpoint protected with JWT.
- Login for receiving a JWT.
- Swagger documentation for these endpoints.
- Comments so you can easily read the code, copy, modify, and extend your backend to your needs.
- An optional Nix flake for deployment.
The template uses:
- Rocket, a popular and easy-to-use web framework.
- SQLx, for interacting with the PostgreSQL database.
- jsonwebtoken, for generating JWTs.
- utoipa, for generating Swagger documentation.
Take a look at the Cargo.toml.
- Fork or clone the repository.
git clone [email protected]:sempruijs/rust-backend-template.git
cd rust-backend-template
- Remove the
flake.nix
andflake.lock
if you don't want to use Nix.
rm flake.nix
rm flake.lock
- Set up PostgreSQL on your machine. Run:
psql postgres://username:password@host:port/database_name
And insert the following table:
-- users table
CREATE TABLE users (
user_id UUID PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
);
Note that you can easily use migrations with SQLx as your schema grows.
- Set variables:
export DATABASE_URL="postgres://username:password@host:port/database_name"
export SECRET_KEY="Your secret key :)"
- Run:
cd backend/
cargo run
If everything went well, you have a running backend that you can easily customize.
If you find a bug, spelling mistake, or have an addition that complements the minimal setup, please create an issue or pull request.
Thank you ❤️
MIT