fix docstrings, update env dist

This commit is contained in:
Matteo Rosati
2026-01-21 09:42:13 +01:00
parent b91c09504d
commit 9f5d8a0a88
3 changed files with 77 additions and 14 deletions

50
app.py
View File

@@ -1,12 +1,21 @@
"""FastAPI application for Akern-Genai project.
This module provides the web application with WebSocket support
for streaming responses from the Gemini model.
"""
import os
import logging
from typing import Annotated
from fastapi import FastAPI, Request, WebSocket, Depends, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from main import generate
# Configure logging format and level
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
@@ -15,20 +24,26 @@ logging.basicConfig(
logger = logging.getLogger(__name__)
# Static files configuration
STATIC_DIR: str = "static"
TEMPLATES_DIR: str = "templates"
STATIC_DIR = "static"
TEMPLATES_DIR = "templates"
# Security configuration
security = HTTPBasic()
app = FastAPI()
app.mount(f"/{STATIC_DIR}", StaticFiles(directory=STATIC_DIR), name="static")
def verify_credentials(credentials: HTTPBasicCredentials = Depends(security)) -> str:
"""Verify HTTP Basic credentials against environment variables.
templates = Jinja2Templates(directory=os.path.join(STATIC_DIR, TEMPLATES_DIR))
Args:
credentials: HTTP Basic authentication credentials.
Returns:
str: The authenticated username.
def verify_credentials(credentials: HTTPBasicCredentials = Depends(security)):
Raises:
HTTPException: If credentials are invalid.
"""
correct_username = os.getenv("BASIC_AUTH_USERNAME")
correct_password = os.getenv("BASIC_AUTH_PASSWORD")
@@ -44,13 +59,34 @@ def verify_credentials(credentials: HTTPBasicCredentials = Depends(security)):
return credentials.username
# Initialize FastAPI application
app = FastAPI()
app.mount(f"/{STATIC_DIR}", StaticFiles(directory=STATIC_DIR), name="static")
templates = Jinja2Templates(directory=os.path.join(STATIC_DIR, TEMPLATES_DIR))
@app.get("/")
async def home(request: Request, username: Annotated[str, Depends(verify_credentials)]):
"""Render the main index page.
Args:
request: The incoming request object.
username: The authenticated username from HTTP Basic auth.
Returns:
TemplateResponse: The rendered HTML template.
"""
return templates.TemplateResponse("index.html", {"request": request})
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
"""Handle WebSocket connections for streaming responses.
Args:
websocket: The WebSocket connection.
"""
await websocket.accept()
while True:
data = await websocket.receive_text()