Add health check API, logging setup, and environment variable support; update requirements and .gitignore

This commit is contained in:
Mabe
2026-08-28 18:44:29 +02:00
parent 36cb2dc567
commit 79e0af79dc
9 changed files with 261 additions and 175 deletions
+1
View File
@@ -1,2 +1,3 @@
__pycache__/ __pycache__/
*.log *.log
.env
View File
+26
View File
@@ -0,0 +1,26 @@
from fastapi import APIRouter
import logging
#Logging
logging.basicConfig(
filename='api.log', # Log to a file
level=logging.DEBUG, # Log everything (DEBUG and above)
format='%(asctime)s %(levelname)s %(message)s'
)
logging.info('Application started')
try:
logging.debug('Debug info here')
except Exception as e:
logging.exception('An error occurred')
router = APIRouter(prefix="/health", tags=["health"])
@router.get("/")
def health_check():
logging.info("get health check request")
return {"status": "ok"}
+5
View File
@@ -0,0 +1,5 @@
from fastapi import FastAPI
from api import health
app =FastAPI()
app.include_router(health.router)
+190
View File
@@ -0,0 +1,190 @@
import db
import logging
import time
import os
import sys
print("loading")
#Logging
logging.basicConfig(
filename='app.log', # Log to a file
level=logging.DEBUG, # Log everything (DEBUG and above)
format='%(asctime)s %(levelname)s %(message)s'
)
logging.info('Application started')
try:
logging.debug('Debug info here')
except Exception as e:
logging.exception('An error occurred')
def insert():
clothe = input("How is the clothe called? ").upper()
weigth = float(input("The weigth please? "))
pieces = int(input("How many pieces? ints only "))
db.new_part(clothe, weigth, pieces)
print("it is inserted")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def insert():
clothe = input("How is the clothe called? ").upper()
weigth = float(input("The weigth please? "))
pieces = int(input("How many pieces? ints only "))
db.new_part(clothe, weigth, pieces)
print("it is inserted")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def update():
clothe = input("How is the clothe called? ").upper()
weigth = input("The weigth please? ")
pieces = input("How many pieces? ints only ")
db.update_weigth(clothe, weigth)
db.update_pieces(clothe, pieces)
print("it is updated")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_weigth():
clothe = input("How is the clothe called? ").upper()
weigth = db.fetch_weigth(clothe)
print(weigth)
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_luggage_weigth():
weigth = db.fetch_luggage_weigth()
print(f"The luggage has a weigth of {weigth}g")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_pieces():
clothe = input("How is the clothe called? ").upper()
pieces = db.fetch_piece(clothe)
print(f"You have {pieces} of {clothe}")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_all_pieces():
pieces = db.fetch_all_pieces()
print(f"You have {pieces} in your luggage")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_clothes():
clothes = db.fetch_clothes()
for clothe in clothes:
print(clothe)
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_weigth_pieces():
clothe = input("How is the clothe called? ").upper()
weigth = db.fetch_weigth(clothe)
pieces = db.fetch_piece(clothe)
print(f"You have {pieces} of {clothe}. The weigth is {weigth}")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def delete():
clothe = input("How is the clothe called? ").upper()
really = input("Do you really want to delete it? Y/N").upper()
if really == "Y":
db.delete(clothe)
print(f"{clothe} was deleted")
time.sleep(3)
else:
time.sleep(0.5)
logging.info("no item deleted")
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def exit_programm():
time.sleep(0.5)
print(r"""
## ## ##########
### ### ## ##
#### #### ## ##
## #### ## ##########
## ## ## ## ##
## ## ## ##
""")
time.sleep(0.5)
print("")
print("")
print("Disconnect DB")
print("logout")
print("shutting down")
time.sleep(0.75)
os.system('cls' if os.name == 'nt' else 'clear')
sys.exit()
def menu():
print("Menu")
print("1: Insert Item")
print("2: Update Item")
print("3: Get weigth of clothe")
print("4: Get weigth of luggage")
print("5: Get pieces of clothe")
print("6: Get pieces of luggage")
print("7: Get clothes")
print("8: Get weigth and pieces of clothes")
print("9: Delete Item")
print("0: EXIT")
choice = input("Type number here ")
os.system('cls' if os.name == 'nt' else 'clear')
logging.info(f"{choice} is choosen")
time.sleep(0.5)
if choice == "1":
insert()
if choice == "2":
update()
if choice == "3":
get_weigth()
if choice == "4":
get_luggage_weigth()
if choice == "5":
get_pieces()
if choice == "6":
get_all_pieces()
if choice == "7":
get_clothes()
if choice == "8":
get_weigth_pieces()
if choice == "0":
exit_programm()
if choice == "9":
delete()
#The Terminal App Starts here
time.sleep(5)
print(r"""
## ## ##########
### ### ## ##
#### #### ## ##
## #### ## ##########
## ## ## ## ##
## ## ## ##
""")
logging.info("Menu is starting")
time.sleep(2.5)
menu()
+16 -1
View File
@@ -1,6 +1,7 @@
import mariadb import mariadb
import logging import logging
from dotenv import load_dotenv
import os
#Logging #Logging
@@ -104,3 +105,17 @@ def delete(cloth_type):
cursor.execute("DELETE FROM luggage WHERE clothes = ?;", (cloth_type,)) cursor.execute("DELETE FROM luggage WHERE clothes = ?;", (cloth_type,))
logging.critical(f"{cloth_type} was deleted") logging.critical(f"{cloth_type} was deleted")
connection.commit() connection.commit()
#using .env
load_dotenv()
db_name = os.getenv("DB_NAME")
db_psswd = os.getenv("DB_PASSWORD")
db_user = os.getenv("DB_USER")
db_port = os.getenv("DB_PORT") or "3306"
db_host = os.getenv("DB_HOST")
if db_host == None:
logging.error("no hostname provided")
os._exit(1)
connect(db_user, db_psswd, db_host, int(db_port), db_name)
+16 -168
View File
@@ -1,26 +1,13 @@
import db
import logging import logging
import time import subprocess
import os
import sys import sys
from dotenv import load_dotenv
import os import os
#using .env
load_dotenv()
db_name = os.getenv("DB_NAME")
db_psswd = os.getenv("DB_PASSWORD")
db_user = os.getenv("DB_USER")
db_port = os.getenv("DB_PORT") or "3306"
db_host = os.getenv("DB_HOST")
if db_host == None:
logging.error("no hostname provided")
os._exit(1)
open("api.log", "w").close()
print("loading")
open("app.log", "w").close() open("app.log", "w").close()
db.connect(db_user, db_psswd, db_host, int(db_port), db_name) open("api_server.log", "w").close()
#Logging #Logging
logging.basicConfig( logging.basicConfig(
filename='app.log', # Log to a file filename='app.log', # Log to a file
@@ -34,158 +21,19 @@ try:
except Exception as e: except Exception as e:
logging.exception('An error occurred') logging.exception('An error occurred')
def insert():
clothe = input("How is the clothe called? ").upper()
weigth = float(input("The weigth please? "))
pieces = int(input("How many pieces? ints only "))
db.new_part(clothe, weigth, pieces)
print("it is inserted")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def update(): env = os.environ.copy()
clothe = input("How is the clothe called? ").upper() env["PYTHONIOENCODING"] = "utf-8"
weigth = input("The weigth please? ")
pieces = input("How many pieces? ints only ")
db.update_weigth(clothe, weigth)
db.update_pieces(clothe, pieces)
print("it is updated")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_weigth(): log_file = open("api_server.log", "w", encoding="utf-8")
clothe = input("How is the clothe called? ").upper() api_process = subprocess.Popen(
weigth = db.fetch_weigth(clothe) ["fastapi", "dev", "api_server.py"],
print(weigth) stdout=log_file,
time.sleep(3) stderr=log_file,
os.system('cls' if os.name == 'nt' else 'clear') env=env
menu() )
cli_process = subprocess.Popen([sys.executable, "cli.py"])
def get_luggage_weigth(): api_process.wait()
weigth = db.fetch_luggage_weigth() cli_process.wait()
print(f"The luggage has a weigth of {weigth}g")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_pieces():
clothe = input("How is the clothe called? ").upper()
pieces = db.fetch_piece(clothe)
print(f"You have {pieces} of {clothe}")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_all_pieces():
pieces = db.fetch_all_pieces()
print(f"You have {pieces} in your luggage")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_clothes():
clothes = db.fetch_clothes()
for clothe in clothes:
print(clothe)
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def get_weigth_pieces():
clothe = input("How is the clothe called? ").upper()
weigth = db.fetch_weigth(clothe)
pieces = db.fetch_piece(clothe)
print(f"You have {pieces} of {clothe}. The weigth is {weigth}")
time.sleep(3)
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def delete():
clothe = input("How is the clothe called? ").upper()
really = input("Do you really want to delete it? Y/N").upper()
if really == "Y":
db.delete(clothe)
print(f"{clothe} was deleted")
time.sleep(3)
else:
time.sleep(0.5)
logging.info("no item deleted")
os.system('cls' if os.name == 'nt' else 'clear')
menu()
def exit_programm():
time.sleep(0.5)
print(r"""
## ## ##########
### ### ## ##
#### #### ## ##
## #### ## ##########
## ## ## ## ##
## ## ## ##
""")
time.sleep(0.5)
print("")
print("")
print("Disconnect DB")
print("logout")
print("shutting down")
time.sleep(0.75)
os.system('cls' if os.name == 'nt' else 'clear')
sys.exit()
def menu():
print("Menu")
print("1: Insert Item")
print("2: Update Item")
print("3: Get weigth of clothe")
print("4: Get weigth of luggage")
print("5: Get pieces of clothe")
print("6: Get pieces of luggage")
print("7: Get clothes")
print("8: Get weigth and pieces of clothes")
print("9: Delete Item")
print("0: EXIT")
choice = input("Type number here ")
os.system('cls' if os.name == 'nt' else 'clear')
logging.info(f"{choice} is choosen")
time.sleep(0.5)
if choice == "1":
insert()
if choice == "2":
update()
if choice == "3":
get_weigth()
if choice == "4":
get_luggage_weigth()
if choice == "5":
get_pieces()
if choice == "6":
get_all_pieces()
if choice == "7":
get_clothes()
if choice == "8":
get_weigth_pieces()
if choice == "0":
exit_programm()
if choice == "9":
delete()
#The Terminal App Starts here
time.sleep(5)
print(r"""
## ## ##########
### ### ## ##
#### #### ## ##
## #### ## ##########
## ## ## ## ##
## ## ## ##
""")
logging.info("Menu is starting")
time.sleep(2.5)
menu()
+1
View File
@@ -1,2 +1,3 @@
mariadb == 1.1.14 mariadb == 1.1.14
python-dotenv == 1.2.3 python-dotenv == 1.2.3
fastapi[standard] == 0.141.1
+4 -4
View File
@@ -1,5 +1,5 @@
home = /usr/bin home = C:\Users\bokaj\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0
include-system-site-packages = false include-system-site-packages = false
version = 3.12.3 version = 3.13.14
executable = /usr/bin/python3.12 executable = C:\Users\bokaj\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\python.exe
command = /usr/bin/python3 -m venv /home/jakob/Documents/py_luggage/venv command = C:\Users\bokaj\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\python.exe -m venv J:\_Python\luggage\venv