FastAPI backend with PostgreSQL, JWT auth, WebSocket admin notifications, and Gmail API email.
- Install dependencies:
pip install -r requirements.txt- Database (PostgreSQL on localhost:5432):
CREATE DATABASE zebrands;- Environment (.env in project root):
cp .env.example .envRequired environment variables:
DATABASE_URL=postgresql+asyncpg://postgres:121224@localhost:5432/zebrands
JWT_SECRET_KEY=change-me-in-production
JWT_ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=60
APP_NAME=ZeBrands Catalog API
ENVIRONMENT=development
GOOGLE_CLIENT_ID=your_google_client_id_here
GOOGLE_CLIENT_SECRET=your_google_client_secret_here
GMAIL_SENDER_EMAIL=your_sender_email@gmail.com
GMAIL_RECIPIENT_EMAIL=your_recipient_email@gmail.com
Usa
.env.examplecomo plantilla y configúralo localmente con tus credenciales reales.
ElGOOGLE_CLIENT_SECRETte lo proporcionaré.
uvicorn app.main:app --reload --host 127.0.0.1 --port 8000Docs: http://127.0.0.1:8000/docs
Seed initial admin:
python seed.pyAdmin: admin@example.com / admin123
PowerShell example:
$response = Invoke-WebRequest -Uri "http://127.0.0.1:8000/auth/token" -Method POST -Headers @{"Content-Type"="application/x-www-form-urlencoded"} -Body "username=admin@example.com&password=admin123"
$token = ($response.Content | ConvertFrom-Json).access_token- Products (public):
- GET /products
- GET /products/{id} (creates a view log)
- Products (admin, Authorization: Bearer ):
- POST /products
- PUT /products/{id}/update (or PUT /products/{id}/edit)
- DELETE /products/{id}
- Admin users (admin):
- GET /admin/users
- POST /admin/users
- PUT /admin/users/{id}/update (or PUT /admin/users/{id}/edit)
- DELETE /admin/users/{id}
- WebSocket (admin): connect to
ws://127.0.0.1:8000/ws/admin-notifications?token=<JWT>and keep the connection open to receive product update events. - Email (Gmail API): product updates trigger a background email to
GMAIL_RECIPIENT_EMAIL.
- GET /analytics/summary — overall counts
- GET /analytics/product-views — aggregated views (supports days, limit, optional product_id)
- GET /analytics/product-views/{id}/detailed — recent view logs
- GET /analytics/user-agents — top agents
Example (PostgreSQL):
CREATE ROLE zebrands_app LOGIN PASSWORD 'strong-password';
GRANT CONNECT ON DATABASE zebrands TO zebrands_app;
\c zebrands
GRANT USAGE ON SCHEMA public TO zebrands_app;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO zebrands_app;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO zebrands_app;Then set:
DATABASE_URL=postgresql+asyncpg://zebrands_app:strong-password@localhost:5432/zebrands
---
}