Files
ratebubble/doc/ops.md
2026-02-28 02:44:41 +03:00

292 lines
4.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Netflix Scraper API - Operasyon Dokümantasyonu
## Hızlı Başlangıç
### Gereksinimler
- Docker & Docker Compose
- Node.js 20+ (local development için)
### Tek Komut ile Başlatma (Development)
```bash
# .env dosyasını oluştur
cp .env.example .env
# .env dosyasını düzenle
nano .env
# Başlat
docker compose -f docker-compose.dev.yml up --build
```
**Hepsi bu kadar!** Uygulama şu adreste çalışacak: `http://localhost:3000`
---
## Ortamlar
### Development
```bash
docker compose -f docker-compose.dev.yml up --build
```
Özellikler:
- Hot reload aktif
- Tüm loglar görünür
- Debug modu
### Production
```bash
docker compose up --build -d
```
Özellikler:
- Multi-stage build
- Non-root user
- Minimal image
- Production optimizations
---
## Environment Değişkenleri
### .env Dosyası
```env
# === Server ===
PORT=3000
NODE_ENV=development
# === PostgreSQL ===
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your-secure-password
POSTGRES_DB=netflix_scraper
# === Redis ===
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_TTL_SECONDS=604800
# === Rate Limiting ===
RATE_LIMIT_WINDOW_MS=60000
RATE_LIMIT_MAX_REQUESTS=30
# === API Keys ===
API_KEY_WEB=web-key-change-me
API_KEY_MOBILE=mobile-key-change-me
API_KEY_ADMIN=admin-key-secret
```
### Değişken Açıklamaları
| Değişken | Açıklama | Varsayılan |
|----------|----------|------------|
| `PORT` | Sunucu portu | 3000 |
| `NODE_ENV` | Ortam (development/production) | development |
| `REDIS_TTL_SECONDS` | Cache süresi (saniye) | 604800 (7 gün) |
| `RATE_LIMIT_WINDOW_MS` | Rate limit penceresi (ms) | 60000 (1 dk) |
| `RATE_LIMIT_MAX_REQUESTS` | Max istek sayısı | 30 |
---
## Migration
Migration'lar otomatik olarak container başlatılırken çalışır. Manuel çalıştırmak için:
```bash
# Container içinde
docker compose exec app npx prisma migrate deploy
# Local
npx prisma migrate deploy
```
### Yeni Migration Oluşturma
```bash
npx prisma migrate dev --name description_of_change
```
---
## Loglama
Tüm loglar JSON formatında structured logging ile yazılır.
### Log Seviyeleri
| Seviye | Açıklama |
|--------|----------|
| `debug` | Detaylı debug bilgisi |
| `info` | Genel bilgi |
| `warn` | Uyarı |
| `error` | Hata |
### Log Formatı
```json
{
"timestamp": "2025-02-27T10:00:00.000Z",
"level": "info",
"message": "Server started",
"service": "netflix-scraper-api",
"port": 3000
}
```
### Log Seviyesi Ayarlama
```env
LOG_LEVEL=debug
```
---
## Health Check
### Endpoints
- `GET /health` - Basit sağlık kontrolü
- `GET /ready` - Bağımlılık kontrolü (DB + Redis)
### Docker Health Check
Container'lar otomatik health check ile izlenir:
```yaml
healthcheck:
test: ["CMD", "wget", "--spider", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
```
---
## Troubleshooting
### Database Bağlantı Hatası
```
Error: Can't reach database server
```
**Çözüm:**
1. PostgreSQL container'ın çalıştığını kontrol et
2. `POSTGRES_HOST` değerini kontrol et (docker'da `postgres` olmalı)
3. Health check'i bekle: `docker compose logs postgres`
### Redis Bağlantı Hatası
```
Error: Redis connection failed
```
**Çözüm:**
1. Redis container'ın çalıştığını kontrol et
2. `REDIS_HOST` değerini kontrol et
### Migration Hatası
```
Error: P3005: The database schema is not empty
```
**Çözüm:**
```bash
# Veritabanını sıfırla
docker compose down -v
docker compose up --build
```
### Port Kullanımda Hatası
```
Error: port is already allocated
```
**Çözüm:**
```bash
# Hangi process kullanıyor
lsof -i :3000
# Process'i durdur
kill -9 <PID>
```
---
## Yararlı Komutlar
### Container Yönetimi
```bash
# Container'ları durdur
docker compose down
# Container'ları sil (volumes dahil)
docker compose down -v
# Logları görüntüle
docker compose logs -f app
# Container içine gir
docker compose exec app sh
```
### Database İşlemleri
```bash
# Prisma studio aç
docker compose exec app npx prisma studio
# Database schema görüntüle
docker compose exec app npx prisma db pull
# Seed çalıştır
docker compose exec app npm run prisma:seed
```
### Redis İşlemleri
```bash
# Redis CLI
docker compose exec redis redis-cli
# Tüm cache'i temizle
docker compose exec redis redis-cli FLUSHALL
```
---
## Backup & Restore
### PostgreSQL Backup
```bash
docker compose exec postgres pg_dump -U postgres netflix_scraper > backup.sql
```
### PostgreSQL Restore
```bash
cat backup.sql | docker compose exec -T postgres psql -U postgres netflix_scraper
```
---
## Monitoring (Opsiyonel)
### Metrics Endpoint (Yakında)
```
GET /metrics
```
Prometheus uyumlu metrics döndürür.