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:
1
.gitignore
vendored
1
.gitignore
vendored
@ -179,3 +179,4 @@ cython_debug/
|
||||
/src/test.py
|
||||
responseTester.py
|
||||
/sys.stdout
|
||||
/src/otherTest.py
|
||||
7
src/DBmodel.py
Normal file
7
src/DBmodel.py
Normal file
@ -0,0 +1,7 @@
|
||||
import psycopg2
|
||||
|
||||
|
||||
class DataBase:
|
||||
connection: psycopg2._psycopg.connection | None = None
|
||||
|
||||
db = DataBase()
|
||||
@ -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()
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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')
|
||||
|
||||
enable_api_docs = config('ENABLE_API_DOCS', cast=bool)
|
||||
|
||||
uvicorn_logging_level = config('UVI_LOGGING_LEVEL')
|
||||
|
||||
logging_level = config('LOGGING_LEVEL')
|
||||
|
||||
@ -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())
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user