๐ŸŒ 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:

๐Ÿงฉ Features

๐Ÿ—บ๏ธ 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

  1. Install dependencies
pip install fastapi uvicorn redis sse-starlette
  1. 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

๐Ÿง  Web Control Commands

Accepted commands via /webcontrol/{command}:

{
  "type": "webcontrol",
  "command": "pp"
}

๐Ÿ““ Logging

๐Ÿ“ฆ Deployment

To restart the deployment in Kubernetes: kubectl rollout restart -n default deployment apiserver

๐Ÿ“š License

MIT License โ€” open for use and contributions.