File size: 10,198 Bytes
26a0c00
 
 
 
 
 
 
 
 
 
 
e4249bd
5b375d5
 
26a0c00
e4249bd
 
 
26a0c00
e4249bd
5b375d5
9fe37a6
5b375d5
fc1232d
26a0c00
 
 
 
 
 
 
 
 
 
 
36bf090
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26a0c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36bf090
 
 
 
26a0c00
 
 
fc1232d
26a0c00
36bf090
 
 
 
 
 
 
26a0c00
 
 
 
 
 
 
 
 
 
e4249bd
 
 
 
 
 
 
 
 
 
26a0c00
 
 
0cded53
26a0c00
 
 
 
0cded53
26a0c00
 
 
 
 
43c605f
5e9fce0
8509bf4
26a0c00
 
 
 
43c605f
5e9fce0
8509bf4
26a0c00
9fe37a6
 
26a0c00
 
 
 
 
 
 
 
 
 
5b375d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26a0c00
 
b6097b9
 
 
 
 
 
 
 
 
26a0c00
 
 
 
 
 
 
 
 
 
88e04a9
26a0c00
 
 
 
 
 
 
 
 
 
 
 
88e04a9
 
 
 
 
26a0c00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
FastAPI application entry point.
Mounts all routes, configures CORS, and serves the Next.js frontend build.
"""
import os
import logging
from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, JSONResponse
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError

from slowapi.errors import RateLimitExceeded
from slowapi.middleware import SlowAPIMiddleware

from app.config import get_settings
from app.rate_limit import limiter
from app.database import init_db, get_db
from app.observability import setup_prometheus_metrics
from app.rag.vectorstore import get_chroma_client
from app.scheduler import start_scheduler, stop_scheduler

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
logger = logging.getLogger(__name__)

settings = get_settings()


async def document_cleanup_job():
    """Background loop to periodically purge documents not accessed in 30 days."""
    import asyncio
    from datetime import datetime, timedelta, timezone
    logger.info("Starting document cleanup background job loop")
    while True:
        try:
            from app.database import SessionLocal
            from app.models import Document
            from app.rag.vectorstore import delete_document_chunks
            from sqlalchemy import or_
            
            db = SessionLocal()
            try:
                cutoff = datetime.now(timezone.utc) - timedelta(days=30)
                expired_docs = db.query(Document).filter(
                    or_(
                        Document.last_accessed_at < cutoff,
                        Document.last_accessed_at.is_(None) & (Document.uploaded_at < cutoff)
                    )
                ).all()
                
                for doc in expired_docs:
                    logger.info(f"Auto-cleanup: Purging document {doc.id} ('{doc.original_name}') due to inactivity since {doc.last_accessed_at or doc.uploaded_at}")
                    
                    # Delete physical file
                    filepath = os.path.join(settings.UPLOAD_DIR, doc.user_id, doc.filename)
                    if os.path.exists(filepath):
                        try:
                            os.remove(filepath)
                        except Exception as e:
                            logger.warning(f"Auto-cleanup: Failed to delete physical file {filepath}: {e}")
                    
                    # Delete vectors
                    try:
                        delete_document_chunks(document_id=doc.id, user_id=doc.user_id)
                    except Exception as e:
                        logger.warning(f"Auto-cleanup: Error deleting vectors for document {doc.id}: {e}")
                    
                    # Delete database record
                    db.delete(doc)
                
                db.commit()
                if expired_docs:
                    logger.info(f"Auto-cleanup: Purged {len(expired_docs)} documents.")
            except Exception as exc:
                logger.error(f"Auto-cleanup job encountered error: {exc}", exc_info=True)
            finally:
                db.close()
                
        except Exception as e:
            logger.error(f"Error in document cleanup background loop: {e}", exc_info=True)
            
        # Run every 24 hours (86400 seconds)
        await asyncio.sleep(86400)


@asynccontextmanager
async def lifespan(app: FastAPI):
    """Application startup/shutdown lifecycle."""
    # ── Startup ──────────────────────────────────────
    logger.info(f"Starting {settings.APP_NAME}")

    # Create tables
    init_db()
    logger.info("Database initialized")

    # Ensure upload directory exists
    os.makedirs(settings.UPLOAD_DIR, exist_ok=True)
    os.makedirs(settings.CHROMA_PERSIST_DIR, exist_ok=True)

    # Pre-load embedding model (warm up)
    try:
        from app.rag.embeddings import get_embedding_model
        get_embedding_model()
        logger.info("Embedding model pre-loaded")
    except Exception as e:
        logger.warning(f"Failed to pre-load embedding model: {e}")

    # Start background cleanup task
    import asyncio
    cleanup_task = asyncio.create_task(document_cleanup_job())

    yield

    # ── Shutdown ─────────────────────────────────────
    stop_scheduler()
    logger.info("Shutting down")
    cleanup_task.cancel()
    try:
        await cleanup_task
    except asyncio.CancelledError:
        pass
    except Exception as e:
        logger.warning(f"Error cancelling cleanup task: {e}")


# ── Create App ───────────────────────────────────────
app = FastAPI(
    title=settings.APP_NAME,
    description="Enterprise Agentic RAG System β€” Upload PDFs and chat with AI",
    version="2.0.0",
    lifespan=lifespan,
)

app.state.limiter = limiter
app.add_exception_handler(
    RateLimitExceeded,
    lambda request, exc: JSONResponse(
        status_code=429,
        content={"detail": "Rate limit exceeded. Please try again later."},
    ),
)
app.add_middleware(SlowAPIMiddleware)

# ── CORS (allow frontend dev server) ─────────────────
app.add_middleware(
    CORSMiddleware,
    allow_origins=settings.cors_origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
logger.info(f"CORS origins: {settings.cors_origins}")

# ── Mount API Routes ─────────────────────────────────
from app.routes.auth import router as auth_router
from app.routes.documents import router as documents_router
from app.routes.chat import router as chat_router
from app.routes.github import router as github_router
from app.routes.admin import router as admin_router
from app.routes.workspaces import router as workspaces_router

app.include_router(auth_router, prefix="/api/v1")
app.include_router(documents_router, prefix="/api/v1")
app.include_router(chat_router, prefix="/api/v1")
app.include_router(github_router, prefix="/api/v1")
app.include_router(admin_router, prefix="/api/v1")
app.include_router(workspaces_router, prefix="/api/v1")

setup_prometheus_metrics(app)


# ── Health Check ─────────────────────────────────────
@app.get("/api/health")
def health_check():
    return {
        "status": "healthy",
        "app": settings.APP_NAME,
        "version": "2.0.0",
    }

@app.get('/health')
def db_health():
    db_status = "down"
    chroma_status = "down"

    # --- DB check ---
    try:
        db = next(get_db())
        db.execute(select(1))
        db_status = "up"
    except SQLAlchemyError:
        db_status = "down"
    except Exception:
        db_status = "down"

    # --- Chroma check ---
    try:
        chroma = get_chroma_client()
        chroma.heartbeat()
        chroma_status = "up"
    except Exception:
        chroma_status = "down"

    overall_status = "ok" if db_status == "up" and chroma_status == "up" else "degraded"
    return{
        "status": db_status,
        "chroma": chroma_status,
        "db": db_status
    }

# ── Serve Next.js Frontend (production) ──────────────
# In local development, frontend build is at ../../frontend/out relative to backend/app/main.py
# In Docker container (where app is copied to /app/app), frontend build is at /app/frontend/out (which is ../frontend/out relative to /app/app/main.py)
_local_build_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "frontend", "out"))
_docker_build_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "frontend", "out"))

if os.path.exists(_docker_build_dir):
    FRONTEND_BUILD_DIR = _docker_build_dir
else:
    FRONTEND_BUILD_DIR = _local_build_dir

if os.path.exists(FRONTEND_BUILD_DIR):
    # Serve static assets (JS, CSS, images)
    app.mount("/_next", StaticFiles(directory=os.path.join(FRONTEND_BUILD_DIR, "_next")), name="next_static")

    # Serve other static files if they exist
    static_dir = os.path.join(FRONTEND_BUILD_DIR, "static")
    if os.path.exists(static_dir):
        app.mount("/static", StaticFiles(directory=static_dir), name="static")

    @app.api_route("/{full_path:path}", methods=["GET", "HEAD"])
    async def serve_frontend(full_path: str):
        """Serve Next.js static export β€” tries exact file, then .html, then index.html."""
        # Try exact file path
        file_path = os.path.join(FRONTEND_BUILD_DIR, full_path)
        if os.path.isfile(file_path):
            return FileResponse(file_path)

        # Try with .html extension
        html_path = os.path.join(FRONTEND_BUILD_DIR, f"{full_path}.html")
        if os.path.isfile(html_path):
            return FileResponse(html_path)

        # Try .txt for RSC payloads (Next.js uses .txt for RSC data)
        txt_path = os.path.join(FRONTEND_BUILD_DIR, f"{full_path}.txt")
        if os.path.isfile(txt_path):
            return FileResponse(txt_path)

        # Try as directory index
        index_path = os.path.join(FRONTEND_BUILD_DIR, full_path, "index.html")
        if os.path.isfile(index_path):
            return FileResponse(index_path)

        # Fallback to root index.html (SPA routing)
        root_index = os.path.join(FRONTEND_BUILD_DIR, "index.html")
        if os.path.isfile(root_index):
            return FileResponse(root_index)

        return FileResponse(root_index) if os.path.exists(root_index) else {"error": "Not found"}
else:
    logger.info("No frontend build found β€” running in API-only mode")

    @app.get("/")
    def root():
        return {
            "message": f"Welcome to {settings.APP_NAME} API",
            "docs": "/docs",
            "health": "/api/health",
        }