File size: 1,100 Bytes
9b87a98
 
 
 
 
 
 
 
 
 
 
 
 
54ea453
 
9b87a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# main.py
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.core.config import settings
from app.api.v1.api_router import api_router
from app.db.session import close_mongo_connection, connect_to_mongo

app = FastAPI(
    title="Multi-Tenant Chat API",
    openapi_url=f"{settings.API_V1_STR}/openapi.json"
)

origins = [
    "http://localhost:3000",
    "https://dialexus-chatapp.vercel.app"
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.on_event("startup")
async def startup_event():
    """
    Connect to MongoDB on startup.
    """
    await connect_to_mongo()

@app.on_event("shutdown")
async def shutdown_event():
    """
    Close MongoDB connection on shutdown.
    """
    await close_mongo_connection()


# Include the API router
app.include_router(api_router, prefix=settings.API_V1_STR)

@app.get("/")
def read_root():
    """
    Root endpoint for basic health check.
    """
    return {"message": "Welcome to the Multi-Tenant Chat API"}