python -m venv .venv
source .venv/bin/activate
pip install -r requirement.txtCreate .env in project root:
APP_NAME=Araf E-commerce API
APP_VERSION=0.1.0
API_PREFIX=/api/v1
DATABASE_URL=mysql+pymysql://root:root@localhost:3306/araf_ecommerce
SECRET_KEY=change-this-secret-key
ACCESS_TOKEN_EXPIRE_MINUTES=60
REFRESH_TOKEN_EXPIRE_DAYS=7
ALGORITHM=HS256
APP_ENV=development
LOG_LEVEL=INFO
REQUEST_TIMEOUT_SECONDS=30
MAX_REQUEST_SIZE_BYTES=1048576
LOGIN_MAX_ATTEMPTS=5
LOGIN_LOCK_MINUTES=15
OTEL_ENABLED=true
OTEL_SERVICE_NAME=araf-ecommerce-api
OTEL_EXPORTER_OTLP_ENDPOINT=
OTEL_EXPORTER_OTLP_INSECURE=true
RATE_LIMIT_PER_MINUTE=60
CORS_ALLOWED_ORIGINS=http://localhost:3000,http://127.0.0.1:3000
REDIS_URL=redis://localhost:6379/0
RABBITMQ_URL=amqp://guest:guest@localhost:5672//
DEFAULT_ADMIN_USERNAME=admin
DEFAULT_ADMIN_PASSWORD=admin123docker compose up -dServices included:
- MySQL
- Redis
- RabbitMQ (management UI on
http://127.0.0.1:15672) - OpenTelemetry Collector
- Jaeger UI (
http://127.0.0.1:16686) - Celery worker
- Flower dashboard (
http://127.0.0.1:5555)
uvicorn main:app --reloadOpen Swagger: http://127.0.0.1:8000/docs
- Call
POST /api/v1/auth/tokenwith form-data:username=adminpassword=admin123
- Copy
access_token - Use
Authorization: Bearer <token>for category APIs - Refresh token when access token expires:
POST /api/v1/auth/refresh- Body:
{ "refresh_token": "..." }
GET /api/v1/categories?search=elec&page=1&size=10POST /api/v1/categoriesPUT /api/v1/categories/{category_id}DELETE /api/v1/categories/{category_id}
All category routes are guarded with OAuth2 JWT auth.
Upload endpoint:
POST /api/v1/categories/{category_id}/image- Content-Type:
multipart/form-data - Field name:
image
Behavior:
- API stores original image under
media/uploads/categories. - API creates image job record (
queued). - Celery task is enqueued via RabbitMQ.
- Celery worker processes image in background and saves a processed file copy under
media/processed/categories. - Job status updates to
completedorfailed.
Supported file formats:
.jpg,.jpeg,.png,.webp
Processed files are accessible via:
/media/...
Admin endpoints (admin token required):
GET /api/v1/admin/image-jobsGET /api/v1/admin/image-jobs?status=failedPOST /api/v1/admin/image-jobs/{job_id}/retryGET /api/v1/admin/image-jobs-dashboard
Dashboard UI options:
- Flower task dashboard:
http://127.0.0.1:5555 - In-app admin dashboard with failed-job retry buttons:
GET /api/v1/admin/image-jobs-dashboard- Paste admin bearer token in the page
- Load failed jobs
- Click Retry for any failed job
Create/upgrade schema with Alembic:
alembic upgrade headIf your DB was created before Alembic tracking (for example via Base.metadata.create_all()), the first upgrade can fail with table already exists. Reconcile by stamping the baseline, then upgrading:
alembic stamp 20260526_0001
alembic upgrade headIf the objects from 20260526_0002 already exist too, stamp directly to head:
alembic stamp headCreate a new migration:
alembic revision --autogenerate -m "your_message"Initial migration file:
alembic/versions/20260526_0001_init_schema.py
Legacy SQL file is still available at:
migrations/0001_create_users_categories.sql
pytest -qGET /healthreturns basic API status.GET /health/dependenciesperforms live Redis and RabbitMQ checks.- Returns
200when both are ready. - Returns
503when one or more dependencies are unavailable.
- Returns
GET /metricsexposes Prometheus metrics.
- OpenTelemetry tracing is enabled with OTLP exporter support.
- Set
OTEL_EXPORTER_OTLP_ENDPOINTto your collector URL to export traces. - Response headers include:
X-Request-IDX-Trace-ID
- Logs include both
request_idandtrace_idfor correlation.
docker compose up -dstarts API + MySQL + Redis + RabbitMQ + OTel Collector + Jaeger.- API traces are preconfigured to export to collector via:
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318/v1/traces
- Open Jaeger UI:
http://127.0.0.1:16686
- Structured JSON logging with request and user context.
- Request ID propagation via
X-Request-ID. - Standardized global error response shape.
- Request timeout and payload size guardrails.
- Idempotency key support for write endpoints (
Idempotency-Key). - Login brute-force lockout by username and client IP.
- Refresh token rotation with revoke tracking (Redis-backed with safe fallback).
- Endpoint-specific rate limits for auth APIs.
- CI workflow:
.github/workflows/ci.yml - Security workflow:
.github/workflows/security.yml
- Production standards:
docs/production-standards.md - Incident runbook:
docs/runbooks/incidents.md