This commit is contained in:
Slipstream 2025-05-14 17:32:17 -06:00
parent d53808268d
commit e720d14853
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD

View File

@ -402,7 +402,15 @@ app = FastAPI(title="Unified API Service", lifespan=lifespan, debug=True)
@app.exception_handler(StarletteHTTPException)
async def teapot_override(request: Request, exc: StarletteHTTPException):
# Check if this is a request to the openrouter_key endpoint
if request.url.path == "/openrouter_key" or request.url.path.endswith("/openrouter_key"):
# Don't convert 404 errors for the openrouter_key endpoint to 418
log.warning(f"Exception in openrouter_key endpoint: {exc.status_code} - {exc.detail}")
raise exc
# For all other 404 errors, return a teapot response
if exc.status_code == 404:
log.info(f"Converting 404 to 418 teapot for path: {request.url.path}")
html_content = """
<!DOCTYPE html>
<html>
@ -645,6 +653,56 @@ async def openrouter_key(request: Request):
print("Unauthorized attempt to access OpenRouter key.")
raise HTTPException(status_code=403, detail="Forbidden")
# Add debug logging
log.info(f"OpenRouter key request authorized. AI_API_KEY is {'set' if settings.AI_API_KEY else 'not set'}")
# Check if AI_API_KEY is set
if not settings.AI_API_KEY:
log.error("AI_API_KEY is not set in environment variables")
raise HTTPException(status_code=500, detail="AI_API_KEY not configured")
return f"{settings.AI_API_KEY}"
# Add the same endpoint to the api_app to ensure it's accessible
@api_app.get("/openrouter_key", response_class=PlainTextResponse)
async def api_openrouter_key(request: Request):
"""private endpoint return openrouter api key (api_app version)"""
# Basic security check
auth_header = request.headers.get("Authorization")
# Use loaded setting
if not settings.MOD_LOG_API_SECRET or not auth_header or auth_header != f"Bearer {settings.MOD_LOG_API_SECRET}":
print("Unauthorized attempt to access OpenRouter key (api_app).")
raise HTTPException(status_code=403, detail="Forbidden")
# Add debug logging
log.info(f"OpenRouter key request authorized (api_app). AI_API_KEY is {'set' if settings.AI_API_KEY else 'not set'}")
# Check if AI_API_KEY is set
if not settings.AI_API_KEY:
log.error("AI_API_KEY is not set in environment variables")
raise HTTPException(status_code=500, detail="AI_API_KEY not configured")
return f"{settings.AI_API_KEY}"
# Add the same endpoint to the discordapi_app to ensure it's accessible
@discordapi_app.get("/openrouter_key", response_class=PlainTextResponse)
async def discordapi_openrouter_key(request: Request):
"""private endpoint return openrouter api key (discordapi_app version)"""
# Basic security check
auth_header = request.headers.get("Authorization")
# Use loaded setting
if not settings.MOD_LOG_API_SECRET or not auth_header or auth_header != f"Bearer {settings.MOD_LOG_API_SECRET}":
print("Unauthorized attempt to access OpenRouter key (discordapi_app).")
raise HTTPException(status_code=403, detail="Forbidden")
# Add debug logging
log.info(f"OpenRouter key request authorized (discordapi_app). AI_API_KEY is {'set' if settings.AI_API_KEY else 'not set'}")
# Check if AI_API_KEY is set
if not settings.AI_API_KEY:
log.error("AI_API_KEY is not set in environment variables")
raise HTTPException(status_code=500, detail="AI_API_KEY not configured")
return f"{settings.AI_API_KEY}"
@app.get("/discord")
@ -663,6 +721,34 @@ async def ip(request: Request):
async def agent(request: Request):
return Response(content=request.headers.get("user-agent", request.client.host), media_type="text/plain")
@app.get("/debug-settings", response_class=PlainTextResponse)
async def debug_settings(request: Request):
"""Debug endpoint to check if settings are loaded correctly"""
# Basic security check - only allow from localhost or with the same auth as openrouter_key
client_host = request.client.host
auth_header = request.headers.get("Authorization")
is_local = client_host == "127.0.0.1" or client_host == "::1" or client_host.startswith("172.")
is_authorized = auth_header and settings.MOD_LOG_API_SECRET and auth_header == f"Bearer {settings.MOD_LOG_API_SECRET}"
if not (is_local or is_authorized):
print(f"Unauthorized attempt to access debug settings from {client_host}.")
raise HTTPException(status_code=403, detail="Forbidden")
# Return a summary of the settings
settings_summary = [
f"API_HOST: {settings.API_HOST}",
f"API_PORT: {settings.API_PORT}",
f"MOD_LOG_API_SECRET: {'set' if settings.MOD_LOG_API_SECRET else 'not set'}",
f"AI_API_KEY: {'set' if settings.AI_API_KEY else 'not set'}",
f"DISCORD_CLIENT_ID: {'set' if settings.DISCORD_CLIENT_ID else 'not set'}",
f"DISCORD_CLIENT_SECRET: {'set' if settings.DISCORD_CLIENT_SECRET else 'not set'}",
f"DISCORD_REDIRECT_URI: {settings.DISCORD_REDIRECT_URI}",
f"POSTGRES_SETTINGS_DB: {settings.POSTGRES_SETTINGS_DB}",
f"REDIS_HOST: {settings.REDIS_HOST}",
]
return "\n".join(settings_summary)
# Add root for dashboard API for clarity
@dashboard_api_app.get("/")
async def dashboard_api_root():