# CLAUDE.md This file provides guidance to **Claude Code** (claude.ai/code) when working with code in this repository. > **Note for AI Assistants**: This file is specifically for Claude Code. If you're using Cursor IDE, refer to the `.cursor/rules/` directory for detailed rule files. Both systems share core principles but are optimized for their respective workflows. > > **Maintaining Instructions**: When updating AI instructions, see [.AI_INSTRUCTIONS_SYNC.md](.AI_INSTRUCTIONS_SYNC.md) for synchronization guidelines between CLAUDE.md and .cursor/rules/. ## Project Overview Coolify is an open-source, self-hostable platform for deploying applications and managing servers - an alternative to Heroku/Netlify/Vercel. It's built with Laravel (PHP) and uses Docker for containerization. ## Development Commands ### Frontend Development - `npm run dev` - Start Vite development server for frontend assets - `npm run build` - Build frontend assets for production ### Backend Development Only run artisan commands inside "coolify" container when in development. - `php artisan serve` - Start Laravel development server - `php artisan migrate` - Run database migrations - `php artisan queue:work` - Start queue worker for background jobs - `php artisan horizon` - Start Laravel Horizon for queue monitoring - `php artisan tinker` - Start interactive PHP REPL ### Code Quality - `./vendor/bin/pint` - Run Laravel Pint for code formatting - `./vendor/bin/phpstan` - Run PHPStan for static analysis - `./vendor/bin/pest` - Run Pest tests (unit tests only, without database) ### Running Tests **IMPORTANT**: Tests that require database connections MUST be run inside the Docker container: - **Inside Docker**: `docker exec coolify php artisan test` (for feature tests requiring database) - **Outside Docker**: `./vendor/bin/pest tests/Unit` (for pure unit tests without database dependencies) - Unit tests should use mocking and avoid database connections - Feature tests that require database must be run in the `coolify` container ## Architecture Overview ### Technology Stack - **Backend**: Laravel 12 (PHP 8.4) - **Frontend**: Livewire 3.5+ with Alpine.js and Tailwind CSS 4.1+ - **Database**: PostgreSQL 15 (primary), Redis 7 (cache/queues) - **Real-time**: Soketi (WebSocket server) - **Containerization**: Docker & Docker Compose - **Queue Management**: Laravel Horizon ### Key Components #### Core Models - `Application` - Deployed applications with Git integration (74KB, highly complex) - `Server` - Remote servers managed by Coolify (46KB, complex) - `Service` - Docker Compose services (58KB, complex) - `Database` - Standalone database instances (PostgreSQL, MySQL, MongoDB, Redis, etc.) - `Team` - Multi-tenancy support - `Project` - Grouping of environments and resources - `Environment` - Environment isolation (staging, production, etc.) #### Job System - Uses Laravel Horizon for queue management - Key jobs: `ApplicationDeploymentJob`, `ServerCheckJob`, `DatabaseBackupJob` - `ServerManagerJob` and `ServerConnectionCheckJob` handle job scheduling #### Deployment Flow 1. Git webhook triggers deployment 2. `ApplicationDeploymentJob` handles build and deployment 3. Docker containers are managed on target servers 4. Proxy configuration (Nginx/Traefik) is updated #### Server Management - SSH-based server communication via `ExecuteRemoteCommand` trait - Docker installation and management - Proxy configuration generation - Resource monitoring and cleanup ### Directory Structure - `app/Actions/` - Domain-specific actions (Application, Database, Server, etc.) - `app/Jobs/` - Background queue jobs - `app/Livewire/` - Frontend components (full-stack with Livewire) - `app/Models/` - Eloquent models - `app/Rules/` - Custom validation rules - `app/Http/Middleware/` - HTTP middleware - `bootstrap/helpers/` - Helper functions for various domains - `database/migrations/` - Database schema evolution - `routes/` - Application routing (web.php, api.php, webhooks.php, channels.php) - `resources/views/livewire/` - Livewire component views - `tests/` - Pest tests (Feature and Unit) ## Development Guidelines ### Frontend Philosophy Coolify uses a **server-side first** approach with minimal JavaScript: - **Livewire** for server-side rendering with reactive components - **Alpine.js** for lightweight client-side interactions - **Tailwind CSS** for utility-first styling with dark mode support - **Enhanced Form Components** with built-in authorization system - Real-time updates via WebSocket without page refreshes ### Form Authorization Pattern **IMPORTANT**: When creating or editing forms, ALWAYS include authorization: #### For Form Components (Input, Select, Textarea, Checkbox, Button): Use `canGate` and `canResource` attributes for automatic authorization: ```html ... Save ``` #### For Modal Components: Wrap with `@can` directives: ```html @can('update', $resource) ... ... @endcan ``` #### In Livewire Components: Always add the `AuthorizesRequests` trait and check permissions: ```php use Illuminate\Foundation\Auth\Access\AuthorizesRequests; class MyComponent extends Component { use AuthorizesRequests; public function mount() { $this->authorize('view', $this->resource); } public function update() { $this->authorize('update', $this->resource); // ... update logic } } ``` ### Livewire Component Structure - Components located in `app/Livewire/` - Views in `resources/views/livewire/` - State management handled on the server - Use wire:model for two-way data binding - Dispatch events for component communication - **CRITICAL**: Livewire component views **MUST** have exactly ONE root element. ALL content must be contained within this single root element. Placing ANY elements (`