-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (74 loc) · 2.71 KB
/
Copy pathmain.py
File metadata and controls
87 lines (74 loc) · 2.71 KB
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
import logging
from contextlib import asynccontextmanager
from typing import Any, AsyncGenerator
from fastapi import FastAPI
from slowapi import _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from slowapi.middleware import SlowAPIMiddleware
from starlette.middleware.cors import CORSMiddleware
from app.core.config import settings
from app.core.database import init_db
from app.core.limiter import limiter
from app.routes import (
auth_routes,
paper_routes,
project_routes,
search_routes,
user_routes,
)
# ---------------------------------------------------------
# Configure Logging
# ---------------------------------------------------------
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
logger = logging.getLogger("inquiro")
# ---------------------------------------------------------
# Lifespan Event Handlers (Modern FastAPI)
# ---------------------------------------------------------
@asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncGenerator[None, Any]:
"""Initialize and tear down application resources."""
logger.info("🚀 Starting Inquiro API in '%s' mode...", settings.ENVIRONMENT)
if settings.ENVIRONMENT == "dev":
await init_db() # Auto-create tables only in dev
logger.info("✅ Startup complete.")
yield
logger.info("🛑 Shutting down Inquiro API...")
logger.info("👋 Shutdown complete.")
# ---------------------------------------------------------
# Initialize FastAPI
# ---------------------------------------------------------
app = FastAPI(
title="Inquiro API",
description="AI-powered research discovery backend for Inquiro.",
version="0.1.0",
lifespan=lifespan,
)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
app.add_middleware(SlowAPIMiddleware)
# ---------------------------------------------------------
# Development-only CORS configuration
# ---------------------------------------------------------
if settings.ENVIRONMENT == "dev":
logger.info("🌐 Enabling CORS for local development...")
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:5173",
"http://127.0.0.1:5173",
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ---------------------------------------------------------
# Register Routers
# ---------------------------------------------------------
app.include_router(user_routes.router)
app.include_router(auth_routes.router)
app.include_router(search_routes.router)
app.include_router(project_routes.router)
app.include_router(paper_routes.router)