2026-07-01 23:15:35 +02:00
|
|
|
|
# LeoCRM v1.0
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
|
|
|
|
|
> Self-hosted CRM for small sales teams (5–25 sales reps).
|
2026-07-01 23:15:35 +02:00
|
|
|
|
> Stack: FastAPI + SQLAlchemy (async) + PostgreSQL + Redis + Alpine.js + Tailwind + Docker + Coolify
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
|
|
|
|
|
## Quick Start (Development)
|
|
|
|
|
|
|
|
|
|
|
|
### 1. Clone and Setup
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-07-01 23:15:35 +02:00
|
|
|
|
git clone <repo-url> leocrm
|
|
|
|
|
|
cd leocrm
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
|
|
|
|
|
# Create virtual environment
|
|
|
|
|
|
python3 -m venv .venv
|
|
|
|
|
|
source .venv/bin/activate
|
|
|
|
|
|
|
|
|
|
|
|
# Install dependencies
|
|
|
|
|
|
pip install -r requirements.txt -r requirements-dev.txt
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 2. Configure Environment
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
cp .env.example .env
|
|
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
# Generate a secure SECRET_KEY (min 32 chars)
|
|
|
|
|
|
python3 -c "import secrets; print('SECRET_KEY=' + secrets.token_urlsafe(48))" >> .env
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
# Edit .env and set DATABASE_URL, REDIS_URL, SECRET_KEY
|
|
|
|
|
|
nano .env
|
2026-06-03 23:51:59 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 3. Initialize Database
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
# Apply migrations
|
|
|
|
|
|
alembic upgrade head
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 4. Run Server
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
# Development with auto-reload
|
|
|
|
|
|
uvicorn app.main:app --reload --port 8000
|
|
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
# Start ARQ worker (for background jobs)
|
|
|
|
|
|
arq app.core.jobs.WorkerSettings
|
2026-06-03 23:51:59 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
Open:
|
|
|
|
|
|
- API: http://localhost:8000
|
|
|
|
|
|
- Swagger UI: http://localhost:8000/docs
|
|
|
|
|
|
- ReDoc: http://localhost:8000/redoc
|
2026-07-01 23:15:35 +02:00
|
|
|
|
- Health: http://localhost:8000/api/v1/health
|
|
|
|
|
|
- Metrics: http://localhost:8000/api/v1/metrics (admin-only)
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
## Production Setup
|
|
|
|
|
|
|
|
|
|
|
|
### Docker Compose
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-07-01 23:15:35 +02:00
|
|
|
|
cp .env.example .env
|
|
|
|
|
|
# Edit .env — set DATABASE_URL, REDIS_URL, SECRET_KEY, CORS_ORIGINS
|
|
|
|
|
|
# Set ENVIRONMENT=production, SESSION_COOKIE_SECURE=true
|
|
|
|
|
|
docker compose up -d
|
|
|
|
|
|
|
|
|
|
|
|
# Run migrations
|
|
|
|
|
|
docker compose exec api alembic upgrade head
|
2026-06-03 23:51:59 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
### Manual (without Docker)
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
```bash
|
|
|
|
|
|
pip install -r requirements.txt
|
|
|
|
|
|
alembic upgrade head
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
# Start API server (2 workers)
|
|
|
|
|
|
uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 2
|
|
|
|
|
|
|
|
|
|
|
|
# Start ARQ worker (separate process)
|
|
|
|
|
|
arq app.core.jobs.WorkerSettings
|
2026-06-03 23:51:59 +00:00
|
|
|
|
```
|
2026-07-01 23:15:35 +02:00
|
|
|
|
|
|
|
|
|
|
See [docs/admin-guide.md](docs/admin-guide.md) for detailed deployment, backup, and troubleshooting instructions.
|
|
|
|
|
|
|
|
|
|
|
|
## API
|
|
|
|
|
|
|
|
|
|
|
|
### Key Endpoints
|
|
|
|
|
|
|
|
|
|
|
|
| Endpoint | Method | Auth | Description |
|
|
|
|
|
|
|---|---|---|---|
|
|
|
|
|
|
| `/api/v1/health` | GET | No | Health check (DB, Redis, storage, worker) |
|
|
|
|
|
|
| `/api/v1/metrics` | GET | Admin | Prometheus metrics (text/plain) |
|
|
|
|
|
|
| `/api/v1/auth/login` | POST | No | Login |
|
|
|
|
|
|
| `/api/v1/contacts` | GET | Yes | List contacts (paginated, max page_size=100) |
|
|
|
|
|
|
| `/api/v1/contacts/export` | GET | Yes | Stream contacts as CSV |
|
|
|
|
|
|
| `/api/v1/companies` | GET | Yes | List companies (paginated, max page_size=100) |
|
|
|
|
|
|
| `/api/v1/companies/export` | GET | Yes | Stream companies as CSV |
|
|
|
|
|
|
|
|
|
|
|
|
### Pagination
|
|
|
|
|
|
|
|
|
|
|
|
All list endpoints support pagination with `page` and `page_size` parameters.
|
|
|
|
|
|
`page_size` is capped at **100** — values >100 return HTTP 422.
|
|
|
|
|
|
|
|
|
|
|
|
### CSV Export
|
|
|
|
|
|
|
|
|
|
|
|
Contacts and companies support streaming CSV export via `/export?format=csv`.
|
|
|
|
|
|
Uses `StreamingResponse` — does not buffer the entire file in memory.
|
|
|
|
|
|
|
|
|
|
|
|
### Swagger UI
|
|
|
|
|
|
|
|
|
|
|
|
Interactive API documentation: http://localhost:8000/docs
|
|
|
|
|
|
|
|
|
|
|
|
See [docs/api-overview.md](docs/api-overview.md) for the full endpoint summary.
|
|
|
|
|
|
|
|
|
|
|
|
## Monitoring
|
|
|
|
|
|
|
|
|
|
|
|
### Health Check
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
curl http://localhost:8000/api/v1/health
|
2026-06-03 23:51:59 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
Returns JSON with overall status (`healthy`/`degraded`) and individual checks for
|
|
|
|
|
|
`database`, `redis`, `storage`, and `worker`.
|
|
|
|
|
|
|
|
|
|
|
|
### Prometheus Metrics
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
# Requires admin authentication
|
|
|
|
|
|
curl -b "leocrm_session=<session>" http://localhost:8000/api/v1/metrics
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
Available metrics:
|
|
|
|
|
|
- `leocrm_http_requests_total` — Total HTTP requests
|
|
|
|
|
|
- `leocrm_http_request_duration_seconds` — Request duration histogram
|
|
|
|
|
|
- `leocrm_db_pool_connections` — Database connection pool size
|
|
|
|
|
|
- `leocrm_arq_jobs_total` — Total ARQ background jobs
|
|
|
|
|
|
|
|
|
|
|
|
### Structured Logging
|
|
|
|
|
|
|
|
|
|
|
|
LeoCRM uses `structlog` for structured JSON logging. All API requests are logged with:
|
|
|
|
|
|
`timestamp`, `level`, `event`, `method`, `path`, `status`, `duration_ms`, `tenant_id`.
|
|
|
|
|
|
|
|
|
|
|
|
## Environment Profiles
|
|
|
|
|
|
|
|
|
|
|
|
| Profile | `ENVIRONMENT` | Use Case |
|
|
|
|
|
|
|---|---|---|
|
|
|
|
|
|
| Development | `development` | Local dev (auto-reload, verbose logging) |
|
|
|
|
|
|
| Testing | `testing` | Test suite (separate test DB, minimal logging) |
|
|
|
|
|
|
| Production | `production` | Docker/Coolify deployment (JSON logging, secure cookies) |
|
|
|
|
|
|
|
|
|
|
|
|
See [docs/admin-guide.md](docs/admin-guide.md#environment-profiles) for profile details.
|
|
|
|
|
|
|
2026-06-03 23:51:59 +00:00
|
|
|
|
## Testing
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
# Run all tests
|
|
|
|
|
|
pytest -v --tb=short
|
|
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
# Run specific test suites
|
|
|
|
|
|
pytest tests/test_monitoring.py tests/test_performance.py tests/test_health.py -v
|
|
|
|
|
|
|
2026-06-03 23:51:59 +00:00
|
|
|
|
# Run with coverage
|
|
|
|
|
|
pytest --cov=app --cov-report=term-missing
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## Environment Variables
|
|
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
See [.env.example](.env.example) for all variables and [docs/admin-guide.md](docs/admin-guide.md#environment-configuration) for detailed descriptions.
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
### Key Variables
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
| Variable | Required | Description |
|
|
|
|
|
|
|---|---|---|
|
|
|
|
|
|
| `DATABASE_URL` | ✅ | PostgreSQL async connection URL |
|
|
|
|
|
|
| `REDIS_URL` | ✅ | Redis connection URL |
|
|
|
|
|
|
| `SECRET_KEY` | ✅ | Secret key for signing (≥32 chars in prod) |
|
|
|
|
|
|
| `CORS_ORIGINS` | ✅ | Comma-separated allowed origins (no wildcards) |
|
|
|
|
|
|
| `ENVIRONMENT` | ❌ | `development` \| `testing` \| `production` |
|
|
|
|
|
|
| `STORAGE_PATH` | ❌ | File storage path (default: `/tmp`) |
|
|
|
|
|
|
| `SMTP_HOST` | ❌ | SMTP server hostname |
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
## Performance Testing
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
# Seed 200k contacts for performance testing
|
|
|
|
|
|
python scripts/seed_perf_data.py --count 200000
|
|
|
|
|
|
|
|
|
|
|
|
# Verify database indexes
|
|
|
|
|
|
python scripts/check_indexes.py
|
|
|
|
|
|
|
|
|
|
|
|
# Test pagination performance
|
|
|
|
|
|
# GET /api/v1/contacts?page=1&page_size=25 — should be <500ms with 200k records
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## Project Structure
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
leocrm/
|
|
|
|
|
|
├── app/
|
|
|
|
|
|
│ ├── main.py # FastAPI entry point with logging middleware
|
|
|
|
|
|
│ ├── config.py # Pydantic settings
|
|
|
|
|
|
│ ├── core/
|
|
|
|
|
|
│ │ ├── monitoring.py # Prometheus metrics + structured logging + health checks
|
|
|
|
|
|
│ │ ├── db.py # Async database engine
|
|
|
|
|
|
│ │ ├── middleware.py # CSRF middleware
|
|
|
|
|
|
│ │ └── ...
|
|
|
|
|
|
│ ├── routes/
|
|
|
|
|
|
│ │ ├── health.py # Health endpoint
|
|
|
|
|
|
│ │ ├── metrics.py # Prometheus metrics endpoint (admin-only)
|
|
|
|
|
|
│ │ ├── contacts.py # Contact CRUD + streaming CSV export
|
|
|
|
|
|
│ │ ├── companies.py # Company CRUD + streaming CSV export
|
|
|
|
|
|
│ │ └── ...
|
|
|
|
|
|
│ ├── models/ # SQLAlchemy models
|
|
|
|
|
|
│ ├── schemas/ # Pydantic schemas
|
|
|
|
|
|
│ ├── services/ # Business logic
|
|
|
|
|
|
│ └── plugins/ # Plugin system
|
|
|
|
|
|
├── scripts/
|
|
|
|
|
|
│ ├── seed_perf_data.py # Performance test data seeding
|
|
|
|
|
|
│ └── check_indexes.py # Database index verification
|
|
|
|
|
|
├── tests/ # Test suite (pytest + pytest-asyncio)
|
|
|
|
|
|
├── docs/
|
|
|
|
|
|
│ ├── admin-guide.md # Admin guide (deploy, backup, restore, troubleshooting)
|
|
|
|
|
|
│ └── api-overview.md # API endpoint summary
|
|
|
|
|
|
├── alembic/ # Database migrations
|
|
|
|
|
|
├── requirements.txt # Production dependencies
|
|
|
|
|
|
├── requirements-dev.txt # Test/lint dependencies
|
|
|
|
|
|
├── .env.example # Environment template
|
|
|
|
|
|
├── docker-compose.yml # Docker Compose
|
|
|
|
|
|
└── README.md # This file
|
|
|
|
|
|
```
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
## Documentation
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
2026-07-01 23:15:35 +02:00
|
|
|
|
- [Admin Guide](docs/admin-guide.md) — Deployment, backup, restore, env vars, troubleshooting
|
|
|
|
|
|
- [API Overview](docs/api-overview.md) — Full endpoint reference
|
|
|
|
|
|
- [Coolify Setup](COOLIFY_SETUP.md) — Coolify deployment instructions
|
|
|
|
|
|
- [Swagger UI](http://localhost:8000/docs) — Interactive API docs (auto-generated)
|
2026-06-03 23:51:59 +00:00
|
|
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
|
|
|
|
Internal project – proprietary.
|