๐ Redis API Server with SSE
Real-time API layer for Redis-backed state updates. This FastAPI service listens to a Redis pub/sub channel and exposes endpoints for both current state retrieval and real-time streaming via Server-Sent Events (SSE).
๐ Overview
This service is the final step in a Redis-first architecture chain. It:
- Subscribes to a Redis pub/sub channel for updates.
- Serves current Redis KV data via REST endpoints.
- Streams updates to connected clients using SSE.
- Supports web control commands via PUT requests.
๐งฉ Features
- ๐ Live updates via /events SSE endpoint
- ๐ฆ State retrieval via /key/{KV_KEY} and /key/Environment
- ๐ฎ Web control interface via /webcontrol/{command}
- โ๏ธ Async Redis integration using redis.asyncio
- ๐ CORS support for frontend development
๐บ๏ธ API Endpoints
| Method | Endpoint | Description |
|---|---|---|
| GET | /key/{KV_KEY} | Returns the current value of a Redis key |
| GET | /key/Environment | Aggregates Weather, AQI, and Moon keys |
| GET | /events | SSE stream of updates from Redis pub/sub |
| PUT | /webcontrol/{command} | Broadcasts a control command (pp, fwd, etc.) |
๐ Data Flow
flowchart LR
subgraph Kubernetes
Microservice -- PUB update --> Redis
Redis -- SUB update --> KV-Updater
KV-Updater -- WRITE KV --> Redis
Redis -- SUB update --> APIServer
APIServer -- READ KV --> Redis
end
Client -- READ API --> APIServer
APIServer -- SSE update --> Client
style Microservice fill:#08f,color:#fff
style Redis fill:#e22,color:#fff
style KV-Updater fill:#eea,color:#000
style APIServer fill:#3d3,color:#fff
style Client fill:#fe1,color:#000
style Kubernetes fill:#fff,stroke:eea,stroke-width:1px,color:#444
linkStyle 4,5 stroke:#00F,color:blue
โ๏ธ Configuration
Settings are loaded from config.py. Example:
config = {
"redis_url": "redis://redis.redis:6379/0",
"update_channel": "update"
}
๐งช Local Development
- Install dependencies
pip install fastapi uvicorn redis sse-starlette
- Run the server
python apiserver.py
Or with hot reload:
uvicorn apiserver:app --reload
๐ CORS
CORS is enabled for http://localhost:3001 to support local frontend development. Adjust as needed in: allow_origins=["http://localhost:3001"]
๐ก SSE Notes
- Clients connect to /events for real-time updates.
- Messages are broadcasted from Redis pub/sub and internal control commands.
- Disconnected clients are automatically removed from the broadcast list.
๐ง Web Control Commands
Accepted commands via /webcontrol/{command}:
- pp โ Pause/play
- fwd โ Fast forward
- rew โ Rewind
- out โ Exit or shutdown These are broadcasted as:
{
"type": "webcontrol",
"command": "pp"
}
๐ Logging
- INFO: Connection events, key fetches
- ERROR: Redis or SSE issues
- DEBUG: Available if logging level is adjusted
๐ฆ Deployment
To restart the deployment in Kubernetes: kubectl rollout restart -n default deployment apiserver
๐ License
MIT License โ open for use and contributions.