mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: refactor the entire learnhouse project
This commit is contained in:
parent
f556e41dda
commit
4c215e91d5
247 changed files with 7716 additions and 1013 deletions
0
apps/api/src/core/__init__.py
Normal file
0
apps/api/src/core/__init__.py
Normal file
0
apps/api/src/core/events/__init__.py
Normal file
0
apps/api/src/core/events/__init__.py
Normal file
8
apps/api/src/core/events/content.py
Normal file
8
apps/api/src/core/events/content.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import os
|
||||
|
||||
|
||||
async def check_content_directory():
|
||||
if not os.path.exists("content"):
|
||||
# create folder for activity
|
||||
print("Creating content directory...")
|
||||
os.makedirs("content")
|
||||
21
apps/api/src/core/events/database.py
Normal file
21
apps/api/src/core/events/database.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import logging
|
||||
from fastapi import FastAPI
|
||||
import motor.motor_asyncio
|
||||
|
||||
|
||||
async def connect_to_db(app: FastAPI):
|
||||
logging.info("Connecting to database...")
|
||||
try:
|
||||
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.")
|
||||
return app
|
||||
35
apps/api/src/core/events/events.py
Normal file
35
apps/api/src/core/events/events.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
from typing import Callable
|
||||
from fastapi import FastAPI
|
||||
from config.config import LearnHouseConfig, get_learnhouse_config
|
||||
from src.core.events.content import check_content_directory
|
||||
from src.core.events.database import close_database, connect_to_db
|
||||
from src.core.events.logs import create_logs_dir
|
||||
from src.core.events.sentry import init_sentry
|
||||
|
||||
|
||||
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
|
||||
|
||||
# Init Sentry
|
||||
await init_sentry(app)
|
||||
|
||||
# Connect to database
|
||||
await connect_to_db(app)
|
||||
|
||||
# Create logs directory
|
||||
await create_logs_dir()
|
||||
|
||||
# Create content directory
|
||||
await check_content_directory()
|
||||
|
||||
return start_app
|
||||
|
||||
|
||||
def shutdown_app(app: FastAPI) -> Callable:
|
||||
async def close_app() -> None:
|
||||
await close_database(app)
|
||||
|
||||
return close_app
|
||||
24
apps/api/src/core/events/logs.py
Normal file
24
apps/api/src/core/events/logs.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import logging
|
||||
import os
|
||||
|
||||
|
||||
async def create_logs_dir():
|
||||
if not os.path.exists("logs"):
|
||||
os.mkdir("logs")
|
||||
|
||||
# Initiate logging
|
||||
async def init_logging():
|
||||
await create_logs_dir()
|
||||
|
||||
# Logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||
datefmt="%d-%b-%y %H:%M:%S",
|
||||
handlers=[
|
||||
logging.FileHandler("logs/learnhouse.log"),
|
||||
logging.StreamHandler()
|
||||
]
|
||||
)
|
||||
|
||||
logging.info("Logging initiated")
|
||||
16
apps/api/src/core/events/sentry.py
Normal file
16
apps/api/src/core/events/sentry.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
from fastapi import FastAPI
|
||||
|
||||
import sentry_sdk
|
||||
|
||||
from config.config import LearnHouseConfig
|
||||
|
||||
async def init_sentry(app: FastAPI) -> None:
|
||||
|
||||
leanrhouse_config : LearnHouseConfig = app.learnhouse_config # type: ignore
|
||||
if leanrhouse_config.hosting_config.sentry_config is not None:
|
||||
sentry_sdk.init(
|
||||
dsn=app.learnhouse_config.hosting_config.sentry_config.dsn, # type: ignore
|
||||
environment=app.learnhouse_config.hosting_config.sentry_config.environment, # type: ignore
|
||||
release=app.learnhouse_config.hosting_config.sentry_config.release, # type: ignore
|
||||
traces_sample_rate=1.0,
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue