Code Formmating, set DB_REBUILD env variable
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Argiris Deligiannidis 2024-04-07 13:43:07 +03:00
parent 933a650bdb
commit 520f104c61
3 changed files with 9 additions and 12 deletions

View File

@ -12,19 +12,16 @@ DATABASE = os.getenv('DATABASE')
DB_HOST = os.getenv('DB_HOST') DB_HOST = os.getenv('DB_HOST')
DB_USER = os.getenv('DB_USER') DB_USER = os.getenv('DB_USER')
DB_PASS = os.getenv('DB_PASS') DB_PASS = os.getenv('DB_PASS')
DB_REBUILD = os.getenv('DB_REBUILD')
DB_URI="postgresql://{}:{}@{}/{}".format(DB_USER, DB_PASS, DB_HOST, DATABASE) #, echo=True) DB_URI="postgresql://{}:{}@{}/{}".format(DB_USER, DB_PASS, DB_HOST, DATABASE) #, echo=True)
engine=create_engine(DB_URI) engine=create_engine(DB_URI)
database=declarative_base() database=declarative_base()
db_session=scoped_session(sessionmaker(bind=engine)) db_session=scoped_session(sessionmaker(bind=engine))
if not database_exists(engine.url): if not database_exists(engine.url):
create_database(engine.url) create_database(engine.url)
try: try:
connection = engine.connect() connection = engine.connect()
print("\n\t\tConnected successfully to database: {}@{}\n".format(DATABASE, DB_HOST)) print("\n\t\tConnected successfully to database: {}@{}\n".format(DATABASE, DB_HOST))

View File

@ -1,4 +1,5 @@
DATABASE='weatherapp' DATABASE='weatherapp'
DB_HOST='10.1.1.1' DB_HOST='10.1.1.1'
DB_USER='weatherapp' DB_USER='weatherapp'
DB_PASS='YEj5zxBsM4pZlDG6NrX2' DB_PASS='YEj5zxBsM4pZlDG6NrX2'
DB_REBUILD='True'

13
main.py
View File

@ -1,13 +1,10 @@
from fastapi import FastAPI, status, HTTPException from fastapi import FastAPI, status, HTTPException
from models import Location from models import Location
from db_connector import database,engine from db_connector import database,engine,DB_REBUILD
import utils import utils
if DB_REBUILD == 'True':
db_rebuild = True database.metadata.drop_all(engine)
database.metadata.drop_all(engine)
if db_rebuild:
utils.initialize_database() utils.initialize_database()
app = FastAPI() app = FastAPI()
@ -31,6 +28,7 @@ def error_400_handler(return_data):
async def index_response(): async def index_response():
return {"Status": "OK"} return {"Status": "OK"}
@app.get("/locations") @app.get("/locations")
async def get_location_weather(places: str): async def get_location_weather(places: str):
""" """
@ -63,7 +61,6 @@ async def get_weather_by_id(id: int):
Returns: Returns:
- The weather data for the specified location - The weather data for the specified location
""" """
result = utils.retrieve_weather_data([id]) result = utils.retrieve_weather_data([id])
error_400_handler(result) error_400_handler(result)
@ -84,6 +81,7 @@ async def add_location(
- longitude (float): The longitude coordinate of the location. - longitude (float): The longitude coordinate of the location.
- latitude (float): The latitude coordinate of the location. - latitude (float): The latitude coordinate of the location.
""" """
if len(name.encode('utf-8')) > 200: if len(name.encode('utf-8')) > 200:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
@ -92,6 +90,7 @@ async def add_location(
else: else:
utils.add_location(Location(name=name,longitude=longitude,latitude=latitude)) utils.add_location(Location(name=name,longitude=longitude,latitude=latitude))
@app.delete("/locations/{id}") @app.delete("/locations/{id}")
async def delete_location(id: int): async def delete_location(id: int):
""" """