A production-ready Rust web API starter kit built with Axum and SQLx, featuring a modular architecture and comprehensive user management system.
- Modern Rust Web Framework: Built with Axum for high-performance async web services
- Database Integration: SQLx with PostgreSQL support and compile-time checked queries
- Modular Architecture: Clean separation of concerns with multiple crates
- User Management: Complete authentication system with roles and permissions
- Database Migrations: Automated schema management with SQLx migrations
- Environment Configuration: Flexible configuration management
- Production Ready: Structured for scalability and maintainability
axum-sqlx-starterkit/
├── api-core/ # Core business logic and domain models
├── api-entity/ # Database entities and models
├── api-lib/ # Shared libraries and utilities
│ └── src/sqlx/ # Database connection and configuration
├── api-middleware/ # Custom middleware components
├── api-migration/ # Database migration scripts
├── .env.example # Environment variables template
├── .env # Environment variables (create from .env.example)
└── Cargo.toml # Workspace configuration
- Rust (latest stable version)
- PostgreSQL (version 12 or higher)
- SQLx CLI for migrations
-
Clone the repository
git clone <repository-url> cd axum-sqlx-starterkit
-
Install SQLx CLI
cargo install sqlx-cli --no-default-features --features postgres
-
Set up environment variables
cp .env.example .env
Edit
.env
with your database configuration:DATABASE_URL=postgresql://username:password@localhost/axum_sqlx_starterkit_db
-
Create and setup database
# Create database sqlx database create # Run migrations sqlx migrate run --source api-migration/src
-
Build the project
cargo build
cargo run
The API will be available at http://localhost:3000
(or your configured port).
The starter kit includes a comprehensive user management system:
app_users
: User accounts with authentication detailsapp_roles
: Role definitions for authorizationapp_permissions
: Granular permission systemapp_role_permissions
: Many-to-many relationship between roles and permissions
- UUID primary keys for all tables
- Automatic timestamp tracking (
created_at
,updated_at
) - Foreign key constraints for data integrity
- Unique constraints on email and phone numbers
# Create a new migration
sqlx migrate add --source api-migration/src <migration_name>
# Run pending migrations
sqlx migrate run --source api-migration/src
# Revert last migration
sqlx migrate revert --source api-migration/src
Use the provided PowerShell script to apply environment variables:
.\apply-env.ps1
api-core/
: Contains core business logic, use cases, and domain modelsapi-entity/
: Database entity definitions and data modelsapi-lib/
: Shared utilities, database connections, and common functionalityapi-middleware/
: Custom Axum middleware for authentication, logging, etc.api-migration/
: All database migration files
# Run all tests
cargo test
# Run tests for a specific crate
cargo test -p api-core
The API follows RESTful conventions. Key endpoints include:
POST /auth/login
- User authenticationPOST /auth/register
- User registrationGET /users
- List users (with pagination)PUT /users/{id}
- Update user informationGET /roles
- List available rolesGET /permissions
- List available permissions
- Password hashing and validation
- Role-based access control (RBAC)
- JWT token authentication (when implemented)
- SQL injection prevention through SQLx
- Input validation and sanitization
# Example Dockerfile structure
FROM rust:1.70 as builder
WORKDIR /app
COPY . .
RUN cargo build --release
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y ca-certificates
COPY --from=builder /app/target/release/axum-sqlx-starterkit /usr/local/bin/
CMD ["axum-sqlx-starterkit"]
Ensure these environment variables are set in production:
DATABASE_URL=postgresql://user:pass@host:port/database
RUST_LOG=info
PORT=3000
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Axum - Modern async web framework
- SQLx - Async SQL toolkit
- Tokio - Async runtime for Rust
- Serde - Serialization framework
If you have any questions or need help, please:
- Check the documentation
- Search existing issues
- Create a new issue if needed
Happy coding! 🦀