Skip to content

jjarrett21/rust_crud_api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust CRUD API with Axum, SQLite, and SQLx

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.


Features

  • 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)

Prerequisites

  • 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 curl for API testing

Getting Started

1. Clone the repository

git clone https://github.com/yourusername/rust-crud-api.git
cd rust-crud-api

2. Set up environment variables

Create a .env file in the project root:

DATABASE_URL=sqlite://./mydb.sqlite

You can also use an in-memory SQLite database for testing:

DATABASE_URL=sqlite::memory:

3. Run database migrations

sqlx migrate run

This creates the necessary tables (users).

4. Build and run the server

cargo run

The server will start on 127.0.0.1:3000.


API Endpoints

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

Example Request

Create user:

curl -X POST http://127.0.0.1:3000/users \
-H "Content-Type: application/json" \
-d '{"name": "Alice", "active": true}'

Project Structure

  • src/main.rs — App entry point, server setup
  • src/models/ — Data models and structs
  • src/routes/ — Route handlers for API endpoints
  • src/state.rs — Application state management
  • migrations/ — SQLx database migration files

Testing

Integration tests spin up the server on a random port and use reqwest to send HTTP requests.

Run tests with:

cargo test

Tests include:

  • Creating users
  • Fetching users
  • Updating users
  • Deleting users

Next Steps / TODOs

  • Add frontend with WebAssembly (WASM) using wasm-bindgen and yew
  • Add CORS middleware for cross-origin frontend support
  • Implement authentication and authorization
  • Add pagination and filtering on /users GET endpoint
  • Use a Docker container for database and app deployment

Resources


License

MIT License © 2025 James Jarrett


About

Rust Tutorial

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published