File size: 2,405 Bytes
480f467
f2ce243
56edde7
 
 
480f467
a8f12f6
 
480f467
 
 
 
 
a8f12f6
56edde7
f2ce243
 
 
 
 
 
 
 
1915c66
f2ce243
 
 
1915c66
 
f2ce243
1915c66
 
f2ce243
 
 
 
 
 
1915c66
f2ce243
 
 
56edde7
480f467
 
f2ce243
 
56edde7
 
480f467
56edde7
 
 
 
 
 
 
 
480f467
a8f12f6
56edde7
480f467
56edde7
 
480f467
56edde7
480f467
 
 
a8f12f6
480f467
 
 
 
a8f12f6
56edde7
 
f2ce243
480f467
a8f12f6
480f467
 
 
 
 
 
 
 
 
5574ac6
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from api.routes import router
from config.settings import settings

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

logger = logging.getLogger(__name__)


@asynccontextmanager
async def lifespan(app: FastAPI):
    """
    Lifespan event handler for startup and shutdown
    """
    # Startup
    logger.info("=" * 50)
    logger.info(f"Starting {settings.api_title} v{settings.api_version}")
    logger.info("=" * 50)
    
    if settings.groq_api_key:
        logger.info(f"Groq API Key: Configured (length: {len(settings.groq_api_key)})")
        logger.info(f"Groq Model: {settings.groq_model}")
    else:
        logger.error("GROQ_API_KEY is NOT set!")
        logger.error("WARNING: API will NOT function without valid API key")
    
    logger.info("=" * 50)
    
    yield
    
    # Shutdown
    logger.info("Shutting down API...")


# Create FastAPI app with lifespan
app = FastAPI(
    title=settings.api_title,
    version=settings.api_version,
    description=settings.api_description,
    lifespan=lifespan
)

# CORS configuration
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Include routes
app.include_router(router, prefix="/api")


@app.get("/")
async def root():
    """Root endpoint"""
    return {
        "service": settings.api_title,
        "version": settings.api_version,
        "status": "running",
        "endpoints": {
            "docs": "/docs",
            "health": "/api/v1/health",
            "coding": "/api/coding",
            "upload": "/api/upload-file"
        }
    }


@app.get("/api/v1/health")
async def health_check():
    """Health check endpoint"""
    api_key_status = "configured" if settings.groq_api_key else "missing"
    
    return {
        "status": "healthy",
        "service": settings.api_title,
        "version": settings.api_version,
        "groq_api_key": api_key_status,
        "groq_model": settings.groq_model
    }


# Run server when executed directly
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=7860,
        reload=False
    )