Spaces:
Sleeping
Sleeping
File size: 9,001 Bytes
1e89ad0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# SwiftOps Backend - Quick Start Guide
Get the backend running in 5 minutes.
---
## π Prerequisites
- Python 3.11+
- Docker & Docker Compose
- Git
---
## β‘ Quick Setup (Docker)
### **1. Clone and Setup**
```bash
# Clone repository
git clone <repo-url>
cd backend
# Copy environment file
cp .env.example .env
```
### **2. Configure Environment**
Edit `.env` and add your Supabase credentials:
```bash
# Minimum required configuration
DATABASE_URL=postgresql://postgres:password@db.your-project.supabase.co:5432/postgres
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_ANON_KEY=your-anon-key
SUPABASE_SERVICE_KEY=your-service-key
SECRET_KEY=your-secret-key-change-this
```
### **3. Start Everything**
```bash
# Start all services (API, Celery, Redis, PostgreSQL)
docker-compose up -d
# View logs
docker-compose logs -f api
# Check status
docker-compose ps
```
### **4. Run Migrations**
```bash
# Run database migrations
docker-compose exec api alembic upgrade head
# Seed test data (optional)
docker-compose exec api python scripts/seed_data.py
```
### **5. Test the API**
Open your browser:
- **API Docs**: http://localhost:8000/docs
- **Health Check**: http://localhost:8000/health
---
## π οΈ Manual Setup (Without Docker)
### **1. Create Virtual Environment**
```bash
# Create venv
python -m venv venv
# Activate (Linux/Mac)
source venv/bin/activate
# Activate (Windows)
venv\Scripts\activate
```
### **2. Install Dependencies**
```bash
pip install -r requirements.txt
```
### **3. Setup Environment**
```bash
cp .env.example .env
# Edit .env with your configuration
```
### **4. Start Redis** (Required for Celery)
```bash
# Using Docker
docker run -d -p 6379:6379 redis:alpine
# Or install Redis locally
# Mac: brew install redis && redis-server
# Ubuntu: sudo apt install redis-server && redis-server
```
### **5. Run Migrations**
```bash
alembic upgrade head
```
### **6. Start Services**
**Terminal 1 - API Server**:
```bash
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
```
**Terminal 2 - Celery Worker**:
```bash
celery -A app.tasks.celery_app worker --loglevel=info
```
**Terminal 3 - Celery Beat** (Scheduler):
```bash
celery -A app.tasks.celery_app beat --loglevel=info
```
---
## π§ͺ Verify Installation
### **1. Health Check**
```bash
curl http://localhost:8000/health
```
Expected response:
```json
{
"status": "healthy",
"database": "connected",
"redis": "connected",
"version": "1.0.0"
}
```
### **2. Run Tests**
```bash
# Run all tests
pytest
# Run with coverage
pytest --cov=app --cov-report=html
# Open coverage report
open htmlcov/index.html
```
### **3. Test API Endpoint**
```bash
# Create a test user (if seed data not loaded)
curl -X POST http://localhost:8000/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"password": "testpassword123",
"name": "Test User"
}'
# Login
curl -X POST http://localhost:8000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"password": "testpassword123"
}'
```
---
## π Project Structure Overview
```
backend/
βββ app/
β βββ api/ # API endpoints
β βββ services/ # Business logic
β βββ repositories/ # Data access
β βββ models/ # Database models
β βββ schemas/ # Request/response schemas
β βββ tasks/ # Background tasks
βββ tests/ # Test suite
βββ scripts/ # Utility scripts
βββ .env # Configuration
```
---
## π Key Endpoints
### **Authentication**
- `POST /api/v1/auth/login` - User login
- `POST /api/v1/auth/register` - User registration
- `POST /api/v1/auth/refresh` - Refresh token
### **Tickets**
- `GET /api/v1/tickets` - List tickets
- `POST /api/v1/tickets` - Create ticket
- `POST /api/v1/tickets/{id}/assign` - Assign ticket
- `PATCH /api/v1/tickets/{id}/status` - Update status
### **Payroll**
- `GET /api/v1/payroll` - List payroll records
- `POST /api/v1/payroll/generate` - Generate payroll
- `PATCH /api/v1/payroll/{id}/mark-paid` - Mark as paid
### **Inventory**
- `GET /api/v1/inventory` - List inventory
- `POST /api/v1/inventory/assign` - Assign to agent
- `POST /api/v1/inventory/return` - Return equipment
### **Analytics**
- `GET /api/v1/analytics/dashboard` - Dashboard metrics
- `GET /api/v1/analytics/agent-performance` - Agent stats
- `GET /api/v1/analytics/sla-compliance` - SLA reports
---
## π Troubleshooting
### **Issue: Database connection failed**
**Solution**:
```bash
# Check DATABASE_URL in .env
# Verify Supabase project is running
# Test connection:
psql $DATABASE_URL
```
### **Issue: Redis connection failed**
**Solution**:
```bash
# Check if Redis is running
docker ps | grep redis
# Or test connection
redis-cli ping
# Should return: PONG
```
### **Issue: Celery tasks not running**
**Solution**:
```bash
# Check Celery worker is running
celery -A app.tasks.celery_app inspect active
# Check Celery beat is running
celery -A app.tasks.celery_app inspect scheduled
# View Celery logs
docker-compose logs -f celery-worker
```
### **Issue: Import errors**
**Solution**:
```bash
# Ensure virtual environment is activated
which python
# Should show: /path/to/venv/bin/python
# Reinstall dependencies
pip install -r requirements.txt --force-reinstall
```
### **Issue: Alembic migration failed**
**Solution**:
```bash
# Check current migration version
alembic current
# Downgrade one version
alembic downgrade -1
# Upgrade again
alembic upgrade head
# If stuck, reset migrations (CAUTION: drops all data)
alembic downgrade base
alembic upgrade head
```
---
## π Common Development Tasks
### **Create a New Migration**
```bash
# Auto-generate migration from model changes
alembic revision --autogenerate -m "Add new column to tickets"
# Create empty migration
alembic revision -m "Custom migration"
# Apply migration
alembic upgrade head
```
### **Add a New API Endpoint**
1. Create schema in `app/schemas/`:
```python
# app/schemas/ticket.py
class TicketCreate(BaseModel):
title: str
description: str
priority: str
```
2. Add service method in `app/services/`:
```python
# app/services/ticket_service.py
async def create_ticket(self, data: TicketCreate):
# Business logic here
pass
```
3. Add API route in `app/api/v1/`:
```python
# app/api/v1/tickets.py
@router.post("/tickets")
async def create_ticket(
data: TicketCreate,
service: TicketService = Depends(get_ticket_service)
):
return await service.create_ticket(data)
```
### **Add a Background Task**
```python
# app/tasks/custom_tasks.py
from app.tasks.celery_app import celery_app
@celery_app.task
def my_background_task(param1, param2):
# Task logic here
pass
# Trigger from API
my_background_task.delay(param1, param2)
```
### **Run Specific Tests**
```bash
# Run single test file
pytest tests/unit/test_services/test_ticket_service.py
# Run single test function
pytest tests/unit/test_services/test_ticket_service.py::test_assign_ticket
# Run tests matching pattern
pytest -k "test_ticket"
# Run with verbose output
pytest -v
# Run with print statements
pytest -s
```
---
## π§ Development Tools
### **Database Management**
```bash
# Connect to database
psql $DATABASE_URL
# View tables
\dt
# Describe table
\d tickets
# Run query
SELECT * FROM tickets LIMIT 10;
```
### **Redis Management**
```bash
# Connect to Redis
redis-cli
# View all keys
KEYS *
# Get value
GET ticket:123
# Delete key
DEL ticket:123
# Clear all data (CAUTION)
FLUSHALL
```
### **API Testing with HTTPie**
```bash
# Install HTTPie
pip install httpie
# Test endpoint
http GET http://localhost:8000/api/v1/tickets \
Authorization:"Bearer YOUR_TOKEN"
# POST request
http POST http://localhost:8000/api/v1/tickets \
Authorization:"Bearer YOUR_TOKEN" \
title="Test Ticket" \
description="Test description"
```
---
## π Next Steps
1. **Read the Architecture Guide**: `ARCHITECTURE.md`
2. **Explore API Documentation**: http://localhost:8000/docs
3. **Review Code Examples**: Check `app/services/` for business logic patterns
4. **Write Tests**: Add tests for new features in `tests/`
5. **Check Background Tasks**: Review `app/tasks/` for scheduled jobs
---
## π Getting Help
- **Documentation**: See `README.md` and `ARCHITECTURE.md`
- **API Docs**: http://localhost:8000/docs
- **Issues**: Create an issue on GitHub
- **Team Chat**: [Your team communication channel]
---
## β
Checklist for New Developers
- [ ] Clone repository
- [ ] Setup environment variables
- [ ] Start services (Docker or manual)
- [ ] Run migrations
- [ ] Run tests (all passing)
- [ ] Access API docs at http://localhost:8000/docs
- [ ] Create a test ticket via API
- [ ] Review architecture documentation
- [ ] Understand folder structure
- [ ] Ready to code! π
|