File size: 2,584 Bytes
a496aae
 
 
 
 
 
 
f97bb86
 
c1e34f1
f97bb86
a496aae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f97bb86
a496aae
 
f97bb86
a496aae
f97bb86
 
 
 
a496aae
f97bb86
a496aae
 
 
c1dde32
a496aae
c1dde32
a496aae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.v1 import repositories, chat
from app.core.database import engine, Base
from datetime import datetime
import os
import logging

# Import all models so SQLAlchemy registers them before creating tables
from app.models import Repository, Conversation, Message, CodeFile

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI(
    title="QODEX API",
    description="AI-powered code repository chat system",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

# CORS configuration for production
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "https://qodex.vercel.app",              # Your frontend domain
        "https://qodex-frontend.vercel.app",     # Alternative frontend domain
        "http://localhost:3000",                 # Local development
        "http://127.0.0.1:3000",                # Local development
    ],
    allow_credentials=True,
    allow_methods=["GET", "POST", "PUT", "DELETE"],
    allow_headers=["*"],
)

# Load models on startup (but don't create tables - managed via Neon console)
@app.on_event("startup")
async def startup_event():
    """Load models so SQLAlchemy relationships work"""
    try:
        # Models are already imported at top of file
        # This just ensures SQLAlchemy registry is ready
        logger.info("πŸ—„οΈ Database models loaded successfully")
        logger.info("πŸ’‘ Note: Tables are managed via Neon console, not auto-created")
    except Exception as e:
        logger.error(f"❌ Error loading models: {e}")

# Health check endpoint
@app.get("/health")
@app.head("/health")
async def health_check():
    """Health check endpoint for monitoring services (GET and HEAD)"""
    return {
        "status": "healthy",
        "timestamp": datetime.utcnow().isoformat(),
        "service": "QODEX API",
        "version": "1.0.0",
        "environment": os.getenv("ENVIRONMENT", "production"),
        "message": "QODEX is running smoothly! πŸš€"
    }

@app.get("/")
async def root():
    """Root endpoint"""
    return {
        "message": "Welcome to QODEX API! πŸš€",
        "description": "AI-powered code repository chat system",
        "docs": "/docs",
        "health": "/health",
        "status": "running",
        "version": "1.0.0"
    }

# Include routers
app.include_router(repositories.router, prefix="/api/v1/repositories", tags=["repositories"])
app.include_router(chat.router, prefix="/api/v1/chat", tags=["chat"])