Readability and functionality improvements

- Refactored all DBwork functions to not set and close connection inside
their body, they use connection as a parameter instead
- Added single file to configure a FastAPI app
- Implemented FastAPI's lifespan function that calls certain functions
on app startup and shutdown
- Added error logging for scraping functions
- Fixed /articles/get/html and /articles/get/md endpoints
- All POST methods now return base64 encoded html/md strings to avoid
weird json formatting
This commit is contained in:
2025-09-04 23:05:12 +03:00
parent 2b191dddd2
commit 6da6ace82f
5 changed files with 85 additions and 69 deletions

41
src/app_creator.py Normal file
View File

@ -0,0 +1,41 @@
import config
import router
import DBwork
from fastapi import FastAPI
from loguru import logger
from contextlib import asynccontextmanager
if config.enable_api_docs:
docs_url = '/api/docs'
else:
docs_url = None
schema_name = 'harticle'
table_name = 'articles'
@asynccontextmanager
async def lifespan(app: FastAPI):
DBwork.set_connection()
DBwork.schema_creator(schema_name, DBwork.connection)
DBwork.table_creator(schema_name, table_name, DBwork.connection)
yield
DBwork.close_connection(DBwork.connection)
app = FastAPI(docs_url=docs_url, lifespan=lifespan)
def create_app():
logging_level = config.logging_level
logger.add(
"sys.stdout",
format="{time:YYYY-MM-DD HH:mm:ss.SSS} | {level} | {file}:{line} - {message}",
colorize=True,
level=logging_level
)
app.include_router(router.router)
return app