Spaces:
Build error
Build error
File size: 5,430 Bytes
4b1daed | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | # API Gateway for RAG Agent Framework
A production-ready, multi-protocol API Gateway for the AmaniQuery RAG Agent Framework. Provides unified access for frontend clients with comprehensive security, observability, and performance features.
## Architecture

```mermaid
graph TB
subgraph "Client Layer"
WEB[Web Browser]
MOBILE[Mobile App]
DEVELOPER[Developer Portal]
end
subgraph "API Gateway Layer"
GW[API Gateway<br/>Go Service]
subgraph "Middleware Stack"
CORS[CORS Handler]
RATE[Rate Limiter]
AUTH[JWT Validator]
AUDIT[Audit Logger]
end
subgraph "Protocol Handlers"
REST[REST Handler]
WS[WebSocket Handler]
GQL[GraphQL Handler]
end
end
subgraph "Backend Services"
AGENT[Agent Service]
RETRIEVER[Retriever Service]
GENERATOR[Generator Service]
MEMORY[Memory Service]
end
WEB --> GW
MOBILE --> GW
DEVELOPER --> GW
GW --> CORS --> RATE --> AUTH --> AUDIT
AUDIT --> REST
AUDIT --> WS
AUDIT --> GQL
REST --> AGENT
WS --> AGENT
GQL --> AGENT
```
## Features
### Multi-Protocol Support
- **REST API** - Standard HTTP endpoints for queries, agents, memory
- **WebSocket** - Real-time streaming for query responses
- **Server-Sent Events** - Lightweight streaming alternative
- **GraphQL** - Flexible query interface (placeholder)
### Security
- **JWT Authentication** - Token-based auth with HMAC/RSA signing
- **OPA Authorization** - Fine-grained policy-based access control
- **Rate Limiting** - Token bucket with per-tenant/user isolation
- **CORS** - Configurable cross-origin policies
- **Security Headers** - HSTS, CSP, X-Frame-Options, etc.
### Observability
- **Prometheus Metrics** - Request counts, latencies, cache hits
- **OpenTelemetry Tracing** - Distributed request tracing
- **Audit Logging** - Structured logs for compliance
### Performance
- **Redis Caching** - Query response caching with smart TTL
- **Circuit Breakers** - Failure isolation per service
- **Connection Pooling** - Efficient gRPC connections
## Quick Start
### Prerequisites
- Go 1.21+
- Docker & Docker Compose
- Redis (for caching/rate limiting)
### Running Locally
```bash
# Clone the repository
cd AmaniQuery
# Copy example config
cp gateway.example.yaml gateway.yaml
# Run with Docker Compose
cd deployments/docker
docker-compose up -d api-gateway
```
### Configuration
See `gateway.example.yaml` for all options. Key settings:
```yaml
server:
bindAddr: ":8443"
auth:
jwtSecret: "${JWT_SECRET}"
cache:
redisAddr: "redis:6379"
```
Environment variables override config with `GATEWAY_` prefix.
## API Endpoints
### Queries
| Method | Path | Description |
|--------|------|-------------|
| POST | `/v2/queries` | Execute RAG query |
| GET | `/v2/queries/{id}` | Get async query result |
| WS | `/v2/queries/stream` | Streaming query |
### Agents
| Method | Path | Description |
|--------|------|-------------|
| POST | `/v2/agents` | Create agent |
| GET | `/v2/agents/{id}` | Get agent |
| DELETE | `/v2/agents/{id}` | Delete agent |
| POST | `/v2/agents/{id}/execute` | Execute plan |
### Memory
| Method | Path | Description |
|--------|------|-------------|
| GET | `/v2/memory/context` | Get context window |
| POST | `/v2/memory/sessions/{id}/consolidate` | Consolidate memory |
### Admin
| Method | Path | Description |
|--------|------|-------------|
| GET | `/admin/health` | Health check |
| GET | `/admin/metrics` | Prometheus metrics |
## WebSocket Protocol
```javascript
// Connect
const ws = new WebSocket('wss://api.example.com/v2/queries/stream?token=JWT');
// Send query
ws.send(JSON.stringify({
type: 'query',
payload: { query: 'What is RAG?', userId: 'user-123' }
}));
// Receive chunks
ws.onmessage = (e) => {
const msg = JSON.parse(e.data);
if (msg.type === 'chunk') console.log(msg.data);
if (msg.type === 'done') console.log('Complete');
};
```
## Deployment
### Docker
```bash
docker build -f deployments/docker/Dockerfile.gateway -t api-gateway .
docker run -p 8443:8443 api-gateway
```
### Kubernetes
```bash
kubectl apply -f deployments/k8s/gateway.yaml
```
## Project Structure
```
internal/gateway/
├── config.go # Configuration
├── gateway.go # Main server
├── types.go # Request/response types
├── cache/
│ └── cache.go # Redis cache
├── handlers/
│ ├── query.go # Query endpoints
│ ├── websocket.go # WebSocket streaming
│ ├── agent.go # Agent CRUD
│ ├── memory.go # Memory endpoints
│ ├── admin.go # Health/metrics
│ └── auth.go # Token endpoint
├── middleware/
│ ├── cors.go # CORS handling
│ ├── ratelimit.go # Rate limiting
│ ├── auth.go # JWT + OPA auth
│ ├── audit.go # Audit logging
│ └── tracing.go # OpenTelemetry
├── observability/
│ └── metrics.go # Prometheus metrics
└── services/
├── registry.go # Service discovery
└── clients.go # gRPC clients
```
## License
Apache 2.0
|