File size: 1,246 Bytes
76ea68f
 
 
 
 
 
269b3c2
92f19f5
302d8c7
76ea68f
00662bb
76ea68f
 
 
 
92f19f5
 
 
 
76ea68f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00662bb
76ea68f
 
 
b2fc939
 
00662bb
269b3c2
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
import os
from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from src.controllers import api_router, websocket_router
from src.config import logger, DatabaseConfig
from src.middlewares import AuthenticationMiddleware


@asynccontextmanager
async def lifespan(app: FastAPI):
    try:
        logger.info("Starting up the application...")

        mongo_config = DatabaseConfig()
        await mongo_config.init_beanie()

        logger.info("Application started successfully...")
        yield
    except Exception as e:
        logger.error(f"Error during startup: {str(e)}")
        raise
    finally:
        logger.info("Shutting down the application...")
        logger.info("Application shutdown complete.")


app = FastAPI(lifespan=lifespan)

app.add_middleware(
    CORSMiddleware,
    allow_origins=os.getenv(
        "CORS_ALLOW_ORIGINS", "http://localhost, http://127.0.0.1"
    ).split(", "),
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)



@app.get("/")
async def check_health():
    return {"response": "Service is healthy!"}


app.include_router(api_router, prefix="/api")
app.include_router(websocket_router, prefix="/ws")