Compare commits

19 Commits
tests ... main

Author SHA1 Message Date
622cf89211 add SQL migration to enable Row Level Security on modules table 2026-03-11 18:35:09 +08:00
e3dba7c6c6 updated the docker files 2026-02-12 02:23:55 +08:00
330ec799d5 updated the caddyfile 2026-02-12 02:17:22 +08:00
82872811db updated the footer 2026-02-12 02:07:04 +08:00
45bae7f01b Updated Caddyfile 2026-02-11 01:52:49 +08:00
9092302033 reverted to an earlier version 2026-02-11 01:45:00 +08:00
3e35b18dce fixed the composed file network 2026-02-11 00:42:22 +08:00
76ed0d2063 Added separate docker compose for local testing 2026-02-11 00:30:40 +08:00
bb8ba326ff Stealth Mode: Disguised JSON as PlainText to bypass corporate filters 2026-02-10 22:32:52 +08:00
692964625d Stealth CORS with Origin Reflection and Reliable Logging 2026-02-10 22:25:12 +08:00
a05c88f6d5 Enabled Caddy logging and added unbuffered health check 2026-02-10 22:20:19 +08:00
29c9f0139e CORS troubleshooting 2026-02-10 22:16:37 +08:00
827dfdcaf5 Fix: Removed redundant FastAPI CORS to prevent duplicate headers 2026-02-10 22:07:42 +08:00
80de744cd6 Fix: Import Request in main.py 2026-02-10 22:00:45 +08:00
0e04626282 Merged Caddy into docker-compose and added edge CORS handling 2026-02-10 21:56:22 +08:00
36530eb2c5 Merged Caddy into docker-compose and added edge CORS handling 2026-02-10 21:34:54 +08:00
e83bb522a4 Full CORS permissive mode and debug logger 2026-02-10 21:29:40 +08:00
b15e39d011 Tweaked CORS 2026-02-10 21:21:42 +08:00
c00396756c Updated the dockercompose 2026-02-10 21:12:43 +08:00
9 changed files with 172 additions and 46 deletions

16
Caddyfile Normal file
View File

@@ -0,0 +1,16 @@
gateway.madebypaulo.com {
log {
output stdout
}
reverse_proxy ai-gateway:8000
}
test.ldex.dev {
reverse_proxy ai_proxy_test:8000
header {
X-Zscaler-Test "Ready"
Access-Control-Allow-Origin *
}
}

View File

@@ -10,16 +10,22 @@ api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
api_key_query = APIKeyQuery(name="api_key", auto_error=False)
# Cache keys for 5 minutes, store up to 1000 keys
# This prevents a Supabase round-trip on every message
auth_cache = TTLCache(maxsize=1000, ttl=300)
async def get_api_key(
api_key_header: str = Security(api_key_header),
api_key_query: str = Security(api_key_query),
api_key_h: str = Security(api_key_header),
api_key_q: str = Security(api_key_query),
db: Session = Depends(get_db)
):
# Use header if provided, otherwise fallback to query param
api_key = api_key_header or api_key_query
api_key = api_key_h or api_key_q
# DEBUG LOGGING FOR 403 TROUBLESHOOTING
print(f"DEBUG: Auth Check - Header: {'Yes' if api_key_h else 'No'}, Query: {'Yes' if api_key_q else 'No'}")
if api_key:
print(f"DEBUG: API Key received (prefix): {api_key[:5]}...")
else:
print("DEBUG: API Key is MISSING from both header and query params")
if not api_key:
raise HTTPException(
@@ -38,7 +44,7 @@ async def get_api_key(
# 3. Check Database for Module key (Database round-trip)
module = db.query(Module).filter(Module.secret_key == api_key, Module.is_active == True).first()
if module:
# Save module ID to cache for next time
# Save key to cache for next time
auth_cache[api_key] = module.id
return module
@@ -48,12 +54,11 @@ async def get_api_key(
)
async def get_current_module(
api_key_header: str = Security(api_key_header),
api_key_query: str = Security(api_key_query),
api_key_h: str = Security(api_key_header),
api_key_q: str = Security(api_key_query),
db: Session = Depends(get_db)
):
# Use header if provided, otherwise fallback to query param
api_key = api_key_header or api_key_query
api_key = api_key_h or api_key_q
if not api_key:
raise HTTPException(

View File

@@ -3,6 +3,7 @@ from app.api.deps import get_api_key, get_current_module
from app.models.module import Module
from sqlalchemy.orm import Session
from app.core.database import get_db
from fastapi.responses import PlainTextResponse
from app.core.limiter import limiter
from app.core.config import settings
from pydantic import BaseModel
@@ -32,7 +33,7 @@ def get_gemini_client():
_client = genai.Client(api_key=settings.GOOGLE_API_KEY, http_options={'api_version': 'v1alpha'})
return _client
@router.post("/chat")
@router.api_route("/chat", methods=["GET", "POST"])
@limiter.limit(settings.RATE_LIMIT)
async def gemini_chat(
request: Request,
@@ -40,7 +41,26 @@ async def gemini_chat(
module: Module = Depends(get_current_module),
db: Session = Depends(get_db)
):
# Handle text/plain as JSON (fallback for CORS "Simple Requests")
chat_data = None
if request.method == "GET":
# Handle GET requests (Ultimate Simple Request)
params = request.query_params
if not params.get("prompt"):
return {"status": "error", "detail": "Missing 'prompt' query parameter"}
chat_data = LLMRequest(
prompt=params.get("prompt"),
context=params.get("context", ""),
system_prompt=params.get("system_prompt"),
knowledge_base=params.get("knowledge_base"),
temperature=float(params.get("temperature", 0.7)),
top_p=float(params.get("top_p", 0.95)),
top_k=int(params.get("top_k", 40)),
max_output_tokens=int(params.get("max_output_tokens", 8192))
)
else:
# Handle POST requests
content_type = request.headers.get("Content-Type", "")
if "text/plain" in content_type:
try:
@@ -58,6 +78,9 @@ async def gemini_chat(
except Exception as e:
return {"status": "error", "detail": f"Invalid JSON: {str(e)}"}
if not chat_data:
return {"status": "error", "detail": "Could not determine request data"}
client = get_gemini_client()
try:
@@ -116,10 +139,14 @@ async def gemini_chat(
module.total_tokens += (prompt_tokens + completion_tokens)
db.commit()
except Exception as e:
return {"status": "error", "detail": str(e)}
# Final Response
return {
"status": "success",
"model": "gemini",
"response": response.text
}
except Exception as e:
return {"status": "error", "detail": str(e)}

View File

@@ -1,8 +1,14 @@
from fastapi import FastAPI
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from slowapi import _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
import logging
# Set up logging to stdout
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("ai_gateway")
from app.api.router import api_router
from app.core.config import settings
from app.core.limiter import limiter
@@ -31,11 +37,20 @@ def create_application() -> FastAPI:
# Mount Static Files
application.mount("/static", StaticFiles(directory="app/static"), name="static")
# Set up CORS
# Debug Logger Middleware
@application.middleware("http")
async def log_requests(request: Request, call_next):
logger.info(f"DEBUG: Incoming {request.method} {request.url}")
logger.info(f"DEBUG: Origin: {request.headers.get('origin')}")
response = await call_next(request)
logger.info(f"DEBUG: Status Code: {response.status_code}")
return response
# Set up fully permissive CORS for standalone/tunnelling mode
application.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False, # Changed to False for better compat with allow_origins=["*"]
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@@ -70,6 +85,10 @@ import os
async def admin_panel():
return FileResponse("app/static/admin.html")
@app.get("/api/v1/health")
async def health_check():
return {"status": "ok", "message": "Global AI Gateway is reachable"}
@app.get("/")
async def root():
return FileResponse("app/static/index.html")

View File

@@ -186,7 +186,7 @@
</div>
<div class="footer">
&copy; 2026 AI Gateway. All rights reserved.
&copy; 2026 Paulo Reyes. All rights reserved.
</div>
</body>

18
docker-compose.test.yml Normal file
View File

@@ -0,0 +1,18 @@
services:
ai-gateway:
build: .
container_name: ai-gateway
expose:
- "8191"
environment:
- PYTHONUNBUFFERED=1
env_file:
- .env
networks:
- gateway_net
restart: always
command: uvicorn app.main:app --host 0.0.0.0 --port 8191
networks:
gateway_net:
external: true

View File

@@ -1,20 +1,37 @@
services:
api:
ai-gateway:
build: .
container_name: ai-gateway
networks:
- caddy_network
ports:
- "8000:8000"
- ai_network
environment:
- PYTHONUNBUFFERED=1
env_file:
- .env
restart: always
command: uvicorn app.main:app --host 0.0.0.0 --port 8000
caddy:
image: caddy:latest
container_name: caddy
restart: always
ports:
- "80:80"
- "443:443"
- "443:443/udp"
networks:
- ai_network
volumes:
- .:/app
# Override command for development/auto-reload if needed
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy_data:/data
- caddy_config:/config
depends_on:
- ai-gateway
networks:
caddy_network:
# Define the network at the bottom
external: true
ai_network:
driver: bridge
volumes:
caddy_data:
caddy_config:

Binary file not shown.

View File

@@ -0,0 +1,24 @@
-- Enable Row Level Security on the modules table.
-- This blocks all direct PostgREST access by default.
-- The backend app connects via the service role (DATABASE_URL), which bypasses RLS,
-- so existing functionality is unaffected.
ALTER TABLE public.modules ENABLE ROW LEVEL SECURITY;
-- Deny all access to anonymous (unauthenticated) PostgREST callers.
-- No policy = no access. This is the default when RLS is enabled, but
-- the explicit policy below makes the intent clear.
CREATE POLICY "deny_anon" ON public.modules
AS RESTRICTIVE
FOR ALL
TO anon
USING (false);
-- Deny all access to authenticated PostgREST callers too.
-- The modules table is internal admin-only and should never be
-- queried directly via the Supabase REST API.
CREATE POLICY "deny_authenticated" ON public.modules
AS RESTRICTIVE
FOR ALL
TO authenticated
USING (false);