Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ai_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import base64
import glob
import json
import logging
import os
import subprocess
from typing import Optional
Expand All @@ -14,6 +15,10 @@

from .redis_helper import REDIS_CONNECTION

# Configure logger
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(name)s - %(message)s')
logger = logging.getLogger('ai-server')

# Load environment variables from .env file
load_dotenv()

Expand Down Expand Up @@ -227,11 +232,15 @@ def chat_with_model(
def authenticate() -> str:
"""Authenticate the given request using an API key."""
api_key = request.headers.get('X-API-KEY')
client_ip = request.remote_addr
endpoint = request.path
if not api_key:
logger.warning(f"Missing API key from {client_ip} at {endpoint}")
abort(401, description="Missing API key")

user = REDIS_CONNECTION.get(f"api-key:{api_key}")
if not user:
logger.warning(f"Invalid API key attempt from {client_ip} at {endpoint}")
abort(401, description="Invalid API key")

return user
Expand Down