A production-ready starter template for building REST APIs with Rust, following clean architecture principles.
- ποΈ Clean Architecture - Organized in layers (Domain, Application, Infrastructure, Interfaces)
- π Actix-web - High-performance, async web framework
- π SQLx - Async PostgreSQL integration with compile-time query checking
- π OpenAPI/Swagger - Automatic API documentation with utoipa
- π Environment Configuration - Using dotenv for flexible configuration
- π§ͺ Development Tools - Formatting, linting, and git hooks
- π GitHub Automation - Workflows for CI, issue management, and PRs
- Rust (latest stable)
- PostgreSQL
- Node.js (for development tools)
- pnpm (package manager)
The project follows clean architecture principles, organized in layers:
src/
βββ domain/ # Enterprise business rules
β βββ entities/ # Business objects and DTOs
β β βββ product.rs # Product entity and DTOs
β β βββ user.rs # User entity and DTOs
β βββ repositories/ # Repository interfaces
β
βββ application/ # Application business rules
β βββ error.rs # Application-specific errors
β βββ use_cases/ # Business use cases
β βββ product/ # Product-related use cases
β β βββ create_product.rs
β β βββ delete_product.rs
β β βββ get_product.rs
β β βββ list_products.rs
β β βββ update_product.rs
β βββ user/ # User-related use cases
β βββ create_user.rs
β βββ delete_user.rs
β βββ get_user.rs
β βββ list_users.rs
β βββ update_user.rs
β
βββ infrastructure/ # External implementations
β βββ repositories/ # Repository implementations
β β βββ product.rs # Product repository PostgreSQL impl
β β βββ user.rs # User repository PostgreSQL impl
β βββ database/ # Database-specific code
β βββ migrations/ # SQL migrations
β
βββ interfaces/ # Interface adapters
β βββ api/ # REST API
β βββ controllers/ # Request handlers
β βββ middleware/ # API middleware
β βββ routes.rs # Route definitions
β βββ docs.rs # OpenAPI documentation
β
βββ config/ # Configuration
βββ app.rs # Application configuration
βββ database.rs # Database configuration
βββ environment.rs # Environment variables
βββ logger.rs # Logging configuration
- Contains enterprise-wide business rules
- Defines entities and repository interfaces
- No dependencies on other layers
- Pure business logic
- Contains application-specific business rules
- Implements use cases using domain entities
- Depends only on domain layer
- Orchestrates domain objects
- Implements interfaces defined in domain layer
- Contains database implementations
- Handles external concerns (database, external services)
- Depends on domain and application layers
- Contains controllers and presenters
- Handles HTTP requests and responses
- Implements API endpoints
- Uses application use cases
- Manages application configuration
- Handles environment variables
- Sets up database connections
- Configures logging
Product
: Represents product data and operationsUser
: Represents user data and operations
- Product Management:
- Create, Read, Update, Delete operations
- List all products
- Input validation and business rules
- User Management:
- User registration and management
- CRUD operations for users
- User data validation
- Products API:
POST /api/products
- Create productGET /api/products
- List productsGET /api/products/{id}
- Get productPUT /api/products/{id}
- Update productDELETE /api/products/{id}
- Delete product
- Users API:
POST /api/users
- Create userGET /api/users
- List usersGET /api/users/{id}
- Get userPUT /api/users/{id}
- Update userDELETE /api/users/{id}
- Delete user
- PostgreSQL for data persistence
- SQLx for type-safe database operations
- Migration-based schema management
- Clone the repository:
git clone https://github.com/yourusername/rust-actix-clean-starter
cd rust-actix-clean-starter
- Install dependencies:
# Install Rust dependencies
cargo fetch
# Install development tools
pnpm install
- Set up environment:
# Copy example environment file
cp .env.example .env
# Edit .env with your configuration
```bash
4. Set up database:
```bash
# Make the setup script executable
chmod +x scripts/setup_db.sh
# Run database setup
./scripts/setup_db.sh
- Run the application:
cargo run
The API will be available at http://localhost:8080
Swagger UI at http://localhost:8080/swagger-ui/
We use rustfmt and prettier for code formatting:
# Format Rust code
cargo fmt
# Format other files
pnpm fmt
This project uses husky for git hooks:
- pre-commit: Formats code and runs clippy
- commit-msg: Validates commit message format
- pre-push: Runs build and clippy checks
- post-merge: Updates dependencies
We follow conventional commits specification:
feat
: New featurefix
: Bug fixdocs
: Documentationstyle
: Formattingrefactor
: Code restructuringperf
: Performance improvementtest
: Adding testschore
: Maintenance
Example:
git commit -m "feat(auth): add user authentication endpoint"
- Rust CI: Builds and checks code on push/PR
- Issue Branch: Creates branches automatically when issues are assigned
- Issue Autolink: Links PRs to issues automatically
Variable | Description | Default |
---|---|---|
SERVER_HOST | Server host address | 127.0.0.1 |
SERVER_PORT | Server port | 8080 |
DATABASE_URL | PostgreSQL connection URL | postgres://postgres:postgres@localhost:5432/rust_clean_db |
RUST_LOG | Logging level | debug |
API documentation is available through Swagger UI at /swagger-ui/
when the application is running.
# Run all tests
cargo test
# Run specific test
cargo test test_name
- Create an issue (or pick an existing one)
- Get assigned to the issue (branch will be created automatically)
- Make your changes
- Create a PR (will be linked to issue automatically)
- Wait for review and CI checks
This project is licensed under the MIT License - see the LICENSE file for details.