Spaces:
Running
Running
from fastapi import FastAPI, HTTPException, Depends | |
from fastapi.middleware.cors import CORSMiddleware | |
from typing import Dict, Any | |
import logging | |
from config import get_settings | |
from services import storage_service | |
import json | |
settings = get_settings() | |
# Configure logging | |
logging.basicConfig( | |
level=logging.DEBUG if settings.DEBUG else logging.INFO, | |
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", | |
) | |
logger = logging.getLogger(__name__) | |
app = FastAPI(title="Profile API", description="API to retrieve profile information") | |
# Enable CORS - Enhanced for better external access | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], # Allow all origins | |
allow_credentials=True, | |
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"], | |
allow_headers=["*"], | |
expose_headers=["Content-Type", "X-Total-Count"], | |
) | |
# Add health check endpoint | |
async def health_check(): | |
""" | |
Health check endpoint to verify the API server is running | |
""" | |
return {"status": "ok", "message": "API server is running"} | |
async def get_profile(profile_id: int) -> Dict[str, Any]: | |
""" | |
Retrieve a profile by its ID | |
Args: | |
profile_id: The ID of the profile | |
Returns: | |
The profile document if found | |
""" | |
try: | |
# Retrieve the profile from SQLite | |
profile = storage_service.get_profile(profile_id) | |
# Check if profile exists | |
if not profile: | |
logger.warning(f"Profile not found: {profile_id}") | |
raise HTTPException(status_code=404, detail=f"Profile with ID {profile_id} not found") | |
logger.debug(f"Retrieved profile: {profile_id}") | |
return profile | |
except HTTPException: | |
# Re-raise HTTP exceptions | |
raise | |
except Exception as e: | |
# Handle any other exceptions | |
logger.error(f"Error retrieving profile {profile_id}: {e}") | |
raise HTTPException(status_code=500, detail=f"Error retrieving profile: {str(e)}") | |
async def get_profile_image(profile_id: int) -> Dict[str, Any]: | |
""" | |
Retrieve just the profile image for a given profile ID | |
Args: | |
profile_id: The ID of the profile | |
Returns: | |
JSON object with the profile image data | |
""" | |
try: | |
# Retrieve the profile from SQLite | |
profile = storage_service.get_profile(profile_id) | |
if not profile: | |
logger.warning(f"Profile not found: {profile_id}") | |
raise HTTPException(status_code=404, detail=f"Profile with ID {profile_id} not found") | |
profile_img = profile.get("profileImg") | |
if not profile_img: | |
return {"profileImg": None} | |
return {"profileImg": profile_img} | |
except HTTPException: | |
raise | |
except Exception as e: | |
logger.error(f"Error retrieving profile image {profile_id}: {e}") | |
raise HTTPException(status_code=500, detail=f"Error retrieving profile image: {str(e)}") | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run("api:app", host="0.0.0.0", port=8000, reload=True) | |