use anyhow::Result;
use colored::*;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use super::CommandUtils;
pub async fn handle(name: String, path: Option<String>, template: Option<String>, git: bool) -> Result<()> {
let project_path = if let Some(p) = path {
PathBuf::from(p).join(&name)
} else {
PathBuf::from(&name)
};
if project_path.exists() {
anyhow::bail!("Directory '{}' already exists", project_path.display());
}
CommandUtils::info(&format!("Creating new Rustisan application '{}'...", name));
fs::create_dir_all(&project_path)?;
create_project_structure(&project_path, &name, template.as_deref()).await?;
if git {
initialize_git(&project_path)?;
}
CommandUtils::success(&format!("Successfully created Rustisan application '{}'", name));
println!("\n{}", "Next steps:".bold());
println!(" cd {}", name);
println!(" rustisan config:generate-key # Generate application key");
println!(" # Configure your database in rustisan.toml");
println!(" rustisan migrate # Run database migrations");
println!(" rustisan serve # Start development server");
Ok(())
}
async fn create_project_structure(path: &Path, name: &str, template: Option<&str>) -> Result<()> {
create_main_cargo_toml(path, name)?;
create_rustisan_config(path)?;
create_gitignore(path)?;
create_src_structure(path)?;
create_directory_structure(path)?;
create_main_rs(path, name)?;
create_readme(path, name)?;
if let Some(template_name) = template {
apply_template(path, template_name).await?;
}
Ok(())
}
fn create_main_cargo_toml(path: &Path, name: &str) -> Result<()> {
let cargo_toml = format!(r#"[package]
name = "{}"
version = "0.1.0"
edition = "2024"
authors = ["Your Name <your.email@example.com>"]
description = "A Rustisan web application"
[dependencies]
rustisan-core = "0.0.1"
tokio = {{ version = "1.0", features = ["full"] }}
serde_json = "1.0"
tracing = "0.1"
chrono = {{ version = "0.4", features = ["serde"] }}
[dev-dependencies]
tokio-test = "0.4"
[[bin]]
name = "{}"
path = "src/main.rs"
"#, name, name);
fs::write(path.join("Cargo.toml"), cargo_toml)?;
Ok(())
}
fn create_rustisan_config(path: &Path) -> Result<()> {
let config = r#"[app]
name = "Rustisan App"
env = "development"
debug = true
url = "http://localhost:3000"
timezone = "UTC"
locale = "en"
key = ""
cors_enabled = true
[server]
host = "127.0.0.1"
port = 3000
timeout = 60
max_connections = 1000
https_enabled = false
[database]
default = "default"
[database.connections.default]
driver = "mysql"
host = "localhost"
port = 3306
database = "rustisan_app"
username = "root"
password = ""
charset = "utf8mb4"
pool_min = 1
pool_max = 10
timeout = 30
[cache]
default = "memory"
ttl = 3600
[session]
driver = "cookie"
lifetime = 120
expire_on_close = false
encrypt = true
cookie_name = "rustisan_session"
cookie_path = "/"
cookie_secure = false
cookie_http_only = true
[logging]
level = "info"
default = "console"
# Additional configuration sections can be added here
# For example:
# [mail]
# driver = "smtp"
# host = "localhost"
# port = 587
# encryption = "tls"
# username = ""
# password = ""
# [redis]
# host = "localhost"
# port = 6379
# password = ""
# database = 0
# [api]
# rate_limit_enabled = true
# rate_limit_max_requests = 60
# rate_limit_window = 60
# default_version = "v1"
# prefix = "api"
"#;
fs::write(path.join("rustisan.toml"), config)?;
Ok(())
}
fn create_gitignore(path: &Path) -> Result<()> {
let gitignore = r#"# Rust
/target/
Cargo.lock
# IDE
.vscode/
.idea/
*.swp
*.swo
*~
# OS
.DS_Store
Thumbs.db
# Environment
# No .env files needed - configuration is in rustisan.toml
# Logs
*.log
logs/
# Database
*.db
*.sqlite
*.sqlite3
# Cache
storage/cache/
storage/sessions/
storage/logs/
# Build artifacts
dist/
build/
"#;
fs::write(path.join(".gitignore"), gitignore)?;
Ok(())
}
fn create_src_structure(path: &Path) -> Result<()> {
let src_path = path.join("src");
fs::create_dir_all(&src_path)?;
fs::create_dir_all(src_path.join("controllers"))?;
fs::create_dir_all(src_path.join("models"))?;
fs::create_dir_all(src_path.join("middleware"))?;
fs::create_dir_all(src_path.join("requests"))?;
fs::create_dir_all(src_path.join("resources"))?;
fs::create_dir_all(src_path.join("services"))?;
fs::create_dir_all(src_path.join("jobs"))?;
fs::create_dir_all(src_path.join("events"))?;
fs::create_dir_all(src_path.join("listeners"))?;
create_controllers_module(&src_path)?;
fs::write(src_path.join("models").join("mod.rs"), "//! Application models\n")?;
fs::write(src_path.join("middleware").join("mod.rs"), "//! Application middleware\n")?;
fs::write(src_path.join("requests").join("mod.rs"), "//! Form request validators\n")?;
fs::write(src_path.join("resources").join("mod.rs"), "//! API resources\n")?;
fs::write(src_path.join("services").join("mod.rs"), "//! Application services\n")?;
fs::write(src_path.join("jobs").join("mod.rs"), "//! Background jobs\n")?;
fs::write(src_path.join("events").join("mod.rs"), "//! Application events\n")?;
fs::write(src_path.join("listeners").join("mod.rs"), "//! Event listeners\n")?;
fs::write(src_path.join("routes.rs"), "\n")?;
Ok(())
}
fn create_directory_structure(path: &Path) -> Result<()> {
fs::create_dir_all(path.join("database").join("migrations"))?;
fs::create_dir_all(path.join("database").join("seeders"))?;
fs::create_dir_all(path.join("database").join("factories"))?;
fs::create_dir_all(path.join("storage").join("logs"))?;
fs::create_dir_all(path.join("storage").join("cache"))?;
fs::create_dir_all(path.join("storage").join("sessions"))?;
fs::create_dir_all(path.join("storage").join("uploads"))?;
fs::create_dir_all(path.join("resources").join("views"))?;
fs::create_dir_all(path.join("resources").join("assets"))?;
fs::create_dir_all(path.join("config"))?;
fs::create_dir_all(path.join("tests").join("unit"))?;
fs::create_dir_all(path.join("tests").join("integration"))?;
fs::create_dir_all(path.join("routes"))?;
Ok(())
}
fn create_controllers_module(src_path: &Path) -> Result<()> {
let controllers_path = src_path.join("controllers");
let controllers_mod = r#"//! Application controllers
pub mod user_controller;
pub use user_controller::UserController;
"#;
fs::write(controllers_path.join("mod.rs"), controllers_mod)?;
let user_controller = r#"//! User controller for handling user-related HTTP requests
//!
//! This controller demonstrates the Laravel-like structure for handling
//! user operations in a Rustisan application.
use std::sync::Arc;
use serde_json::json;
use rustisan_core::{Request, Response, Result};
/// UserController handles all user-related HTTP requests
pub struct UserController;
impl UserController {
/// Creates a new UserController instance
pub fn new() -> Self {
Self
}
/// Display a listing of users (GET /users)
pub async fn index(&self) -> Result<Response> {
// In a real application, this would fetch users from a database
let users = vec![
json!({
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"created_at": "2024-01-01T00:00:00Z"
}),
json!({
"id": 2,
"name": "Jane Smith",
"email": "jane@example.com",
"created_at": "2024-01-02T00:00:00Z"
}),
json!({
"id": 3,
"name": "Bob Johnson",
"email": "bob@example.com",
"created_at": "2024-01-03T00:00:00Z"
})
];
Response::json(json!({
"users": users,
"count": users.len(),
"message": "Users retrieved successfully"
}))
}
/// Show a specific user (GET /users/{id})
pub async fn show(&self, id: u32) -> Result<Response> {
// In a real application, this would fetch the user from a database
let user = json!({
"id": id,
"name": "User Name",
"email": format!("user{}@example.com", id),
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z"
});
Response::json(json!({
"user": user,
"message": format!("User {} retrieved successfully", id)
}))
}
/// Store a new user (POST /users)
pub async fn store(&self, _request: Request) -> Result<Response> {
// In a real application, this would validate the request and save to database
// For demonstration, we'll just return a success response
let new_user = json!({
"id": 999,
"name": "New User",
"email": "newuser@example.com",
"created_at": chrono::Utc::now().to_rfc3339(),
"updated_at": chrono::Utc::now().to_rfc3339()
});
Response::created(json!({
"user": new_user,
"message": "User created successfully"
}))
}
/// Update an existing user (PUT /users/{id})
pub async fn update(&self, id: u32, _request: Request) -> Result<Response> {
// In a real application, this would validate the request and update the database
let updated_user = json!({
"id": id,
"name": "Updated User Name",
"email": format!("updated{}@example.com", id),
"created_at": "2024-01-01T00:00:00Z",
"updated_at": chrono::Utc::now().to_rfc3339()
});
Response::json(json!({
"user": updated_user,
"message": format!("User {} updated successfully", id)
}))
}
/// Delete a user (DELETE /users/{id})
pub async fn destroy(&self, id: u32) -> Result<Response> {
// In a real application, this would delete the user from the database
Response::json(json!({
"message": format!("User {} deleted successfully", id),
"deleted_id": id
}))
}
/// Get user statistics (GET /users/stats)
pub async fn stats(&self) -> Result<Response> {
let stats = json!({
"total_users": 150,
"active_users": 120,
"inactive_users": 30,
"users_created_today": 5,
"users_created_this_week": 25,
"users_created_this_month": 100
});
Response::json(json!({
"statistics": stats,
"message": "User statistics retrieved successfully"
}))
}
/// Search users (GET /users/search?q=term)
pub async fn search(&self, _request: Request) -> Result<Response> {
// In a real application, this would search the database
let search_term = "sample"; // In a real implementation, extract from request
let results = vec![
json!({
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"relevance": 0.95
}),
json!({
"id": 2,
"name": "Jane Smith",
"email": "jane@example.com",
"relevance": 0.85
})
];
Response::json(json!({
"results": results,
"search_term": search_term,
"count": results.len(),
"message": "Search completed successfully"
}))
}
}
impl Default for UserController {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_user_controller_index() {
let controller = UserController::new();
let response = controller.index().await;
assert!(response.is_ok());
}
#[tokio::test]
async fn test_user_controller_show() {
let controller = UserController::new();
let response = controller.show(1).await;
assert!(response.is_ok());
}
#[tokio::test]
async fn test_user_controller_stats() {
let controller = UserController::new();
let response = controller.stats().await;
assert!(response.is_ok());
}
}
"#;
fs::write(controllers_path.join("user_controller.rs"), user_controller)?;
Ok(())
}
fn create_main_rs(path: &Path, name: &str) -> Result<()> {
let main_rs = format!(r#"//! {} - A Rustisan web application
//!
//! This is the main entry point for the application.
use std::net::SocketAddr;
use std::sync::Arc;
use rustisan_core::{{
app::Application,
config::Config,
init_logging,
routing::create_success_response,
Response, Result,
}};
use serde_json::json;
use tracing::{{error, info, warn}};
mod controllers;
mod routes;
use controllers::UserController;
#[tokio::main]
async fn main() -> Result<()> {{
// Initialize logging
init_logging();
info!("🚀 Starting {} Application...");
// Create application with configuration
let mut app = create_application().await?;
// Register all routes
register_routes(&mut app).await?;
// Start the server
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
info!("🌐 Server starting on http://{{}}", addr);
info!("📚 Available endpoints:");
print_available_routes();
// Serve the application
if let Err(e) = app.serve(addr).await {{
error!("❌ Server error: {{}}", e);
return Err(e);
}}
Ok(())
}}
/// Creates and configures the Rustisan application
async fn create_application() -> Result<Application> {{
info!("🔧 Configuring Rustisan application...");
// Load configuration
let mut config = Config::default();
config.app_name = "{} Application".to_string();
config.app_env = "development".to_string();
config.app_debug = true;
// Create application
let mut app = Application::with_config(config);
// Set application state
app.set_state("version", "0.1.0");
app.set_state("author", "Rustisan Team");
app.set_state("description", "A {} application built with Rustisan framework");
info!("✅ Application configured successfully");
Ok(app)
}}
/// Registers all application routes
async fn register_routes(app: &mut Application) -> Result<()> {{
info!("📍 Registering application routes...");
let router = app.router();
// Basic routes
router.get("/", || async {{
create_success_response(json!({{
"message": "Welcome to {} Application!",
"version": "0.1.0",
"status": "success",
"framework": "Rustisan",
"inspired_by": "Laravel",
"documentation": "Visit /docs for API documentation",
"health_check": "Visit /health for health status",
"timestamp": chrono::Utc::now()
}})).unwrap_or_else(|_| Response::internal_error("Failed to create response").unwrap())
}});
// Health check route
router.get("/health", || async {{
create_success_response(json!({{
"status": "healthy",
"service": "{}",
"version": "0.1.0",
"uptime": "running",
"timestamp": chrono::Utc::now(),
"checks": {{
"database": "ok",
"memory": "ok",
"disk": "ok"
}}
}})).unwrap_or_else(|_| Response::internal_error("Health check failed").unwrap())
}});
// Documentation route
router.get("/docs", || async {{
create_success_response(json!({{
"documentation": {{
"title": "{} API Documentation",
"version": "1.0.0",
"description": "A comprehensive API built with the Rustisan framework",
"framework": "Rustisan (inspired by Laravel)",
"endpoints": {{
"basic": {{
"GET /": "Welcome message and application info",
"GET /health": "Application health check",
"GET /docs": "This documentation"
}},
"users": {{
"GET /users": "List all users",
"GET /users/:id": "Get specific user by ID",
"POST /users": "Create a new user",
"PUT /users/:id": "Update existing user",
"DELETE /users/:id": "Delete user",
"GET /users/stats": "Get user statistics"
}},
"api_v1": {{
"description": "All user endpoints are also available with /api/v1 prefix",
"base_url": "/api/v1",
"GET /api/v1/status": "API status information"
}}
}},
"response_format": {{
"success": {{
"structure": "{{ data: any, message?: string }}",
"example": {{
"data": {{"key": "value"}},
"message": "Operation successful"
}}
}},
"error": {{
"structure": "{{ error: {{ code: string, message: string, status: number }} }}",
"example": {{
"error": {{
"code": "NOT_FOUND",
"message": "Resource not found",
"status": 404
}}
}}
}}
}}
}}
}})).unwrap_or_else(|_| Response::internal_error("Failed to generate documentation").unwrap())
}});
// Create controller instance
let controller = Arc::new(UserController::new());
// User routes - demonstrating Laravel-like resource routing
register_user_routes(router, controller.clone()).await?;
// API v1 routes - demonstrating route groups
register_api_routes(router, controller).await?;
info!("✅ All routes registered successfully");
Ok(())
}}
/// Registers user-related routes
async fn register_user_routes(
router: &mut rustisan_core::routing::Router,
controller: Arc<UserController>,
) -> Result<()> {{
info!("📍 Registering user routes...");
// GET /users - List all users
{{
let ctrl = controller.clone();
router.get("/users", move || {{
let controller = ctrl.clone();
async move {{
match controller.index().await {{
Ok(response) => response,
Err(_) => Response::internal_error("Failed to fetch users").unwrap()
}}
}}
}});
}}
// GET /users/:id - Get specific user
{{
let ctrl = controller.clone();
router.get("/users/:id", move || {{
let controller = ctrl.clone();
async move {{
match controller.show(1).await {{
Ok(response) => response,
Err(_) => Response::internal_error("Failed to fetch user").unwrap()
}}
}}
}});
}}
// GET /users/stats - User statistics
{{
let ctrl = controller.clone();
router.get("/users/stats", move || {{
let controller = ctrl.clone();
async move {{
match controller.stats().await {{
Ok(response) => response,
Err(_) => Response::internal_error("Failed to fetch user statistics").unwrap()
}}
}}
}});
}}
Ok(())
}}
/// Registers API v1 routes using route groups
async fn register_api_routes(
router: &mut rustisan_core::routing::Router,
controller: Arc<UserController>,
) -> Result<()> {{
info!("📍 Registering API v1 routes...");
router.group("/api/v1", |group| {{
// API status endpoint
group.get("/status", move || async {{
create_success_response(json!({{
"api": {{
"name": "{} API",
"version": "v1",
"status": "active",
"uptime": "running",
"framework": "Rustisan",
"endpoints": {{
"users": [
"GET /api/v1/users - List users",
"GET /api/v1/users/:id - Get user",
"POST /api/v1/users - Create user",
"PUT /api/v1/users/:id - Update user",
"DELETE /api/v1/users/:id - Delete user"
]
}},
"features": [
"RESTful API design",
"JSON responses",
"Error handling",
"Request validation",
"Route grouping"
],
"timestamp": chrono::Utc::now()
}}
}})).unwrap_or_else(|_| Response::internal_error("Failed to get API status").unwrap())
}});
// API user endpoints
let api_controller = controller.clone();
{{
let ctrl = api_controller.clone();
group.get("/users", move || {{
let controller = ctrl.clone();
async move {{
match controller.index().await {{
Ok(response) => response,
Err(_) => Response::internal_error("Failed to fetch users via API").unwrap()
}}
}}
}});
}}
{{
let ctrl = api_controller.clone();
group.get_with_id("/users/:id", move |id| {{
let controller = ctrl.clone();
async move {{
match controller.show(id).await {{
Ok(response) => response,
Err(_) => Response::internal_error("Failed to fetch user via API").unwrap()
}}
}}
}});
}}
}});
Ok(())
}}
/// Prints available routes for user reference
fn print_available_routes() {{
println!(" 📋 Route List:");
println!(" ┌─────────────────────────────────────────────────────────────┐");
println!(" │ METHOD │ PATH │ DESCRIPTION │");
println!(" ├─────────────────────────────────────────────────────────────┤");
println!(" │ GET │ / │ Welcome & app info │");
println!(" │ GET │ /health │ Health check │");
println!(" │ GET │ /docs │ API documentation │");
println!(" │ GET │ /users │ List all users │");
println!(" │ GET │ /users/:id │ Get user by ID │");
println!(" │ GET │ /users/stats │ User statistics │");
println!(" │ GET │ /api/v1/status │ API status │");
println!(" │ GET │ /api/v1/users │ List users (API) │");
println!(" │ GET │ /api/v1/users/:id │ Get user (API) │");
println!(" └─────────────────────────────────────────────────────────────┘");
println!();
println!(" 🔗 Quick Links:");
println!(" • Application: http://127.0.0.1:3000/");
println!(" • Health Check: http://127.0.0.1:3000/health");
println!(" • Documentation: http://127.0.0.1:3000/docs");
println!(" • Users API: http://127.0.0.1:3000/users");
println!(" • API Status: http://127.0.0.1:3000/api/v1/status");
println!();
}}
#[cfg(test)]
mod tests {{
use super::*;
#[tokio::test]
async fn test_create_application() {{
let app = create_application().await;
assert!(app.is_ok());
let app = app.unwrap();
assert_eq!(app.config().app_name, "{} Application");
assert!(app.config().is_development());
}}
#[tokio::test]
async fn test_register_routes() {{
let mut app = create_application().await.unwrap();
let result = register_routes(&mut app).await;
assert!(result.is_ok());
}}
#[test]
fn test_print_routes() {{
// Test that the function doesn't panic
print_available_routes();
}}
}}
"#, name, name, name, name, name, name, name, name, name);
fs::write(path.join("src").join("main.rs"), main_rs)?;
Ok(())
}
fn create_readme(path: &Path, name: &str) -> Result<()> {
let readme = format!(r#"# {}
A web application built with Rustisan, a Laravel-inspired web framework for Rust.
## Getting Started
### Prerequisites
- Rust 1.70+ with 2024 edition support
- MySQL or PostgreSQL database
### Installation
1. Clone this repository
2. Configure your environment in `rustisan.toml`
3. Install dependencies:
```bash
cargo build
```
### Running the Application
```bash
# Development server
rustisan serve
# Or with cargo
cargo run
```
The application will be available at `http://localhost:3000`.
### Database Setup
```bash
# Create database
rustisan db:create
# Run migrations
rustisan migrate
# Seed database (optional)
rustisan seed
```
### Available Commands
```bash
# Generate a controller
rustisan make:controller UserController
# Generate a model
rustisan make:model User --migration
# Generate a migration
rustisan make:migration create_users_table
# View all routes
rustisan route:list
# Run tests
rustisan test
```
## Project Structure
```
src/
├── controllers/ # HTTP controllers
├── models/ # Data models
├── middleware/ # HTTP middleware
├── requests/ # Form request validators
├── resources/ # API resources
├── services/ # Business logic services
├── jobs/ # Background jobs
├── events/ # Application events
└── listeners/ # Event listeners
database/
├── migrations/ # Database migrations
├── seeders/ # Database seeders
└── factories/ # Model factories
routes/ # Route definitions
resources/ # Views and assets
storage/ # Application storage
tests/ # Test files
```
## Documentation
For more information about Rustisan, visit: [https://github.com/lugotardo/rustisan](https://github.com/lugotardo/rustisan)
## License
This project is licensed under the MIT License.
"#, name);
fs::write(path.join("README.md"), readme)?;
Ok(())
}
fn initialize_git(path: &Path) -> Result<()> {
CommandUtils::info("Initializing git repository...");
Command::new("git")
.args(&["init"])
.current_dir(path)
.output()?;
Command::new("git")
.args(&["add", "."])
.current_dir(path)
.output()?;
Command::new("git")
.args(&["commit", "-m", "Initial commit"])
.current_dir(path)
.output()?;
Ok(())
}
async fn apply_template(path: &Path, template: &str) -> Result<()> {
CommandUtils::info(&format!("Applying template '{}'...", template));
match template {
"api" => apply_api_template(path).await?,
"web" => apply_web_template(path).await?,
"minimal" => apply_minimal_template(path).await?,
_ => {
CommandUtils::warning(&format!("Unknown template '{}', using default", template));
}
}
Ok(())
}
async fn apply_api_template(_path: &Path) -> Result<()> {
CommandUtils::info("API template applied");
Ok(())
}
async fn apply_web_template(_path: &Path) -> Result<()> {
CommandUtils::info("Web template applied");
Ok(())
}
async fn apply_minimal_template(_path: &Path) -> Result<()> {
CommandUtils::info("Minimal template applied");
Ok(())
}