# RVY - Rust Code Generator
[](https://opensource.org/licenses/MIT)
[](https://www.rust-lang.org/)
A powerful CLI tool for scaffolding production-ready Rust projects with **Clean Architecture**, **REST APIs**, **OpenAPI/Swagger documentation**, and **multiple database support**.
## โจ Features
- ๐๏ธ **Clean Architecture** - Service โ Usecase โ Repository โ Adapter pattern
- ๐ **REST API** - Full CRUD operations with Axum framework
- ๐ **OpenAPI 3.1.0** - Auto-generated Swagger documentation with Authorization
- ๐๏ธ **Multi-Database** - Runtime switching between PostgreSQL, MySQL, SQLite, MongoDB
- ๐ **Bearer Auth** - Built-in authorization support in all endpoints
- โก **Async/Await** - Tokio-based async runtime
- ๐ฏ **Type-Safe** - Full Rust type safety with SQLx compile-time checks
- ๐ฆ **Zero Configuration** - Works out of the box with sensible defaults
## Installation
### From crates.io (Recommended)
```bash
cargo install rvy
```
### From source
```bash
git clone https://github.com/rvy-reverny/rvy.git
cd rvy
cargo install --path .
```
### Verify installation
```bash
rvy --help
```
## Usage
### Create a new project
```bash
rvy new project my_app
```
This creates a new project with the following structure:
```
my_app/
โโโ Cargo.toml
โโโ src/
โ โโโ main.rs
โ โโโ service/
โ โโโ usecase/
โ โโโ repository/
โ โโโ data/
```
### Generate all layers at once (Recommended)
```bash
# Generate complete entity with all layers, database adapters, and REST API
rvy new-all user
```
This generates:
- โ
Service layer: `src/service/user_service.rs`
- โ
Usecase layer: `src/usecase/user_usecase.rs`
- โ
Repository trait: `src/repository/user.rs`
- โ
Data model: `src/data/user_data.rs`
- โ
REST API Handler: `src/handler/user_handler.rs` with OpenAPI annotations
- โ
Database adapters: `src/adapter/user_{postgres,mysql,mongodb,sqlite}.rs`
- โ
Factory pattern: `src/factory/user_factory.rs`
- โ
Database config: `src/config/database.rs`
- โ
Usage examples: `examples/user_example.rs` and `docs/user_USAGE.md`
- โ
Auto-updated `main.rs` with routes and Swagger UI
### Generate individual components
```bash
# Generate specific layers
rvy gen service user
rvy gen usecase user
rvy gen repository user
rvy gen data user
# Generate REST API handler with OpenAPI docs
rvy gen handler user
# Generate database adapters
rvy gen adapter user
# Generate factory for runtime DB selection
rvy gen factory user
```
## ๐ Quick Start
### 1. Create a new project
```bash
rvy new project my_api
cd my_api
```
### 2. Generate your first entity
```bash
rvy new-all product
```
### 3. Set up database connection
Create a `.env` file:
```env
DATABASE_URL=postgres://user:password@localhost:5432/mydb
# Or use other databases:
# DATABASE_URL=mysql://user:password@localhost:3306/mydb
# DATABASE_URL=sqlite://data.db
# DATABASE_URL=mongodb://localhost:27017/mydb
```
### 4. Run the application
```bash
cargo run
```
### 5. Access Swagger UI
Open your browser: **http://127.0.0.1:3000/swagger-ui**
You'll see:
- ๐ Complete API documentation
- ๐ Authorization button (click to add Bearer token)
- ๐งช Try it out feature for testing endpoints
- ๐ Multiple API specs (one per entity)
## ๐ Generated Project Structure
```text
my_api/
โโโ Cargo.toml
โโโ .env
โโโ src/
โ โโโ main.rs # Auto-configured with routes & Swagger
โ โโโ lib.rs
โ โโโ service/ # Business logic
โ โ โโโ mod.rs
โ โ โโโ product_service.rs
โ โโโ usecase/ # Application use cases
โ โ โโโ mod.rs
โ โ โโโ product_usecase.rs
โ โโโ repository/ # Data access traits
โ โ โโโ mod.rs
โ โ โโโ product.rs
โ โโโ data/ # DTOs with OpenAPI schemas
โ โ โโโ mod.rs
โ โ โโโ product_data.rs
โ โโโ handler/ # REST API with OpenAPI annotations
โ โ โโโ mod.rs
โ โ โโโ product_handler.rs
โ โโโ adapter/ # Database implementations
โ โ โโโ mod.rs
โ โ โโโ product_postgres.rs
โ โ โโโ product_mysql.rs
โ โ โโโ product_mongodb.rs
โ โ โโโ product_sqlite.rs
โ โโโ factory/ # Runtime DB selection
โ โ โโโ mod.rs
โ โ โโโ product_factory.rs
โ โโโ config/
โ โโโ mod.rs
โ โโโ database.rs # DB configuration
โโโ examples/
โ โโโ product_example.rs
โโโ docs/
โโโ product_USAGE.md
```
## ๐ง Options
- `--dry-run`: Preview what will be generated without writing files
- `--force`: Overwrite existing files
## ๐ก Examples
```bash
# Preview generation
rvy new-all user --dry-run
# Force overwrite existing files
rvy gen handler user --force
# Generate multiple entities
rvy new-all product
rvy new-all user
rvy new-all order
```
## ๐๏ธ Architecture
RVY follows **Clean Architecture** principles with clear separation of concerns:
```text
Handler (REST API)
โ
Service (Business Logic)
โ
Usecase (Application Logic)
โ
Repository (Data Access Interface)
โ
Adapter (Database Implementation)
โ
Database (PostgreSQL/MySQL/SQLite/MongoDB)
```
### Layer Responsibilities
- **Handler**: REST API endpoints, request/response handling, OpenAPI documentation
- **Service**: Business rules and domain logic
- **Usecase**: Application-specific business rules
- **Repository**: Data access interface (trait)
- **Adapter**: Concrete database implementations
- **Data**: DTOs with serialization and validation
- **Factory**: Runtime database adapter selection
- **Config**: Application configuration and environment variables
## ๐ API Documentation
Generated APIs include:
### Endpoints (per entity)
- `GET /{entity}s` - Get all records
- `GET /{entity}s/{id}` - Get record by ID
- `POST /{entity}s` - Create new record
- `PUT /{entity}s/{id}` - Update record
- `DELETE /{entity}s/{id}` - Delete record
### OpenAPI Features
- โ
**OpenAPI 3.1.0** specification
- โ
**Bearer Authentication** - Token-based auth on all endpoints
- โ
**Request/Response Schemas** - Full type definitions
- โ
**Example Values** - Sample data for testing
- โ
**Multiple API Specs** - Separate docs per entity
- โ
**Interactive Testing** - Try endpoints directly from Swagger UI
## ๐๏ธ Database Support
### Supported Databases
| PostgreSQL | `postgres://user:pass@localhost:5432/db` |
| MySQL | `mysql://user:pass@localhost:3306/db` |
| SQLite | `sqlite://data.db` |
| MongoDB | `mongodb://localhost:27017/db` |
### Runtime Selection
The database adapter is selected at runtime based on the `DATABASE_URL` environment variable. No need to recompile for different databases!
```rust
// Automatically detected from DATABASE_URL
let config = DatabaseConfig::from_env();
let repository = create_product_repository(&config).await?;
```
## ๐ Authentication
All generated endpoints include Bearer token authentication:
```rust
#[utoipa::path(
get,
path = "/products",
responses(/* ... */),
security(("bearer_auth" = [])) // ๐ Requires authentication
)]
```
To test with Swagger UI:
1. Click **Authorize** button ๐
2. Enter: `Bearer your-token-here`
3. Click **Authorize**
4. All requests will include the token
## ๐ ๏ธ Technology Stack
- **Web Framework**: [Axum](https://github.com/tokio-rs/axum) 0.7
- **Async Runtime**: [Tokio](https://tokio.rs/)
- **Database**: [SQLx](https://github.com/launchbadge/sqlx) 0.8, [MongoDB](https://github.com/mongodb/mongo-rust-driver) 3.1
- **OpenAPI**: [utoipa](https://github.com/juhaku/utoipa) 5.4 (OpenAPI 3.1.0)
- **Swagger UI**: [utoipa-swagger-ui](https://github.com/juhaku/utoipa) 8.1
- **Serialization**: [serde](https://serde.rs/)
- **Date/Time**: [chrono](https://github.com/chronotope/chrono)
- **Environment**: [dotenvy](https://github.com/allan2/dotenvy)
## ๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request
## ๐ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## ๐ Acknowledgments
- Built with โค๏ธ using Rust
- Inspired by Clean Architecture principles
- OpenAPI 3.1.0 specification
- Community feedback and contributions
---
**Made with ๐ฆ Rust**