From 16eccddb5908ad045840e4fdaa60d9237a8433f2 Mon Sep 17 00:00:00 2001 From: n0body Date: Sat, 6 Sep 2025 01:09:30 +0300 Subject: [PATCH] Hotfix + config optimization - Fixed a bug that didn't let any function receive connection objects - Added uvicorn logging level to config --- .gitignore | 3 ++- src/DBmodel.py | 7 +++++++ src/DBwork.py | 16 ++-------------- src/app_creator.py | 10 ++++------ src/config.py | 12 ++++++++---- src/main.py | 3 ++- src/router.py | 8 ++++---- 7 files changed, 29 insertions(+), 30 deletions(-) create mode 100644 src/DBmodel.py diff --git a/.gitignore b/.gitignore index 83197fc..0867c04 100644 --- a/.gitignore +++ b/.gitignore @@ -178,4 +178,5 @@ cython_debug/ ############CUSTOM########### /src/test.py responseTester.py -/sys.stdout \ No newline at end of file +/sys.stdout +/src/otherTest.py \ No newline at end of file diff --git a/src/DBmodel.py b/src/DBmodel.py new file mode 100644 index 0000000..cb060c7 --- /dev/null +++ b/src/DBmodel.py @@ -0,0 +1,7 @@ +import psycopg2 + + +class DataBase: + connection: psycopg2._psycopg.connection | None = None + +db = DataBase() diff --git a/src/DBwork.py b/src/DBwork.py index e03a786..2692688 100644 --- a/src/DBwork.py +++ b/src/DBwork.py @@ -1,15 +1,13 @@ import psycopg2 import config from loguru import logger +from DBmodel import db #connection stuff -connection = None - def set_connection(): - global connection try: - connection = psycopg2.connect( + db.connection = psycopg2.connect( dbname = config.db_name, user = config.postgres_user, password = config.postgres_password, @@ -52,16 +50,6 @@ def delete_entry(article_url, connection): logger.error(f'Failed to clear a rating entry for article \'{article_url}\': {e.pgerror}') -# def delete_rating(article_url, connection): -# try: -# cursor = connection.cursor() -# cursor.execute("UPDATE harticle.articles SET rating = NULL WHERE article_url = %s;", (article_url,)) -# connection.commit() -# logger.info(f'Rating for article \'{article_url}\' was cleared successfully') -# except psycopg2.Error as e: -# logger.error(f'Failed to clear a rating entry for article \'{article_url}\': {e.pgerror}') - - def get_all_entries(connection): try: cursor = connection.cursor() diff --git a/src/app_creator.py b/src/app_creator.py index fecdd91..8bc5889 100644 --- a/src/app_creator.py +++ b/src/app_creator.py @@ -1,6 +1,7 @@ import config import router import DBwork +from DBmodel import db from fastapi import FastAPI from loguru import logger from contextlib import asynccontextmanager @@ -11,17 +12,14 @@ if config.enable_api_docs: else: docs_url = None -schema_name = config.schema_name -table_name = config.table_name - @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) + DBwork.schema_creator(config.schema_name, db.connection) + DBwork.table_creator(config.schema_name, config.table_name, db.connection) yield - DBwork.close_connection(DBwork.connection) + DBwork.close_connection(db.connection) app = FastAPI(docs_url=docs_url, lifespan=lifespan) diff --git a/src/config.py b/src/config.py index 1d0bb96..2d2d10a 100644 --- a/src/config.py +++ b/src/config.py @@ -5,8 +5,12 @@ db_name = config('DB_NAME') postgres_user = config('POSTGRES_USER') postgres_password = config('POSTGRES_PASSWORD') host_name = config('HOST_NAME') -port = config('PORT') -logging_level = config('LOGGING_LEVEL') -enable_api_docs = config('ENABLE_API_DOCS', cast=bool) +port = config('PG_PORT') schema_name = config('SCHEMA_NAME') -table_name = config('TABLE_NAME') \ No newline at end of file +table_name = config('TABLE_NAME') + +enable_api_docs = config('ENABLE_API_DOCS', cast=bool) + +uvicorn_logging_level = config('UVI_LOGGING_LEVEL') + +logging_level = config('LOGGING_LEVEL') diff --git a/src/main.py b/src/main.py index d1ace66..9490351 100644 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1,8 @@ import uvicorn +from config import uvicorn_logging_level from app_creator import create_app if __name__ == '__main__': app = create_app() - uvicorn.run(app=app, host="127.0.0.1", port=8000, log_level="info") + uvicorn.run(app=app, host="127.0.0.1", port=8000, log_level=uvicorn_logging_level.lower()) diff --git a/src/router.py b/src/router.py index 9082217..63accee 100644 --- a/src/router.py +++ b/src/router.py @@ -1,6 +1,6 @@ import DBwork +from DBmodel import db import scraper -from DBwork import connection as conn from fastapi import Response, status, APIRouter from pydantic import BaseModel import psycopg2 @@ -31,7 +31,7 @@ async def ping(): @router.get('/rates') async def get_rates(): - result = dumps(DBwork.get_all_entries(conn)) + result = dumps(DBwork.get_all_entries(db.connection)) return result @@ -40,7 +40,7 @@ async def save_rating(entry: Entry, response: Response): try: DBwork.add_entry(article_url=entry.url, rating=entry.rating, - connection=conn + connection=db.connection ) message = 'success' except psycopg2.Error: @@ -56,7 +56,7 @@ async def save_rating(entry: Entry, response: Response): @router.post('/article/remove_rate') async def remove_rating(entry: Entry, response: Response): try: - DBwork.delete_entry(entry.url, conn) + DBwork.delete_entry(entry.url, db.connection) message = 'success' except psycopg2.Error: response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR