3 releases
| 0.1.2 | Dec 24, 2025 |
|---|---|
| 0.1.1 | Dec 24, 2025 |
| 0.1.0 | Dec 18, 2025 |
#517 in Command line utilities
70KB
1.5K
SLoC
RVY - Rust Code Generator
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)
cargo install rvy
From source
git clone https://github.com/rvy-reverny/rvy.git
cd rvy
cargo install --path .
Verify installation
rvy --help
Usage
Create a new project
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)
# 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.rswith 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.rsanddocs/user_USAGE.md - โ
Auto-updated
main.rswith routes and Swagger UI
Generate individual components
# 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
rvy new project my_api
cd my_api
2. Generate your first entity
rvy new-all product
3. Set up database connection
Create a .env file:
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
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
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
# 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:
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 recordsGET /{entity}s/{id}- Get record by IDPOST /{entity}s- Create new recordPUT /{entity}s/{id}- Update recordDELETE /{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
| Database | Connection String Example |
|---|---|
| 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!
// 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:
#[utoipa::path(
get,
path = "/products",
responses(/* ... */),
security(("bearer_auth" = [])) // ๐ Requires authentication
)]
To test with Swagger UI:
- Click Authorize button ๐
- Enter:
Bearer your-token-here - Click Authorize
- All requests will include the token
๐ ๏ธ Technology Stack
- Web Framework: Axum 0.7
- Async Runtime: Tokio
- Database: SQLx 0.8, MongoDB 3.1
- OpenAPI: utoipa 5.4 (OpenAPI 3.1.0)
- Swagger UI: utoipa-swagger-ui 8.1
- Serialization: serde
- Date/Time: chrono
- Environment: dotenvy
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the 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
Dependencies
~1โ1.5MB
~28K SLoC