🔔 Notification Service
Notification Service is an asynchronous microservice built with FastAPI for managing and delivering notifications across multiple channels (Email, Telegram, In-App). Designed with scalability and high performance in mind, it leverages background task processing and real-time WebSocket communication.
🚀 Key Features
- Multi-Channel Delivery: Seamlessly send messages via Email (SMTP), Telegram (Bot API), and In-App (WebSockets).
- Asynchronous Architecture: Fully async implementation using
FastAPI, asyncpg, and SQLAlchemy 2.0 for maximum throughput.
- Background Processing: Time-consuming tasks (like reaching out to 3rd-party APIs or SMTP servers) are offloaded to Celery workers using Redis as a message broker.
- Real-Time Updates: Clients instantly receive notification statuses via WebSockets powered by Redis Pub/Sub for cross-worker event broadcasting.
- Secure Authentication: JWT-based authentication (access tokens) and secure password hashing (
bcrypt).
- Templating Engine: Create and manage dynamic notification templates using Jinja-like variables (
{{ username }}, {{ user_id }}).
- Containerized: Ready for deployment with Docker and Docker Compose, wrapping the API, Celery worker, PostgreSQL, and Redis in a single isolated environment.
🛠️ Tech Stack
- Backend Framework: FastAPI, Pydantic
- Database & ORM: PostgreSQL, SQLAlchemy 2.0 (async), Alembic (Migrations)
- Task Queue & Cache: Celery, Redis
- Real-time: WebSockets, Redis Pub/Sub
- Testing: Pytest, pytest-asyncio, HTTPX
- Deployment: Docker, Docker Compose
🏗️ Architecture
graph TB
Client["Client (HTTP)"] -->|POST /notifications/send| API["FastAPI"]
WS_Client["Client (WS)"] <-->|WebSocket| API
API -->|"Save to DB (status=pending)"| DB[(PostgreSQL)]
API -->|"Publish Task"| Broker["Redis (Broker)"]
Broker --> Worker["Celery Worker"]
Worker -->|Email| SMTP["SMTP Server"]
Worker -->|Telegram| TG["Telegram Bot API"]
Worker -->|In-App| RedisPub["Redis Pub/Sub"]
RedisPub -->|"Push Event"| API
API -->|"WebSocket Push"| WS_Client
Worker -->|"Update Status"| DB
⚙️ Quick Start (Docker)
-
Clone the repository
git clone https://github.com/your-username/notification-service.git
cd notification-service
-
Set up Environment Variables
Create a .env file based on the template (provide your DB credentials, SMTP, and Telegram tokens).
-
Run with Docker Compose
docker-compose up --build -d
The API will be available at http://localhost:8000.
-
Apply Database Migrations (if not run automatically)
docker exec -it notify-api alembic upgrade head
📚 API Endpoints Overview
Interactive documentation available at http://localhost:8000/docs (Swagger UI).
- Auth:
POST /auth/register, POST /auth/login
- Templates:
GET /templates/, POST /templates/, GET /templates/{id}, PATCH /templates/{id}, DELETE /templates/{id}
- Notifications:
POST /notifications/send - Schedule a notification (background task)
GET /notifications/ - History & Pagination
GET /notifications/stats - Delivery statistics
GET /notifications/{id} - Specific notification status
- WebSocket:
ws://localhost:8000/ws/notifications- Connect to receive live updates.
🧪 Testing
The project includes a comprehensive test suite using pytest and an in-memory SQLite database to mock services and test business logic without external dependencies.
pytest tests/ -v
评论