A simple CRUD (Create, Read, Update, Delete) REST API built with Rust using the Actix Web framework and PostgreSQL as the database[1].
- RESTful API endpoints for basic CRUD operations
- PostgreSQL integration with
sqlx - JSON serialization with
serde - User authentication support with
jsonwebtokenand password hashing usingargon2 - Environment variable configuration with
dotenv - CORS support with
actix-cors - Logging with
env_logger - UUID support for unique identifiers
- Date and time handling with
chrono
- Rust (latest stable version recommended)
- PostgreSQL database (remote or local)
cargopackage manager (comes with Rust)sqlx-clifor database migrations (optional but recommended)
-
Clone the repository:
git clone <repository-url> cd crud-api -
Create a
.envfile in the root directory with your PostgreSQL connection URL:DATABASE_URL=postgresql://user:password@your-postgres-host/database_name -
Install dependencies and build the project:
cargo build -
Run database migrations (if you use
sqlxmigrations):sqlx migrate run -
Start the server:
cargo run
The API will be running at http://localhost:8000
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/healthchecker |
Health check endpoint |
| GET | /api/items |
Retrieve all items |
| POST | /api/items |
Create a new item |
| GET | /api/items/{id} |
Retrieve an item by ID |
| PATCH | /api/items/{id} |
Update an item by ID |
| DELETE | /api/items/{id} |
Delete an item by ID |
(Replace items with your actual resource name, e.g., todos or users.)
Key crates used in this project:
actix-web— Web framework for building HTTP serversactix-cors— Cross-Origin Resource Sharing supportserdeandserde_json— Serialization and deserialization of JSONsqlx— Async PostgreSQL driver and ORMuuid— For generating UUIDschrono— Date and time handlingjsonwebtoken— JWT token creation and validationargon2— Password hashingdotenv— Manage environment variablesenv_logger— Logging support
Use cargo-watch for hot reloading during development:
cargo install cargo-watch
cargo watch -q -c -w src/ -x run
export RUST_LOG=actix_web=info
This project is licensed under the MIT License.