dateme/QUICK_REFERENCE.md
2025-12-17 00:44:01 +02:00

9.3 KiB

Quick Reference - Common Commands

🐳 Docker & Docker Compose

Local Development

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f backend
docker-compose logs -f frontend
docker-compose logs -f postgres

# Stop services
docker-compose down

# Remove volumes (reset database)
docker-compose down -v

# Rebuild images
docker-compose build --no-cache

Build Images for Deployment

# Build backend
docker build -t dating-app-backend:v1 -f backend/Dockerfile backend/

# Build frontend
docker build -t dating-app-frontend:v1 -f frontend/Dockerfile frontend/

# Tag for registry
docker tag dating-app-backend:v1 myregistry.com/dating-app-backend:v1
docker tag dating-app-frontend:v1 myregistry.com/dating-app-frontend:v1

# Push to registry
docker push myregistry.com/dating-app-backend:v1
docker push myregistry.com/dating-app-frontend:v1

☸️ Kubernetes & Helm

Deploy

# Install new release
helm install dating-app ./helm/dating-app -n dating-app --create-namespace

# Install with custom values
helm install dating-app ./helm/dating-app -n dating-app --create-namespace -f values-custom.yaml

# Deploy to AWS
helm install dating-app ./helm/dating-app -n dating-app --create-namespace -f helm/dating-app/values-aws.yaml

# Dry-run (test without deploying)
helm install dating-app ./helm/dating-app -n dating-app --dry-run --debug

Update Deployment

# Upgrade to new images
helm upgrade dating-app ./helm/dating-app -n dating-app -f my-values.yaml

# Rollback to previous version
helm rollback dating-app -n dating-app

# See deployment history
helm history dating-app -n dating-app

View Status

# All resources in namespace
kubectl get all -n dating-app

# Pods
kubectl get pods -n dating-app
kubectl get pods -n dating-app -w  # Watch for changes

# Services
kubectl get svc -n dating-app

# Ingress
kubectl get ingress -n dating-app
kubectl describe ingress -n dating-app

# PersistentVolumeClaims
kubectl get pvc -n dating-app

# Events
kubectl get events -n dating-app --sort-by='.lastTimestamp'

Debugging

# Pod logs
kubectl logs -n dating-app deployment/backend
kubectl logs -n dating-app deployment/backend -f  # Follow
kubectl logs -n dating-app deployment/backend --previous  # Previous run

# Pod details
kubectl describe pod -n dating-app <pod-name>

# Interactive shell
kubectl exec -it -n dating-app <pod-name> -- /bin/bash

# Port forwarding
kubectl port-forward -n dating-app svc/backend 8000:8000
kubectl port-forward -n dating-app svc/frontend 3000:80

# Run debug container
kubectl run -it --rm debug --image=ubuntu:latest --restart=Never -n dating-app -- /bin/bash

Clean Up

# Delete release
helm uninstall dating-app -n dating-app

# Delete namespace (everything in it)
kubectl delete namespace dating-app

# Delete specific resource
kubectl delete pod -n dating-app <pod-name>
kubectl delete pvc -n dating-app <pvc-name>

🔧 PostgreSQL

Connect to Database

# Via Docker Compose
docker exec -it dating_app_postgres psql -U dating_user -d dating_app

# Via Kubernetes
kubectl exec -it -n dating-app <postgres-pod> -- psql -U dating_user -d dating_app

# From outside (with port-forward)
# First: kubectl port-forward -n dating-app svc/postgres 5432:5432
psql -h localhost -U dating_user -d dating_app

Common SQL Commands

-- List all tables
\dt

-- Describe table
\d users

-- List all databases
\l

-- Count records
SELECT COUNT(*) FROM users;

-- View all users
SELECT * FROM users;

-- View all profiles
SELECT * FROM profiles;

-- Check likes
SELECT * FROM likes;

-- Check conversations
SELECT * FROM conversations;

-- Reset database (delete all data)
DROP TABLE IF EXISTS messages CASCADE;
DROP TABLE IF EXISTS conversations CASCADE;
DROP TABLE IF EXISTS likes CASCADE;
DROP TABLE IF EXISTS photos CASCADE;
DROP TABLE IF EXISTS profiles CASCADE;
DROP TABLE IF EXISTS users CASCADE;

-- Quit
\q

📝 Environment Setup

Backend

# Create .env file
cp backend/.env.example backend/.env

# Edit with your values
nano backend/.env

# Required variables:
# DATABASE_URL
# JWT_SECRET
# JWT_EXPIRES_MINUTES
# MEDIA_DIR
# CORS_ORIGINS

