File size: 2,607 Bytes
bdf20e7
7b5352e
 
bdf20e7
7b5352e
 
 
 
 
 
 
 
bdf20e7
7b5352e
 
bdf20e7
 
 
7b5352e
 
dff6fcd
 
 
7b5352e
bdf20e7
7b5352e
 
 
 
9b5a2e4
7b5352e
236f036
 
 
bdf20e7
 
236f036
9257463
236f036
 
bbcb666
bdf20e7
 
7b5352e
 
bdf20e7
63f56d4
 
dad455c
 
 
63f56d4
 
 
 
a506cc3
 
 
 
 
 
7b5352e
 
190fb26
 
 
 
 
 
 
 
 
 
63f56d4
190fb26
 
 
bdf20e7
 
7b5352e
 
63f56d4
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
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles

# Ensure the 'app' directory is always discoverable in sys.path
APP_DIR = Path(__file__).resolve().parent
if str(APP_DIR) not in sys.path:
    sys.path.insert(0, str(APP_DIR))

from routers import api_router
from utils.logger import app_logger

load_dotenv()

# App initialization
app = FastAPI(
    title="Atlas - Document Intelligence API",
    description="Atlas Hierarchical Tree Search Document Analysis API with Multimodal Support",
    version="2.0.0",
)

# Static files for extracted document images
STATIC_DIR = os.path.join(APP_DIR, "static")
os.makedirs(STATIC_DIR, exist_ok=True)
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")

# CORS middleware
_raw_cors = os.getenv("CORS_ALLOW_ORIGINS", "http://localhost:5173,http://localhost:3000")
cors_origins = [origin.strip() for origin in _raw_cors.split(",") if origin.strip()]

app.add_middleware(
    CORSMiddleware,
    allow_origins=cors_origins,
    allow_origin_regex=os.getenv("CORS_ALLOW_ORIGIN_REGEX", r"https://.*\.vercel\.app|https://.*\.hf\.space"),
    allow_credentials=False,
    allow_methods=["GET", "POST", "PATCH", "DELETE", "OPTIONS"],
    allow_headers=["*"],
)

# Register API routers
app.include_router(api_router)

# Mount FastMCP Server (SSE & HTTP protocol for external MCP clients)
try:
    from mcp_server.web_search_mcp import mcp as research_mcp
    app.mount("/mcp", research_mcp.http_app())
    app_logger.info("FastMCP Research & Study Server mounted successfully at /mcp")
except Exception as mcp_err:
    app_logger.warning(f"Could not mount FastMCP at /mcp: {mcp_err}")


@app.get("/", tags=["Health"])
async def root():
    """Public root endpoint."""
    return {"message": "Atlas - Document Intelligence API", "docs": "/docs", "health": "/health"}


@app.get("/health", tags=["Health"])
async def health_check():
    """Health check endpoint to verify service availability and database status."""
    from core.persistent_db import DB_STATUS, persistent_db
    try:
        docs = persistent_db.list_docs()
        count = len(docs)
    except Exception:
        count = 0
    return {
        "status": "ok",
        "service": "Vectorless RAG API",
        "fastmcp": "active",
        "database": DB_STATUS,
        "docs_count": count,
    }


@app.on_event("startup")
async def on_startup():
    app_logger.info("Vectorless RAG API service started with FastMCP Web Search enabled.")