Skip to content

Latest commit

 

History

History
87 lines (61 loc) · 2.51 KB

README.md

File metadata and controls

87 lines (61 loc) · 2.51 KB

Rust Backend Template

Easily get started building your backend with Rust and PostgreSQL.

About

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.

Usage

  1. Fork or clone the repository.
git clone [email protected]:sempruijs/rust-backend-template.git
cd rust-backend-template
  1. Remove the flake.nix and flake.lock if you don't want to use Nix.
rm flake.nix
rm flake.lock
  1. 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.

  1. Set variables:
export DATABASE_URL="postgres://username:password@host:port/database_name"
export SECRET_KEY="Your secret key :)"
  1. Run:
cd backend/
cargo run

If everything went well, you have a running backend that you can easily customize.

Contributing

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 ❤️

License

MIT