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
+45 -19

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"
+39 -18
View File
@@ -1,21 +1,42 @@
from fastapi import FastAPI import os
import mariadb from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import MySQLdb
from dotenv import load_dotenv
load_dotenv()
app = FastAPI() app = FastAPI()
@app.get("/api/portfolio") origins = [
def read_portfolio(): "http://localhost",
try: "http://localhost:5173",
conn = mariadb.connect( ]
user="root", app.add_middleware(
password="motdepasse", CORSMiddleware,
host="localhost", allow_origins=origins,
port=3306, allow_credentials=True,
database="portfolio" allow_methods=["*"],
allow_headers=["*"],
) )
cursor = conn.cursor()
cursor.execute("SELECT * FROM portfolio;")
result = cursor.fetchall() def get_db_connection():
conn.close() try:
return {"data": result} conn = MySQLdb.connect(
except mariadb.Error as e: user=os.getenv("DB_USER"),
return {"error": str(e)} 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.