Hotfix + config optimization

- Fixed a bug that didn't let any function receive connection objects
- Added uvicorn logging level to config
This commit is contained in:
2025-09-06 01:09:30 +03:00
parent c2dd26c5d3
commit 16eccddb59
7 changed files with 29 additions and 30 deletions

1
.gitignore vendored
View File

@ -179,3 +179,4 @@ cython_debug/
/src/test.py /src/test.py
responseTester.py responseTester.py
/sys.stdout /sys.stdout
/src/otherTest.py

7
src/DBmodel.py Normal file
View File

@ -0,0 +1,7 @@
import psycopg2
class DataBase:
connection: psycopg2._psycopg.connection | None = None
db = DataBase()

View File

@ -1,15 +1,13 @@
import psycopg2 import psycopg2
import config import config
from loguru import logger from loguru import logger
from DBmodel import db
#connection stuff #connection stuff
connection = None
def set_connection(): def set_connection():
global connection
try: try:
connection = psycopg2.connect( db.connection = psycopg2.connect(
dbname = config.db_name, dbname = config.db_name,
user = config.postgres_user, user = config.postgres_user,
password = config.postgres_password, 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}') 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): def get_all_entries(connection):
try: try:
cursor = connection.cursor() cursor = connection.cursor()

View File

@ -1,6 +1,7 @@
import config import config
import router import router
import DBwork import DBwork
from DBmodel import db
from fastapi import FastAPI from fastapi import FastAPI
from loguru import logger from loguru import logger
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
@ -11,17 +12,14 @@ if config.enable_api_docs:
else: else:
docs_url = None docs_url = None
schema_name = config.schema_name
table_name = config.table_name
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
DBwork.set_connection() DBwork.set_connection()
DBwork.schema_creator(schema_name, DBwork.connection) DBwork.schema_creator(config.schema_name, db.connection)
DBwork.table_creator(schema_name, table_name, DBwork.connection) DBwork.table_creator(config.schema_name, config.table_name, db.connection)
yield yield
DBwork.close_connection(DBwork.connection) DBwork.close_connection(db.connection)
app = FastAPI(docs_url=docs_url, lifespan=lifespan) app = FastAPI(docs_url=docs_url, lifespan=lifespan)

View File

@ -5,8 +5,12 @@ db_name = config('DB_NAME')
postgres_user = config('POSTGRES_USER') postgres_user = config('POSTGRES_USER')
postgres_password = config('POSTGRES_PASSWORD') postgres_password = config('POSTGRES_PASSWORD')
host_name = config('HOST_NAME') host_name = config('HOST_NAME')
port = config('PORT') port = config('PG_PORT')
logging_level = config('LOGGING_LEVEL')
enable_api_docs = config('ENABLE_API_DOCS', cast=bool)
schema_name = config('SCHEMA_NAME') schema_name = config('SCHEMA_NAME')
table_name = config('TABLE_NAME') 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')

View File

@ -1,7 +1,8 @@
import uvicorn import uvicorn
from config import uvicorn_logging_level
from app_creator import create_app from app_creator import create_app
if __name__ == '__main__': if __name__ == '__main__':
app = create_app() 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())

View File

@ -1,6 +1,6 @@
import DBwork import DBwork
from DBmodel import db
import scraper import scraper
from DBwork import connection as conn
from fastapi import Response, status, APIRouter from fastapi import Response, status, APIRouter
from pydantic import BaseModel from pydantic import BaseModel
import psycopg2 import psycopg2
@ -31,7 +31,7 @@ async def ping():
@router.get('/rates') @router.get('/rates')
async def get_rates(): async def get_rates():
result = dumps(DBwork.get_all_entries(conn)) result = dumps(DBwork.get_all_entries(db.connection))
return result return result
@ -40,7 +40,7 @@ async def save_rating(entry: Entry, response: Response):
try: try:
DBwork.add_entry(article_url=entry.url, DBwork.add_entry(article_url=entry.url,
rating=entry.rating, rating=entry.rating,
connection=conn connection=db.connection
) )
message = 'success' message = 'success'
except psycopg2.Error: except psycopg2.Error:
@ -56,7 +56,7 @@ async def save_rating(entry: Entry, response: Response):
@router.post('/article/remove_rate') @router.post('/article/remove_rate')
async def remove_rating(entry: Entry, response: Response): async def remove_rating(entry: Entry, response: Response):
try: try:
DBwork.delete_entry(entry.url, conn) DBwork.delete_entry(entry.url, db.connection)
message = 'success' message = 'success'
except psycopg2.Error: except psycopg2.Error:
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR