-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
33 lines (21 loc) · 830 Bytes
/
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
import os
from dotenv import load_dotenv
from loguru import logger
from fastapi import FastAPI
from tasks.routes import router as tasks_router
from users.routes import router as users_router
from motor.motor_asyncio import AsyncIOMotorClient
load_dotenv()
# ----------- LOAD ROUTERS ----------- #
app = FastAPI()
app.include_router(tasks_router, prefix="/tasks", tags=["tasks"])
app.include_router(users_router, prefix="/users", tags=["users"])
# ----------- DATABASE CONNECTION ----------- #
async def open_db() -> AsyncIOMotorClient:
app.state.mongodb = AsyncIOMotorClient(os.getenv("MONGODB_URL"))
logger.info("CONNECTED TO MONGODB!")
async def close_db():
app.state.mongodb.close()
logger.info("CLOSED MONGODB!")
app.add_event_handler("startup", open_db)
app.add_event_handler("shutdown", close_db)