Initial commit
This commit is contained in:
507
conf/app.py
Normal file
507
conf/app.py
Normal file
@@ -0,0 +1,507 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
FastAPI application with multiple endpoints that publish to NATS JetStream
|
||||
Each endpoint corresponds to an external API that workers will forward to
|
||||
"""
|
||||
import os
|
||||
import json
|
||||
import asyncio
|
||||
from fastapi import FastAPI, HTTPException, Request, Body
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.requests import Request as StarletteRequest
|
||||
from starlette.responses import Response as StarletteResponse
|
||||
from pydantic import BaseModel
|
||||
import nats
|
||||
import nats.errors
|
||||
|
||||
from prometheus_client import Counter, Histogram, generate_latest, REGISTRY
|
||||
from starlette.responses import Response
|
||||
import uvicorn
|
||||
from typing import Optional, Dict, Any, List, Union
|
||||
|
||||
# Prometheus metrics
|
||||
request_count = Counter('http_requests_total', 'Total HTTP requests', ['method', 'endpoint', 'status'])
|
||||
request_duration = Histogram('http_request_duration_seconds', 'HTTP request duration', ['method', 'endpoint'])
|
||||
|
||||
app = FastAPI(title="NATS Backend API - Multi-Endpoint", version="1.0.0")
|
||||
|
||||
# Custom CORS middleware to ensure headers are always added
|
||||
class CORSHeaderMiddleware(BaseHTTPMiddleware):
|
||||
async def dispatch(self, request: StarletteRequest, call_next):
|
||||
origin = request.headers.get("origin", "*")
|
||||
response = await call_next(request)
|
||||
response.headers["Access-Control-Allow-Origin"] = origin
|
||||
response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, PATCH, DELETE, OPTIONS"
|
||||
response.headers["Access-Control-Allow-Headers"] = "*"
|
||||
response.headers["Access-Control-Allow-Credentials"] = "false"
|
||||
response.headers["Access-Control-Max-Age"] = "600"
|
||||
return response
|
||||
|
||||
# Add custom CORS middleware first
|
||||
app.add_middleware(CORSHeaderMiddleware)
|
||||
|
||||
# Also add FastAPI's CORS middleware as backup
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origin_regex=r".*", # Match all origins
|
||||
allow_credentials=False,
|
||||
allow_methods=["*"], # Allow all HTTP methods
|
||||
allow_headers=["*"], # Allow all headers
|
||||
expose_headers=["*"], # Expose all headers
|
||||
max_age=600,
|
||||
)
|
||||
|
||||
# NATS connection (will be initialized on startup)
|
||||
nc = None
|
||||
js = None
|
||||
|
||||
# Configuration
|
||||
NATS_URL = os.getenv("NATS_URL", "nats://nats-server:4222")
|
||||
NATS_USER = os.getenv("NATS_USER", "admin")
|
||||
NATS_PASSWORD = os.getenv("NATS_PASSWORD", "package@321#")
|
||||
|
||||
# Endpoint to NATS subject mapping
|
||||
ENDPOINT_ROUTES = {
|
||||
"/live/api/v1/deliveries/createdeliveries": "api.v1.deliveries.createdeliveries",
|
||||
"/live/api/v1/deliveries/updatedelivery": "api.v1.deliveries.updatedelivery",
|
||||
"/live/api/v2/partners/createriderlog": "api.v2.partners.createriderlog",
|
||||
"/live/api/v2/deliveries/createdeliverylog": "api.v2.deliveries.createdeliverylog",
|
||||
"/live/api/v2/partners/createbreaklog": "api.v2.partners.createbreaklog",
|
||||
"/live/api/v2/partners/updatebreaklog": "api.v2.partners.updatebreaklog",
|
||||
"/live/api/v1/mob/orders/createorder": "api.v1.mob.orders.createorder",
|
||||
"/live/api/v1/web/products/create": "api.v1.web.products.create",
|
||||
# Customer Endpoints (Sync)
|
||||
"/live/api/v1/mob/customers/login": "api.v1.mob.customers.login",
|
||||
"/live/api/v1/mob/customers/create": "api.v1.mob.customers.create",
|
||||
}
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
"""Initialize NATS connection on startup"""
|
||||
global nc, js
|
||||
try:
|
||||
print(f"Connecting to NATS at {NATS_URL}...")
|
||||
nc = await nats.connect(
|
||||
servers=[NATS_URL],
|
||||
user=NATS_USER,
|
||||
password=NATS_PASSWORD,
|
||||
reconnect_time_wait=2,
|
||||
max_reconnect_attempts=10
|
||||
)
|
||||
js = nc.jetstream()
|
||||
print("✅ Connected to NATS JetStream")
|
||||
print(f"✅ Configured {len(ENDPOINT_ROUTES)} endpoint routes")
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to connect to NATS: {e}")
|
||||
raise
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def shutdown():
|
||||
"""Close NATS connection on shutdown"""
|
||||
global nc
|
||||
if nc:
|
||||
await nc.close()
|
||||
print("NATS connection closed")
|
||||
|
||||
@app.options("/{full_path:path}")
|
||||
async def options_handler(full_path: str, request: Request):
|
||||
"""Handle OPTIONS requests for CORS preflight"""
|
||||
origin = request.headers.get("origin")
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content={},
|
||||
headers={
|
||||
"Access-Control-Allow-Origin": origin if origin else "*",
|
||||
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "*",
|
||||
"Access-Control-Allow-Credentials": "false",
|
||||
"Access-Control-Max-Age": "600",
|
||||
}
|
||||
)
|
||||
|
||||
@app.get("/health")
|
||||
async def health():
|
||||
"""Health check endpoint"""
|
||||
return {"status": "healthy", "nats_connected": nc.is_connected if nc else False}
|
||||
|
||||
@app.get("/ready")
|
||||
async def ready():
|
||||
"""Readiness check endpoint"""
|
||||
if nc and nc.is_connected:
|
||||
return {"status": "ready"}
|
||||
raise HTTPException(status_code=503, detail="Not ready")
|
||||
|
||||
async def publish_to_nats(endpoint: str, data: Union[Dict[str, Any], List[Dict[str, Any]]], request_method: str = "POST"):
|
||||
"""Publish message to NATS with endpoint metadata"""
|
||||
payload = {
|
||||
"endpoint": endpoint,
|
||||
"method": request_method,
|
||||
"data": data,
|
||||
"received_at": int(asyncio.get_event_loop().time() * 1000),
|
||||
"original_path": endpoint
|
||||
}
|
||||
|
||||
# Get NATS subject for this endpoint
|
||||
subject = ENDPOINT_ROUTES.get(endpoint, "api.unknown")
|
||||
|
||||
if js:
|
||||
try:
|
||||
ack = await js.publish(subject, json.dumps(payload).encode())
|
||||
return ack.seq
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to publish to NATS: {e}")
|
||||
raise HTTPException(status_code=500, detail=f"Failed to publish message: {str(e)}")
|
||||
else:
|
||||
raise HTTPException(status_code=503, detail="NATS not connected")
|
||||
|
||||
async def publish_request_to_nats(endpoint: str, data: Dict[str, Any], request_method: str = "POST", timeout: int = 10):
|
||||
"""
|
||||
Publish to NATS and WAIT for a reply (Request-Reply pattern).
|
||||
Used for synchronous endpoints like Login.
|
||||
"""
|
||||
payload = {
|
||||
"endpoint": endpoint,
|
||||
"method": request_method,
|
||||
"data": data,
|
||||
"received_at": int(asyncio.get_event_loop().time() * 1000),
|
||||
"original_path": endpoint
|
||||
}
|
||||
|
||||
subject = ENDPOINT_ROUTES.get(endpoint, "api.unknown")
|
||||
|
||||
if not js:
|
||||
raise HTTPException(status_code=503, detail="NATS by connected")
|
||||
|
||||
try:
|
||||
# Create a unique inbox for the reply
|
||||
inbox = nc.new_inbox()
|
||||
|
||||
# Subscribe to the inbox first
|
||||
sub = await nc.subscribe(inbox, max_msgs=1)
|
||||
|
||||
# Publish request with reply inbox
|
||||
# Note: We use js.publish to ensure it goes to the Stream (Queue), but attach a reply subject
|
||||
await js.publish(subject, json.dumps(payload).encode(), reply=inbox)
|
||||
|
||||
# Wait for valid response
|
||||
try:
|
||||
msg = await sub.next_msg(timeout=timeout)
|
||||
response_data = json.loads(msg.data.decode())
|
||||
return response_data
|
||||
except nats.errors.TimeoutError:
|
||||
raise HTTPException(status_code=504, detail="Gateway Timeout: Upstream service did not respond in time")
|
||||
finally:
|
||||
await sub.unsubscribe()
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to request from NATS: {e}")
|
||||
raise HTTPException(status_code=500, detail=f"RPC Error: {str(e)}")
|
||||
|
||||
# Endpoint 1: Update Delivery (v1) - External API requires PUT
|
||||
@app.put("/live/api/v1/deliveries/updatedelivery")
|
||||
async def update_delivery_v1(data: Dict[str, Any], request: Request):
|
||||
"""Update Delivery endpoint - forwards to NATS (PUT only, as external API requires PUT)"""
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
endpoint = "/live/api/v1/deliveries/updatedelivery"
|
||||
method = "PUT" # Always use PUT for this endpoint
|
||||
|
||||
try:
|
||||
message_id = await publish_to_nats(endpoint, data, method)
|
||||
duration = asyncio.get_event_loop().time() - start_time
|
||||
request_duration.labels(method="PUT", endpoint=endpoint).observe(duration)
|
||||
request_count.labels(method="PUT", endpoint=endpoint, status="200").inc()
|
||||
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content={"status": "accepted", "message_id": message_id, "endpoint": endpoint}
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
request_count.labels(method="PUT", endpoint=endpoint, status="500").inc()
|
||||
print(f"❌ Error processing {endpoint}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
# Endpoint: Create Deliveries (v1)
|
||||
@app.post("/live/api/v1/deliveries/createdeliveries")
|
||||
async def create_deliveries_v1(data: Union[Dict[str, Any], List[Dict[str, Any]]], request: Request):
|
||||
"""Create Deliveries endpoint - forwards payload to NATS"""
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
endpoint = "/live/api/v1/deliveries/createdeliveries"
|
||||
method = request.method
|
||||
|
||||
try:
|
||||
message_id = await publish_to_nats(endpoint, data, method)
|
||||
duration = asyncio.get_event_loop().time() - start_time
|
||||
request_duration.labels(method=method, endpoint=endpoint).observe(duration)
|
||||
request_count.labels(method=method, endpoint=endpoint, status="200").inc()
|
||||
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content={"status": "accepted", "message_id": message_id, "endpoint": endpoint}
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
request_count.labels(method=method, endpoint=endpoint, status="500").inc()
|
||||
print(f"❌ Error processing {endpoint}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
# Endpoint 2: Create Rider Log (v2)
|
||||
@app.post("/live/api/v2/partners/createriderlog")
|
||||
async def create_rider_log_v2(data: Dict[str, Any], request: Request):
|
||||
"""Create Rider Log endpoint - forwards to NATS"""
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
endpoint = "/live/api/v2/partners/createriderlog"
|
||||
method = request.method
|
||||
|
||||
try:
|
||||
message_id = await publish_to_nats(endpoint, data, method)
|
||||
duration = asyncio.get_event_loop().time() - start_time
|
||||
request_duration.labels(method=method, endpoint=endpoint).observe(duration)
|
||||
request_count.labels(method=method, endpoint=endpoint, status="200").inc()
|
||||
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content={"status": "accepted", "message_id": message_id, "endpoint": endpoint}
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
request_count.labels(method=method, endpoint=endpoint, status="500").inc()
|
||||
print(f"❌ Error processing {endpoint}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
# Endpoint 3: Create Delivery Log (v2)
|
||||
@app.post("/live/api/v2/deliveries/createdeliverylog")
|
||||
async def create_delivery_log_v2(
|
||||
request: Request,
|
||||
data: List[Dict[str, Any]] = Body(
|
||||
...,
|
||||
example=[{
|
||||
"logid": 0,
|
||||
"tenantid": 1,
|
||||
"partnerid": 44,
|
||||
"locationid": 1,
|
||||
"orderheaderid": 123456,
|
||||
"deliveryid": 654321,
|
||||
"userid": 1111,
|
||||
"orderid": "1-20231624",
|
||||
"orderstatus": "active",
|
||||
"starttime": "2025-12-10 17:51:03",
|
||||
"logdate": "2025-12-10 18:14:04",
|
||||
"latitude": "11.0050664",
|
||||
"longitude": "76.9508776"
|
||||
}]
|
||||
)
|
||||
):
|
||||
"""
|
||||
Create Delivery Log endpoint - forwards to NATS.
|
||||
Accepts either a single dict or a list of dicts to align with external API expectations.
|
||||
"""
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
endpoint = "/live/api/v2/deliveries/createdeliverylog"
|
||||
method = request.method
|
||||
|
||||
try:
|
||||
# Expect only a list of dicts
|
||||
if not isinstance(data, list) or not all(isinstance(item, dict) for item in data):
|
||||
raise HTTPException(status_code=422, detail="Body must be a list of objects")
|
||||
|
||||
normalized_data = data
|
||||
|
||||
message_id = await publish_to_nats(endpoint, normalized_data, method)
|
||||
duration = asyncio.get_event_loop().time() - start_time
|
||||
request_duration.labels(method=method, endpoint=endpoint).observe(duration)
|
||||
request_count.labels(method=method, endpoint=endpoint, status="200").inc()
|
||||
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content={"status": "accepted", "message_id": message_id, "endpoint": endpoint}
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
request_count.labels(method=method, endpoint=endpoint, status="500").inc()
|
||||
print(f"❌ Error processing {endpoint}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
# Endpoint 4: Create Break Rider Log (v2)
|
||||
@app.post("/live/api/v2/partners/createbreaklog")
|
||||
async def create_break_log_v2(data: Dict[str, Any], request: Request):
|
||||
"""Create Break Rider Log endpoint - forwards to NATS"""
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
endpoint = "/live/api/v2/partners/createbreaklog"
|
||||
method = request.method
|
||||
|
||||
try:
|
||||
message_id = await publish_to_nats(endpoint, data, method)
|
||||
duration = asyncio.get_event_loop().time() - start_time
|
||||
request_duration.labels(method=method, endpoint=endpoint).observe(duration)
|
||||
request_count.labels(method=method, endpoint=endpoint, status="200").inc()
|
||||
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content={"status": "accepted", "message_id": message_id, "endpoint": endpoint}
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
request_count.labels(method=method, endpoint=endpoint, status="500").inc()
|
||||
print(f"❌ Error processing {endpoint}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
# Endpoint 5: Update Break Rider Log (v2) - Supports both POST and PUT
|
||||
@app.post("/live/api/v2/partners/updatebreaklog")
|
||||
@app.put("/live/api/v2/partners/updatebreaklog")
|
||||
async def update_break_log_v2(data: Dict[str, Any], request: Request):
|
||||
"""Update Break Rider Log endpoint - forwards to NATS"""
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
endpoint = "/live/api/v2/partners/updatebreaklog"
|
||||
method = request.method # Will be POST or PUT
|
||||
|
||||
try:
|
||||
message_id = await publish_to_nats(endpoint, data, method)
|
||||
duration = asyncio.get_event_loop().time() - start_time
|
||||
request_duration.labels(method=method, endpoint=endpoint).observe(duration)
|
||||
request_count.labels(method=method, endpoint=endpoint, status="200").inc()
|
||||
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content={"status": "accepted", "message_id": message_id, "endpoint": endpoint}
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
request_count.labels(method=method, endpoint=endpoint, status="500").inc()
|
||||
print(f"❌ Error processing {endpoint}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@app.get("/metrics")
|
||||
async def metrics():
|
||||
"""Prometheus metrics endpoint"""
|
||||
return Response(content=generate_latest(REGISTRY), media_type="text/plain")
|
||||
|
||||
@app.get("/routes")
|
||||
async def list_routes():
|
||||
"""List all configured routes"""
|
||||
return {
|
||||
"routes": ENDPOINT_ROUTES,
|
||||
"total": len(ENDPOINT_ROUTES)
|
||||
}
|
||||
|
||||
|
||||
# Endpoint 6: Create Order (Mob V1)
|
||||
@app.post("/live/api/v1/mob/orders/createorder")
|
||||
async def create_order_mob_v1(data: Dict[str, Any], request: Request):
|
||||
"""Create Order endpoint (Mobile) - forwards to NATS"""
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
endpoint = "/live/api/v1/mob/orders/createorder"
|
||||
method = request.method
|
||||
|
||||
try:
|
||||
message_id = await publish_to_nats(endpoint, data, method)
|
||||
duration = asyncio.get_event_loop().time() - start_time
|
||||
request_duration.labels(method=method, endpoint=endpoint).observe(duration)
|
||||
request_count.labels(method=method, endpoint=endpoint, status="200").inc()
|
||||
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content={"status": "accepted", "message_id": message_id, "endpoint": endpoint}
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
request_count.labels(method=method, endpoint=endpoint, status="500").inc()
|
||||
print(f"❌ Error processing {endpoint}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
# Endpoint 7: Create Product (Web V1)
|
||||
@app.post("/live/api/v1/web/products/create")
|
||||
async def create_product_web_v1(data: Dict[str, Any], request: Request):
|
||||
"""Create Product endpoint (Web) - forwards to NATS"""
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
endpoint = "/live/api/v1/web/products/create"
|
||||
method = request.method
|
||||
|
||||
try:
|
||||
message_id = await publish_to_nats(endpoint, data, method)
|
||||
duration = asyncio.get_event_loop().time() - start_time
|
||||
request_duration.labels(method=method, endpoint=endpoint).observe(duration)
|
||||
request_count.labels(method=method, endpoint=endpoint, status="200").inc()
|
||||
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content={"status": "accepted", "message_id": message_id, "endpoint": endpoint}
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
request_count.labels(method=method, endpoint=endpoint, status="500").inc()
|
||||
print(f"❌ Error processing {endpoint}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
|
||||
# Endpoint 8: Customer Login (Sync Request-Reply)
|
||||
@app.post("/live/api/v1/mob/customers/login")
|
||||
async def customer_login(data: Dict[str, Any], request: Request):
|
||||
"""Customer Login - Waits for response from worker"""
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
endpoint = "/live/api/v1/mob/customers/login"
|
||||
method = request.method
|
||||
|
||||
try:
|
||||
# Wait for reply!
|
||||
response_data = await publish_request_to_nats(endpoint, data, method)
|
||||
|
||||
duration = asyncio.get_event_loop().time() - start_time
|
||||
request_duration.labels(method=method, endpoint=endpoint).observe(duration)
|
||||
request_count.labels(method=method, endpoint=endpoint, status="200").inc()
|
||||
|
||||
# Return the actual backend response
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content=response_data
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
request_count.labels(method=method, endpoint=endpoint, status="500").inc()
|
||||
print(f"❌ Error processing {endpoint}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
# Endpoint 9: Customer Create (Sync Request-Reply)
|
||||
@app.post("/live/api/v1/mob/customers/create")
|
||||
async def customer_create(data: Dict[str, Any], request: Request):
|
||||
"""Customer Create - Waits for response from worker"""
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
endpoint = "/live/api/v1/mob/customers/create"
|
||||
method = request.method
|
||||
|
||||
try:
|
||||
# Wait for reply!
|
||||
response_data = await publish_request_to_nats(endpoint, data, method)
|
||||
|
||||
duration = asyncio.get_event_loop().time() - start_time
|
||||
request_duration.labels(method=method, endpoint=endpoint).observe(duration)
|
||||
request_count.labels(method=method, endpoint=endpoint, status="200").inc()
|
||||
|
||||
return JSONResponse(
|
||||
status_code=200,
|
||||
content=response_data
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
request_count.labels(method=method, endpoint=endpoint, status="500").inc()
|
||||
print(f"❌ Error processing {endpoint}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
|
||||
27
conf/nginx-atlantis.conf
Normal file
27
conf/nginx-atlantis.conf
Normal file
@@ -0,0 +1,27 @@
|
||||
events {}
|
||||
http {
|
||||
upstream atlantis_k8s {
|
||||
server host.docker.internal:30825;
|
||||
server 66.116.226.234:30825 backup;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8205;
|
||||
|
||||
location / {
|
||||
# Proxy to Kubernetes NodePort (30825)
|
||||
proxy_pass http://atlantis_k8s;
|
||||
|
||||
# Using $http_host and explicitly setting headers to ensure backend gets correct metadata
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
# Connection settings
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
}
|
||||
}
|
||||
}
|
||||
24
conf/nginx-doormile.conf
Normal file
24
conf/nginx-doormile.conf
Normal file
@@ -0,0 +1,24 @@
|
||||
events {}
|
||||
http {
|
||||
upstream doormile_k8s {
|
||||
server 66.116.225.226:30830;
|
||||
server 66.116.226.234:30830 backup;
|
||||
}
|
||||
server {
|
||||
listen 8206;
|
||||
location / {
|
||||
proxy_pass http://doormile_k8s;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
# WebSocket support
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_read_timeout 3600s;
|
||||
proxy_send_timeout 3600s;
|
||||
}
|
||||
}
|
||||
}
|
||||
23
conf/nginx-fiesta.conf
Normal file
23
conf/nginx-fiesta.conf
Normal file
@@ -0,0 +1,23 @@
|
||||
events {}
|
||||
http {
|
||||
upstream fiesta_k8s {
|
||||
# Your main server where NodePort is exposed
|
||||
server host.docker.internal:30823;
|
||||
# Your new server acting as a backup
|
||||
server 66.116.226.234:30823 backup;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8204;
|
||||
|
||||
location / {
|
||||
# Proxy to Kubernetes NodePort (30823) for Fiesta
|
||||
proxy_pass http://fiesta_k8s;
|
||||
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
}
|
||||
}
|
||||
37
conf/nginx-jupiter.conf
Normal file
37
conf/nginx-jupiter.conf
Normal file
@@ -0,0 +1,37 @@
|
||||
events {}
|
||||
http {
|
||||
upstream jupiter_k8s {
|
||||
server 66.116.225.226:30822;
|
||||
server 66.116.226.234:30822 backup;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8203;
|
||||
|
||||
location / {
|
||||
# Proxy to Kubernetes NodePort (30822)
|
||||
# NodePorts are bound to 0.0.0.0 and are more reliable to access from host.docker.internal
|
||||
proxy_pass http://jupiter_k8s;
|
||||
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
# CORS configuration - "The Backend Fix"
|
||||
add_header 'Access-Control-Allow-Origin' '*' always;
|
||||
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, PATCH, DELETE' always;
|
||||
add_header 'Access-Control-Allow-Headers' '*' always;
|
||||
|
||||
if ($request_method = 'OPTIONS') {
|
||||
add_header 'Access-Control-Allow-Origin' '*' always;
|
||||
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, PATCH, DELETE' always;
|
||||
add_header 'Access-Control-Allow-Headers' '*' always;
|
||||
add_header 'Access-Control-Max-Age' 1728000;
|
||||
add_header 'Content-Type' 'text/plain; charset=utf-8';
|
||||
add_header 'Content-Length' 0;
|
||||
return 204;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
33
conf/nginx-k8s-dashboard.conf
Normal file
33
conf/nginx-k8s-dashboard.conf
Normal file
@@ -0,0 +1,33 @@
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
upstream k8s_dashboard {
|
||||
server host.docker.internal:30826;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8083;
|
||||
server_name _;
|
||||
|
||||
location / {
|
||||
proxy_pass http://k8s_dashboard;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
|
||||
# WebSocket support (if needed)
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
|
||||
# Timeouts
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 60s;
|
||||
proxy_read_timeout 60s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
51
conf/nginx-queue-proxy.conf
Normal file
51
conf/nginx-queue-proxy.conf
Normal file
@@ -0,0 +1,51 @@
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
upstream k8s_lb {
|
||||
# Alaska deliveries LoadBalancer NodePort (8201 service -> 30662 node port)
|
||||
server host.docker.internal:30662;
|
||||
server 66.116.226.234:30662 backup;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8201;
|
||||
server_name _;
|
||||
|
||||
location / {
|
||||
# Handle OPTIONS preflight requests BEFORE proxying
|
||||
if ($request_method = 'OPTIONS') {
|
||||
add_header 'Access-Control-Allow-Origin' '*' always;
|
||||
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, PATCH, DELETE, OPTIONS' always;
|
||||
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
|
||||
add_header 'Access-Control-Max-Age' 1728000 always;
|
||||
add_header 'Content-Type' 'text/plain; charset=utf-8' always;
|
||||
add_header 'Content-Length' 0 always;
|
||||
return 204;
|
||||
}
|
||||
|
||||
# Add CORS headers to all responses
|
||||
add_header 'Access-Control-Allow-Origin' '*' always;
|
||||
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, PATCH, DELETE, OPTIONS' always;
|
||||
add_header 'Access-Control-Allow-Headers' '*' always;
|
||||
add_header 'Access-Control-Max-Age' '600' always;
|
||||
add_header 'Vary' 'Origin' always;
|
||||
|
||||
# Proxy to Kubernetes LoadBalancer
|
||||
proxy_pass http://k8s_lb;
|
||||
|
||||
# Hide upstream CORS headers to avoid duplicates with Nginx headers
|
||||
proxy_hide_header 'Access-Control-Allow-Origin';
|
||||
proxy_hide_header 'Access-Control-Allow-Methods';
|
||||
proxy_hide_header 'Access-Control-Allow-Headers';
|
||||
proxy_hide_header 'Access-Control-Max-Age';
|
||||
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
254
conf/worker.py
Normal file
254
conf/worker.py
Normal file
@@ -0,0 +1,254 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
NATS Worker - "New Idea" Implementation
|
||||
- Single Domain per Worker (Stream + Consumer)
|
||||
- Persistent HTTP Session (Connection Pooling)
|
||||
- JetStream Native Retries (NAK)
|
||||
- No internal retry loops
|
||||
"""
|
||||
import os
|
||||
import json
|
||||
import asyncio
|
||||
import aiohttp
|
||||
import nats
|
||||
from nats.errors import TimeoutError as NatsTimeoutError
|
||||
from prometheus_client import Counter, Histogram, Gauge, start_http_server
|
||||
import signal
|
||||
import sys
|
||||
from typing import Any, Dict, List, Union, Tuple
|
||||
|
||||
# Prometheus metrics
|
||||
messages_processed = Counter('worker_messages_processed_total', 'Total messages processed', ['status', 'endpoint'])
|
||||
message_duration = Histogram('worker_message_duration_seconds', 'Message processing duration', ['endpoint'])
|
||||
messages_in_flight = Gauge('worker_messages_in_flight', 'Messages currently being processed')
|
||||
|
||||
# Configuration
|
||||
NATS_URL = os.getenv("NATS_URL", "nats://nats-server:4222")
|
||||
NATS_USER = os.getenv("NATS_USER", "admin")
|
||||
NATS_PASSWORD = os.getenv("NATS_PASSWORD", "package@321#")
|
||||
|
||||
# Domain Configuration (Env Vars from Deployment)
|
||||
NATS_STREAM = os.getenv("NATS_STREAM", "ORDERS")
|
||||
NATS_CONSUMER = os.getenv("NATS_CONSUMER", "orders-worker")
|
||||
# Supports comma-separated subjects: "api.v1.orders.*,api.v2.orders.*"
|
||||
FILTER_SUBJECTS = os.getenv("FILTER_SUBJECT", "api.v1.mob.orders.*").split(",")
|
||||
|
||||
WORKER_CONCURRENCY = int(os.getenv("WORKER_CONCURRENCY", "10"))
|
||||
BASE_URL = os.getenv("EXTERNAL_BASE_URL", "https://jupiter.nearle.app")
|
||||
|
||||
# Endpoint mapping is still useful for constructing the target URL
|
||||
ENDPOINT_MAPPING = {
|
||||
"/live/api/v1/deliveries/createdeliveries": f"{BASE_URL}/live/api/v1/deliveries/createdeliveries",
|
||||
"/live/api/v1/deliveries/updatedelivery": f"{BASE_URL}/live/api/v1/deliveries/updatedelivery",
|
||||
"/live/api/v2/partners/createriderlog": f"{BASE_URL}/live/api/v2/partners/createriderlog",
|
||||
"/live/api/v2/deliveries/createdeliverylog": f"{BASE_URL}/live/api/v2/deliveries/createdeliverylog",
|
||||
"/live/api/v2/partners/createbreaklog": f"{BASE_URL}/live/api/v2/partners/createbreaklog",
|
||||
"/live/api/v2/partners/updatebreaklog": f"{BASE_URL}/live/api/v2/partners/updatebreaklog",
|
||||
"/live/api/v1/mob/orders/createorder": f"{BASE_URL}/live/api/v1/mob/orders/createorder",
|
||||
"/live/api/v1/web/products/create": f"{BASE_URL}/live/api/v1/web/products/create",
|
||||
"/live/api/v1/mob/customers/login": f"{BASE_URL}/live/api/v1/mob/customers/login",
|
||||
"/live/api/v1/mob/customers/create": f"{BASE_URL}/live/api/v1/mob/customers/create",
|
||||
}
|
||||
|
||||
# Global State
|
||||
nc = None
|
||||
js = None
|
||||
session: aiohttp.ClientSession = None
|
||||
running = True
|
||||
semaphore = None # Initialized in main
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
global running
|
||||
print("\n?? Shutdown signal received...")
|
||||
running = False
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
|
||||
async def forward_to_external(endpoint: str, payload: dict, api_key: str = None, method: str = "POST") -> tuple[str, Any]:
|
||||
"""
|
||||
Forward to external API using global session.
|
||||
Returns (status_code, response_data).
|
||||
Does NOT retry internally.
|
||||
"""
|
||||
external_url = ENDPOINT_MAPPING.get(endpoint)
|
||||
if not external_url:
|
||||
print(f"?? Unknown endpoint: {endpoint}, dropping.")
|
||||
return "DROP", None
|
||||
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if api_key:
|
||||
headers["Authorization"] = f"Bearer {api_key}"
|
||||
|
||||
data_to_forward = payload.get("data", payload)
|
||||
|
||||
# Payload Normalization for logs
|
||||
if endpoint == "/live/api/v2/deliveries/createdeliverylog":
|
||||
if isinstance(data_to_forward, dict):
|
||||
data_to_forward = [data_to_forward]
|
||||
elif not isinstance(data_to_forward, list):
|
||||
data_to_forward = [data_to_forward]
|
||||
|
||||
http_method = method.upper() if method else "POST"
|
||||
|
||||
try:
|
||||
# We use the global 'session' here
|
||||
async with session.request(
|
||||
method=http_method,
|
||||
url=external_url,
|
||||
json=data_to_forward if http_method != "GET" else None,
|
||||
params=data_to_forward if http_method == "GET" else None,
|
||||
headers=headers,
|
||||
timeout=aiohttp.ClientTimeout(total=30)
|
||||
) as response:
|
||||
|
||||
body = None
|
||||
try:
|
||||
body = await response.json()
|
||||
except:
|
||||
body = await response.text()
|
||||
|
||||
if 200 <= response.status < 300:
|
||||
return "OK", body
|
||||
elif response.status >= 500:
|
||||
print(f"?? Server Error {response.status} from {endpoint}")
|
||||
return "RETRY", body
|
||||
else:
|
||||
print(f"?? Client Error {response.status} from {endpoint}: {body}")
|
||||
return "DROP", body # 4xx errors should generally not be retried infinitely
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
print(f"?? Timeout calling {endpoint}")
|
||||
return "RETRY", None
|
||||
except Exception as e:
|
||||
print(f"?? Network/Client Error calling {endpoint}: {e}")
|
||||
return "RETRY", None
|
||||
|
||||
async def process_message(msg):
|
||||
async with semaphore:
|
||||
messages_in_flight.inc()
|
||||
start_time = asyncio.get_event_loop().time()
|
||||
endpoint = "unknown"
|
||||
|
||||
try:
|
||||
payload = json.loads(msg.data.decode())
|
||||
endpoint = payload.get("endpoint", payload.get("original_path", "unknown"))
|
||||
http_method = payload.get("method", "POST")
|
||||
|
||||
print(f"?? Processing {endpoint}")
|
||||
|
||||
api_key = os.getenv("EXTERNAL_ENDPOINT_API_KEY", "")
|
||||
|
||||
# Single attempt
|
||||
status, response_data = await forward_to_external(endpoint, payload, api_key, http_method)
|
||||
|
||||
if status == "OK":
|
||||
await msg.ack()
|
||||
messages_processed.labels(status="success", endpoint=endpoint).inc()
|
||||
|
||||
# Request-Reply Logic
|
||||
if msg.reply:
|
||||
reply_payload = json.dumps(response_data) if isinstance(response_data, (dict, list)) else str(response_data)
|
||||
await nc.publish(msg.reply, reply_payload.encode())
|
||||
|
||||
elif status == "DROP":
|
||||
# Unrecoverable error or unknown endpoint
|
||||
await msg.term() # Terminate stops redelivery
|
||||
messages_processed.labels(status="dropped", endpoint=endpoint).inc()
|
||||
|
||||
else: # RETRY
|
||||
# Let JetStream handle backoff
|
||||
await msg.nak(delay=2) # Custom delay before redelivery if desired, or just nak()
|
||||
messages_processed.labels(status="retried", endpoint=endpoint).inc()
|
||||
|
||||
except json.JSONDecodeError:
|
||||
print("?? Invalid JSON, terminating message")
|
||||
await msg.term()
|
||||
except Exception as e:
|
||||
print(f"? Critical Worker Error: {e}")
|
||||
await msg.nak()
|
||||
finally:
|
||||
messages_in_flight.dec()
|
||||
duration = asyncio.get_event_loop().time() - start_time
|
||||
message_duration.labels(endpoint=endpoint).observe(duration)
|
||||
|
||||
async def main():
|
||||
global nc, js, session, semaphore
|
||||
|
||||
print(f"?? Starting Worker for Domain: {NATS_CONSUMER}")
|
||||
print(f"?? Stream: {NATS_STREAM}, Subjects: {FILTER_SUBJECTS}")
|
||||
print(f"?? Concurrency: {WORKER_CONCURRENCY}")
|
||||
|
||||
semaphore = asyncio.Semaphore(WORKER_CONCURRENCY)
|
||||
session = aiohttp.ClientSession()
|
||||
|
||||
try:
|
||||
nc = await nats.connect(
|
||||
servers=[NATS_URL],
|
||||
user=NATS_USER,
|
||||
password=NATS_PASSWORD,
|
||||
reconnect_time_wait=2,
|
||||
max_reconnect_attempts=-1
|
||||
)
|
||||
js = nc.jetstream()
|
||||
print("? Connected to NATS JetStream")
|
||||
|
||||
# Create Pull Subscriptions for each filter subject
|
||||
# All sharing the same Consumer Name ensuring load balancing if multiple pods run this
|
||||
|
||||
subs = []
|
||||
for subject in FILTER_SUBJECTS:
|
||||
subject = subject.strip()
|
||||
if not subject: continue
|
||||
|
||||
# FIX: On WorkQueue streams, we cannot reuse the same durable name for different filters
|
||||
# We append a sanitized version of the subject to ensure uniqueness per filter
|
||||
clean_subject_suffix = subject.replace(".", "_").replace("*", "all").replace(">", "all")
|
||||
durable_name = f"{NATS_CONSUMER}_{clean_subject_suffix}"
|
||||
|
||||
print(f"?? Subscribing to {subject} on durable consumer '{durable_name}'")
|
||||
try:
|
||||
sub = await js.pull_subscribe(
|
||||
subject,
|
||||
durable=durable_name,
|
||||
stream=NATS_STREAM
|
||||
)
|
||||
subs.append(sub)
|
||||
except Exception as e:
|
||||
print(f"? Failed to subscribe to {subject}: {e}")
|
||||
|
||||
if not subs:
|
||||
print("? No active subscriptions!")
|
||||
return
|
||||
|
||||
start_http_server(9090)
|
||||
print("?? Metrics on :9090")
|
||||
|
||||
while running:
|
||||
# Poll all subscriptions
|
||||
for sub in subs:
|
||||
try:
|
||||
# Batch size 10, short timeout to keep loop responsive
|
||||
msgs = await sub.fetch(10, timeout=0.5)
|
||||
for m in msgs:
|
||||
asyncio.create_task(process_message(m))
|
||||
except NatsTimeoutError:
|
||||
pass
|
||||
except Exception as e:
|
||||
print(f"?? Fetch Error: {e}")
|
||||
await asyncio.sleep(1)
|
||||
|
||||
# Small sleep to prevent tight loop if no messages
|
||||
# await asyncio.sleep(0.01)
|
||||
|
||||
except Exception as e:
|
||||
print(f"?? Fatal Error: {e}")
|
||||
finally:
|
||||
if session:
|
||||
await session.close()
|
||||
if nc:
|
||||
await nc.close()
|
||||
print("?? Worker Shutdown")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user