An open API service indexing awesome lists of open source software.

https://github.com/maulanasdqn/axum-sqlx-starterkit

Axum SQLX Starterkit
https://github.com/maulanasdqn/axum-sqlx-starterkit

postgresql rest-api rust sqlx sqlx-cli

Last synced: 4 months ago
JSON representation

Axum SQLX Starterkit

Awesome Lists containing this project

README

          

# Axum SQLx Starter Kit

A production-ready Rust web API starter kit built with Axum and SQLx, featuring a modular architecture and comprehensive user management system.

## ๐Ÿš€ Features

- **Modern Rust Web Framework**: Built with [Axum](https://github.com/tokio-rs/axum) for high-performance async web services
- **Database Integration**: [SQLx](https://github.com/launchbadge/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

## ๐Ÿ“ Project Structure

```
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
```

## ๐Ÿ› ๏ธ Prerequisites

- [Rust](https://rustup.rs/) (latest stable version)
- [PostgreSQL](https://www.postgresql.org/) (version 12 or higher)
- [SQLx CLI](https://github.com/launchbadge/sqlx/tree/master/sqlx-cli) for migrations

## ๐Ÿ“ฆ Installation

1. **Clone the repository**

```bash
git clone
cd axum-sqlx-starterkit
```

2. **Install SQLx CLI**

```bash
cargo install sqlx-cli --no-default-features --features postgres
```

3. **Set up environment variables**

```bash
cp .env.example .env
```

Edit `.env` with your database configuration:

```env
DATABASE_URL=postgresql://username:password@localhost/axum_sqlx_starterkit_db
```

4. **Create and setup database**

```bash
# Create database
sqlx database create

# Run migrations
sqlx migrate run --source api-migration/src
```

5. **Build the project**
```bash
cargo build
```

## ๐Ÿš€ Running the Application

```bash
cargo run
```

The API will be available at `http://localhost:3000` (or your configured port).

## ๐Ÿ—„๏ธ Database Schema

The starter kit includes a comprehensive user management system:

### Tables

- **`app_users`**: User accounts with authentication details
- **`app_roles`**: Role definitions for authorization
- **`app_permissions`**: Granular permission system
- **`app_role_permissions`**: Many-to-many relationship between roles and permissions

### Key Features

- 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

## ๐Ÿ”ง Development

### Running Migrations

```bash
# Create a new migration
sqlx migrate add --source api-migration/src

# Run pending migrations
sqlx migrate run --source api-migration/src

# Revert last migration
sqlx migrate revert --source api-migration/src
```

### Environment Setup

Use the provided PowerShell script to apply environment variables:

```powershell
.\apply-env.ps1
```

### Code Organization

- **`api-core/`**: Contains core business logic, use cases, and domain models
- **`api-entity/`**: Database entity definitions and data models
- **`api-lib/`**: Shared utilities, database connections, and common functionality
- **`api-middleware/`**: Custom Axum middleware for authentication, logging, etc.
- **`api-migration/`**: All database migration files

## ๐Ÿงช Testing

```bash
# Run all tests
cargo test

# Run tests for a specific crate
cargo test -p api-core
```

## ๐Ÿ“ API Documentation

The API follows RESTful conventions. Key endpoints include:

- `POST /auth/login` - User authentication
- `POST /auth/register` - User registration
- `GET /users` - List users (with pagination)
- `PUT /users/{id}` - Update user information
- `GET /roles` - List available roles
- `GET /permissions` - List available permissions

## ๐Ÿ”’ Security Features

- Password hashing and validation
- Role-based access control (RBAC)
- JWT token authentication (when implemented)
- SQL injection prevention through SQLx
- Input validation and sanitization

## ๐Ÿš€ Deployment

### Docker (Recommended)

```dockerfile
# 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"]
```

### Environment Variables

Ensure these environment variables are set in production:

```env
DATABASE_URL=postgresql://user:pass@host:port/database
RUST_LOG=info
PORT=3000
```

## ๐Ÿค Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ™ Acknowledgments

- [Axum](https://github.com/tokio-rs/axum) - Modern async web framework
- [SQLx](https://github.com/launchbadge/sqlx) - Async SQL toolkit
- [Tokio](https://tokio.rs/) - Async runtime for Rust
- [Serde](https://serde.rs/) - Serialization framework

## ๐Ÿ“ž Support

If you have any questions or need help, please:

1. Check the [documentation](docs/)
2. Search existing [issues](issues/)
3. Create a new [issue](issues/new) if needed

---

**Happy coding! ๐Ÿฆ€**