Project Structure
This guide explains the directory structure and file organization of Azu projects created with the CLI. Understanding this structure will help you navigate and develop your applications more effectively.
Overview
Azu follows a convention-over-configuration approach with a well-defined directory structure that promotes maintainability, testability, and scalability.
Complete Project Structure
When you create a new Azu project with azu new my_app, you get this structure:
my_app/
├── 📁 src/ # Application source code
│ ├── 📄 my_app.cr # Main application module
│ ├── 📄 server.cr # HTTP server configuration
│ ├── 📁 endpoints/ # HTTP endpoints (controllers)
│ │ └── 📁 welcome/
│ │ └── 📄 index_endpoint.cr
│ ├── 📁 models/ # Database models (CQL/Jennifer)
│ │ └── 📄 your_models_goes_here.txt
│ ├── 📁 contracts/ # Request/response contracts
│ │ └── 📁 welcome/
│ │ └── 📄 index_contract.cr
│ ├── 📁 pages/ # Page components (views)
│ │ └── 📁 welcome/
│ │ └── 📄 index_page.cr
│ ├── 📁 services/ # Business logic services
│ ├── 📁 middleware/ # HTTP middleware components
│ ├── 📁 components/ # Reusable live components
│ ├── 📁 validators/ # Custom validation logic
│ ├── 📁 initializers/ # Application startup configuration
│ │ ├── 📄 database.cr
│ │ └── 📄 logger.cr
│ └── 📁 db/ # Database-related files
│ ├── 📄 schema.cr # Database schema definition
│ ├── 📄 seed.cr # Sample data seeding
│ ├── 📁 migrations/ # Database migrations
│ └── 📄 README.md
├── 📁 spec/ # Test files
│ ├── 📄 my_app_spec.cr # Main application tests
│ ├── 📄 spec_helper.cr # Test configuration
│ ├── 📁 endpoints/ # Endpoint tests
│ ├── 📁 models/ # Model tests
│ ├── 📁 services/ # Service tests
│ └── 📁 support/ # Test support files
├── 📁 public/ # Static assets
│ ├── 📁 assets/ # CSS, JS, images
│ │ ├── 📁 css/
│ │ │ ├── 📄 bootstrap.min.css
│ │ │ └── 📄 cover.css
│ │ └── 📁 js/
│ │ └── 📄 bootstrap.min.js
│ └── 📁 templates/ # Jinja2 templates
│ ├── 📄 layout.jinja # Base layout template
│ ├── 📁 helpers/ # Partial templates
│ │ └── 📄 _nav.jinja
│ └── 📁 welcome/
│ └── 📄 index_page.jinja
├── 📁 tasks/ # Custom task definitions
│ └── 📄 taskfile.cr
├── 📁 config/ # Configuration files
├── 📄 shard.yml # Crystal dependencies
├── 📄 README.md # Project documentation
└── 📄 LICENSE # License fileDirectory Details
/src - Application Source Code
/src - Application Source CodeThe heart of your application where all Crystal source code lives.
Main Files
my_app.cr: Main application module that defines routes, middleware, and configurationserver.cr: HTTP server startup and configuration
/src/endpoints - HTTP Endpoints (Controllers)
/src/endpoints - HTTP Endpoints (Controllers)Contains the HTTP request handlers, similar to controllers in other frameworks.
Structure:
Example Endpoint:
/src/models - Database Models
/src/models - Database ModelsContains your data models using CQL ORM or Jennifer ORM.
Example Model:
/src/contracts - Request/Response Contracts
/src/contracts - Request/Response ContractsType-safe request validation and response formatting.
Example Contract:
/src/pages - Page Components (Views)
/src/pages - Page Components (Views)Render HTML responses using templates.
Example Page:
/src/services - Business Logic Services
/src/services - Business Logic ServicesEncapsulate complex business logic following Domain-Driven Design principles.
Example Service:
/src/middleware - HTTP Middleware
/src/middleware - HTTP MiddlewareCustom middleware for request/response processing.
Example Middleware:
/src/components - Live Components
/src/components - Live ComponentsReal-time interactive components for dynamic user interfaces.
Example Component:
/src/validators - Custom Validators
/src/validators - Custom ValidatorsReusable validation logic for models and contracts.
Example Validator:
/src/initializers - Application Initializers
/src/initializers - Application InitializersConfigure various aspects of your application during startup.
Database Initializer (database.cr):
/src/db - Database Files
/src/db - Database Filesschema.cr: Database schema definitionseed.cr: Sample data for developmentmigrations/: Database migration files
/spec - Test Files
/spec - Test FilesMirror the structure of /src for organized testing.
Example Test:
/public - Static Assets
/public - Static Assets/assets: CSS, JavaScript, images, fonts/templates: Jinja2 templates for HTML rendering
Template Structure:
/tasks - Custom Tasks
/tasks - Custom TasksDefine custom command-line tasks for your application.
Example Task:
File Naming Conventions
General Rules
Files:
snake_case.crClasses:
PascalCaseMethods/Variables:
snake_caseConstants:
SCREAMING_SNAKE_CASE
Specific Conventions
Endpoints
File:
src/endpoints/users/index_endpoint.crClass:
Users::IndexEndpoint
Models
File:
src/models/user.crClass:
User
Services
File:
src/services/user_registration_service.crClass:
UserRegistrationService
Contracts
File:
src/contracts/users/create_contract.crStruct:
Users::CreateContract
Pages
File:
src/pages/users/index_page.crClass:
Users::IndexPage
Components
File:
src/components/counter_component.crClass:
CounterComponent
Middleware
File:
src/middleware/authentication_middleware.crClass:
AuthenticationMiddleware
Tests
File:
spec/models/user_spec.crDescribes:
User
Configuration Files
shard.yml - Dependencies
shard.yml - DependenciesEnvironment Configuration
Create environment-specific configuration:
Best Practices
Organization
Group related files in subdirectories
Mirror test structure in
/specUse descriptive names for files and classes
Keep files focused on single responsibilities
Dependencies
Explicit imports in each file
Group imports by source (stdlib, shards, local)
Avoid circular dependencies
Example Import Organization:
Testing
Test files mirror source structure
Use descriptive test names
Group related tests in nested
describeblocksTest both success and failure scenarios
Working with the Structure
Adding New Features
Generate scaffolding:
azu generate scaffold Feature name:stringImplement business logic in services
Add validations in models and contracts
Create custom middleware for cross-cutting concerns
Write comprehensive tests
Refactoring
Extract shared logic into services
Create reusable components for common UI patterns
Use modules for shared behavior
Keep controllers thin by moving logic to services
This structure provides a solid foundation for building maintainable, testable, and scalable Azu applications. The conventions help maintain consistency across projects and make it easier for team members to navigate the codebase.
Next Steps:
Command Reference - Learn about CLI commands
Generators Guide - Generate code efficiently
Development Workflows - Common development patterns
Last updated