mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: use env variables to load db
This commit is contained in:
parent
ef5fcd5870
commit
8cc6111aaf
4 changed files with 22 additions and 6 deletions
|
|
@ -2,17 +2,20 @@ import logging
|
|||
from fastapi import FastAPI
|
||||
import motor.motor_asyncio
|
||||
|
||||
async def connect_to_db(app: FastAPI) :
|
||||
|
||||
async def connect_to_db(app: FastAPI):
|
||||
logging.info("Connecting to database...")
|
||||
try:
|
||||
app.mongodb_client = motor.motor_asyncio.AsyncIOMotorClient("mongodb://learnhouse:learnhouse@mongo:27017/") # type: ignore
|
||||
app.db = app.mongodb_client["learnhouse"] # type: ignore
|
||||
app.mongodb_client = motor.motor_asyncio.AsyncIOMotorClient( # type: ignore
|
||||
app.learnhouse_config.database_config.mongodb_connection_string) # type: ignore
|
||||
app.db = app.mongodb_client["learnhouse"] # type: ignore
|
||||
logging.info("Connected to database!")
|
||||
except Exception as e:
|
||||
logging.error("Failed to connect to database!")
|
||||
logging.error(e)
|
||||
|
||||
|
||||
async def close_database(app: FastAPI):
|
||||
app.mongodb_client.close() # type: ignore
|
||||
app.mongodb_client.close() # type: ignore
|
||||
logging.info("LearnHouse has been shut down.")
|
||||
return app
|
||||
return app
|
||||
|
|
|
|||
|
|
@ -1,11 +1,16 @@
|
|||
from typing import Callable
|
||||
from fastapi import FastAPI
|
||||
from config.config import LearnHouseConfig, get_learnhouse_config
|
||||
from src.core.events.database import close_database, connect_to_db
|
||||
from src.core.events.logs import create_logs_dir
|
||||
|
||||
|
||||
def startup_app(app: FastAPI) -> Callable:
|
||||
async def start_app() -> None:
|
||||
# Get LearnHouse Config
|
||||
learnhouse_config: LearnHouseConfig = get_learnhouse_config()
|
||||
app.learnhouse_config = learnhouse_config # type: ignore
|
||||
|
||||
# Connect to database
|
||||
await connect_to_db(app)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue