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:
41
src/app_creator.py
Normal file
41
src/app_creator.py
Normal 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
|
||||
Reference in New Issue
Block a user