update requierements.txt, add of db connection function and root

This commit is contained in:
Giovanni committed 2025-08-29 18:40:08 +02:00
1 parent 7a89aee31f
commit 8304940daf
4 files changed
+47 -21

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
DB_USER="portfolio_user"
DB_PASS="USER@gio53"
DB_HOST="localhost"
DB_NAME="portfolio"
+41 -20
View File
@@ -1,21 +1,42 @@
from fastapi import FastAPI import os
import mariadb from fastapi import FastAPI, HTTPException
app = FastAPI() from fastapi.middleware.cors import CORSMiddleware
import MySQLdb
from dotenv import load_dotenv
@app.get("/api/portfolio") load_dotenv()
def read_portfolio():
try: app = FastAPI()
conn = mariadb.connect(
user="root", origins = [
password="motdepasse", "http://localhost",
host="localhost", "http://localhost:5173",
port=3306, ]
database="portfolio" app.add_middleware(
) CORSMiddleware,
cursor = conn.cursor() allow_origins=origins,
cursor.execute("SELECT * FROM portfolio;") allow_credentials=True,
result = cursor.fetchall() allow_methods=["*"],
conn.close() allow_headers=["*"],
return {"data": result} )
except mariadb.Error as e:
return {"error": str(e)}
def get_db_connection():
try:
conn = MySQLdb.connect(
user=os.getenv("DB_USER"),
passwd=os.getenv("DB_PASS"),
host=os.getenv("DB_HOST"),
db=os.getenv("DB_NAME"),
charset='utf8mb4',
cursorclass=MySQLdb.cursors.DictCursor
)
return conn
except MySQLdb.Error as e:
print(f"Erreur de connexion à la base de données : {e}")
raise HTTPException(status_code=500, detail="Erreur interne du serveur lors de la connexion à la base de données.")
@app.get("/")
def read_root():
return {"status": "API Portfolio fonctionnelle"}
+2 -1
View File
@@ -1,3 +1,4 @@
fastapi fastapi
uvicorn uvicorn
mariadb mysqlclient
python-dotenv
View File
Whitespace-only changes.