A tutorial project demonstrating a simple RESTful CRUD API built with Rust using the Axum web framework, SQLite as the database, and SQLx for async database interactions. The project also includes integration tests and sets up state management and migrations.
- Async REST API with Axum
- SQLite database with connection pooling via SQLx
- Database migrations with SQLx migrate macros
- User CRUD operations (Create, Read, Update, Delete)
- UUID-based user IDs
- JSON request/response serialization with Serde
- Application state management via shared
AppState - Integration tests automating server startup and HTTP requests
- CORS middleware support for frontend integration (optional)
- Rust (latest stable) — Install Rust
- Cargo (comes with Rust)
- SQLite installed or bundled with your OS
- sqlx-cli for migrations (
cargo install sqlx-cli --no-default-features --features sqlite) - Postman or
curlfor API testing
git clone https://github.com/yourusername/rust-crud-api.git
cd rust-crud-apiCreate a .env file in the project root:
DATABASE_URL=sqlite://./mydb.sqliteYou can also use an in-memory SQLite database for testing:
DATABASE_URL=sqlite::memory:sqlx migrate runThis creates the necessary tables (users).
cargo runThe server will start on 127.0.0.1:3000.
| Method | Endpoint | Description |
|---|---|---|
| GET | /users |
Get all users |
| POST | /users |
Create a new user |
| PUT | /users/:id |
Update a user by ID |
| DELETE | /users/:id |
Delete a user by ID |
Create user:
curl -X POST http://127.0.0.1:3000/users \
-H "Content-Type: application/json" \
-d '{"name": "Alice", "active": true}'src/main.rs— App entry point, server setupsrc/models/— Data models and structssrc/routes/— Route handlers for API endpointssrc/state.rs— Application state managementmigrations/— SQLx database migration files
Integration tests spin up the server on a random port and use reqwest to send HTTP requests.
Run tests with:
cargo testTests include:
- Creating users
- Fetching users
- Updating users
- Deleting users
- Add frontend with WebAssembly (WASM) using
wasm-bindgenandyew - Add CORS middleware for cross-origin frontend support
- Implement authentication and authorization
- Add pagination and filtering on
/usersGET endpoint - Use a Docker container for database and app deployment
MIT License © 2025 James Jarrett