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 mariadb
import os
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import MySQLdb
from dotenv import load_dotenv
load_dotenv()
app = FastAPI()
@app.get("/api/portfolio")
def read_portfolio():
try:
conn = mariadb.connect(
user="root",
password="motdepasse",
host="localhost",
port=3306,
database="portfolio"
origins = [
"http://localhost",
"http://localhost:5173",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
cursor = conn.cursor()
cursor.execute("SELECT * FROM portfolio;")
result = cursor.fetchall()
conn.close()
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
uvicorn
mariadb
mysqlclient
python-dotenv
View File
Whitespace-only changes.