-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
58 lines (47 loc) · 1.46 KB
/
main.py
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
import logging
from api import chat, tools, webhooks
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from lib.startup import init_background_tasks
# Load environment variables first
load_dotenv()
# Configure module logger
logger = logging.getLogger("uvicorn.error")
app = FastAPI()
# Setup CORS origins
cors_origins = [
"https://sprint.aibtc.dev",
"https://sprint-faster.aibtc.dev",
"https://*.aibtcdev-frontend.pages.dev", # Cloudflare preview deployments
"http://localhost:3000", # Local development
"https://staging.aibtc.chat",
"https://app.aibtc.dev",
"https://aibtc.dev",
"https://app-staging.aibtc.dev",
]
# Setup middleware to allow CORS
app.add_middleware(
CORSMiddleware,
allow_origins=cors_origins,
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=["*"],
max_age=3600, # Cache preflight requests for 1 hour
)
@app.on_event("startup")
async def startup_event():
"""Initialize services on startup."""
try:
await init_background_tasks()
logger.info("Background tasks initialized")
except Exception as e:
logger.error(f"Error during startup: {e}")
raise e
# Lightweight health check endpoint
@app.get("/")
async def health():
return {"status": "healthy"}
app.include_router(tools.router)
app.include_router(chat.router)
app.include_router(webhooks.router)