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
|
|
@ -1,3 +1,4 @@
|
|||
from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
import os
|
||||
import yaml
|
||||
|
|
@ -19,6 +20,7 @@ class DatabaseConfig(BaseModel):
|
|||
user: str
|
||||
password: str
|
||||
database_name: str
|
||||
mongodb_connection_string: Optional[str]
|
||||
|
||||
|
||||
class LearnHouseConfig(BaseModel):
|
||||
|
|
@ -54,6 +56,8 @@ def get_learnhouse_config() -> LearnHouseConfig:
|
|||
env_user = os.environ.get('LEARNHOUSE_DB_USER')
|
||||
env_password = os.environ.get('LEARNHOUSE_DB_PASSWORD')
|
||||
env_database_name = os.environ.get('LEARNHOUSE_DB_NAME')
|
||||
env_mongodb_connection_string = os.environ.get(
|
||||
'LEARNHOUSE_MONGODB_CONNECTION_STRING')
|
||||
|
||||
# Fill in values with YAML file if they are not provided
|
||||
site_name = env_site_name or yaml_config.get('site_name')
|
||||
|
|
@ -80,6 +84,8 @@ def get_learnhouse_config() -> LearnHouseConfig:
|
|||
'database_config', {}).get('password')
|
||||
database_name = env_database_name or yaml_config.get(
|
||||
'database_config', {}).get('database_name')
|
||||
mongodb_connection_string = env_mongodb_connection_string or yaml_config.get(
|
||||
'database_config', {}).get('mongodb_connection_string')
|
||||
|
||||
# Create HostingConfig and DatabaseConfig objects
|
||||
hosting_config = HostingConfig(
|
||||
|
|
@ -96,7 +102,8 @@ def get_learnhouse_config() -> LearnHouseConfig:
|
|||
port=int(db_port),
|
||||
user=user,
|
||||
password=password,
|
||||
database_name=database_name
|
||||
database_name=database_name,
|
||||
mongodb_connection_string=mongodb_connection_string
|
||||
)
|
||||
|
||||
# Create LearnHouseConfig object
|
||||
|
|
|
|||
|
|
@ -20,3 +20,4 @@ database_config:
|
|||
user: myuser
|
||||
password: mypassword
|
||||
database_name: mydatabase
|
||||
mongodb_connection_string: mongodb://learnhouse:learnhouse@mongo:27017/
|
||||
|
|
|
|||
|
|
@ -2,16 +2,19 @@ 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.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
|
||||
logging.info("LearnHouse has been shut down.")
|
||||
|
|
|
|||
|
|
@ -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