update requierements.txt, add of db connection function and root
This commit is contained in:
1 parent
7a89aee31f
commit
8304940daf
4 files changed
+44
-18
No files matched your search
@@ -0,0 +1,4 @@
|
||||
DB_USER="portfolio_user"
|
||||
DB_PASS="USER@gio53"
|
||||
DB_HOST="localhost"
|
||||
DB_NAME="portfolio"
|
||||
@@ -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():
|
||||
origins = [
|
||||
"http://localhost",
|
||||
"http://localhost:5173",
|
||||
]
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
def get_db_connection():
|
||||
try:
|
||||
conn = mariadb.connect(
|
||||
user="root",
|
||||
password="motdepasse",
|
||||
host="localhost",
|
||||
port=3306,
|
||||
database="portfolio"
|
||||
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
|
||||
)
|
||||
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)}
|
||||
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
@@ -1,3 +1,4 @@
|
||||
fastapi
|
||||
uvicorn
|
||||
mariadb
|
||||
mysqlclient
|
||||
python-dotenv
|
||||
Reference in new issue
Block a user