Frontend

# Create .env file
cp frontend/.env.example frontend/.env

# Edit with API URL
nano frontend/.env

# Required variables:
# VITE_API_URL

🧪 Testing

API Testing with curl

# Register user
curl -X POST http://localhost:8000/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "test@example.com",
    "password": "password123",
    "display_name": "Test User"
  }'

# Login
TOKEN=$(curl -s -X POST http://localhost:8000/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "test@example.com",
    "password": "password123"
  }' | grep -o '"access_token":"[^"]*' | cut -d'"' -f4)

# Get current user
curl -H "Authorization: Bearer $TOKEN" http://localhost:8000/auth/me

# Create profile
curl -X POST http://localhost:8000/profiles/ \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "display_name": "Test User",
    "age": 25,
    "gender": "male",
    "location": "San Francisco",
    "bio": "Test bio",
    "interests": ["hiking", "travel"]
  }'

# Upload photo
curl -X POST http://localhost:8000/photos/upload \
  -H "Authorization: Bearer $TOKEN" \
  -F "file=@/path/to/photo.jpg"

# Get profiles to discover
curl -H "Authorization: Bearer $TOKEN" \
  http://localhost:8000/profiles/discover/list

# Like a user (user_id=2)
curl -X POST http://localhost:8000/likes/2 \
  -H "Authorization: Bearer $TOKEN"

# Get matches
curl -H "Authorization: Bearer $TOKEN" \
  http://localhost:8000/likes/matches/list

# Get conversations
curl -H "Authorization: Bearer $TOKEN" \
  http://localhost:8000/chat/conversations

# Send message (conversation_id=1)
curl -X POST http://localhost:8000/chat/conversations/1/messages \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"content": "Hello!"}'

# Get messages
curl -H "Authorization: Bearer $TOKEN" \
  http://localhost:8000/chat/conversations/1/messages

API Documentation

# Open Swagger UI
open http://localhost:8000/docs

# Open ReDoc
open http://localhost:8000/redoc

📦 Package Management

Python (Backend)

# Create virtual environment
cd backend
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Run development server
python -m uvicorn main:app --reload

# Add new dependency
pip install new-package
pip freeze > requirements.txt

Node (Frontend)

# Install dependencies
cd frontend
npm install

# Development server
npm run dev

# Production build
npm run build

# Preview production build
npm run preview

# Add new dependency
npm install new-package

🔍 Useful Grep/Search

Find by pattern

# Find all API endpoints in backend
grep -r "@router" backend/app/routers/

# Find all useState hooks in frontend
grep -r "useState" frontend/src/

# Find environment variable usage
grep -r "import.meta.env" frontend/src/

# Find all database queries
grep -r "cur.execute" backend/app/

📊 Monitoring

Check resource usage

# Kubernetes
kubectl top pods -n dating-app
kubectl top nodes

# Docker
docker stats dating_app_backend
docker stats dating_app_postgres

Health checks

# Backend health
curl http://localhost:8000/health

# Frontend health (with forwarding)
# kubectl port-forward -n dating-app svc/frontend 3000:80
curl http://localhost:3000/health

🔐 Security Commands

Generate secure secrets

# Generate JWT secret
openssl rand -hex 32

# Generate password
openssl rand -base64 32

# Hash a password (for testing)
python -c "from passlib.context import CryptContext; pwd_context = CryptContext(schemes=['bcrypt']); print(pwd_context.hash('mypassword'))"

📋 Helm Troubleshooting

Common Helm commands

# Lint chart for syntax errors
helm lint ./helm/dating-app

# Template rendering (see generated YAML)
helm template dating-app ./helm/dating-app -f values.yaml

# Get current values
helm get values dating-app -n dating-app

# Show manifest of deployed release
helm get manifest dating-app -n dating-app

# Compare current values with chart defaults
helm diff upgrade dating-app ./helm/dating-app -n dating-app

🚀 Deployment Checklist

Before deploying to production

# [ ] Update all passwords and secrets
# [ ] Test locally with docker-compose
# [ ] Build and push images
# [ ] Update Helm values with production settings
# [ ] Run helm lint
# [ ] Run helm template and review YAML
# [ ] Run helm install with --dry-run
# [ ] Verify namespace creation
# [ ] Verify PVCs creation
# [ ] Verify pods are running
# [ ] Verify services are accessible
# [ ] Verify ingress is configured
# [ ] Test API endpoints
# [ ] Test database connectivity
# [ ] Check logs for errors
# [ ] Monitor resource usage

Tip: Save this file for quick reference during development and deployment!