base model
This commit is contained in:
parent
83389e0c10
commit
36da53a562
4 changed files with 105 additions and 69 deletions
37
config.py
37
config.py
|
|
@ -4,29 +4,38 @@ from sqlalchemy.orm import sessionmaker
|
|||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from passlib.context import CryptContext
|
||||
from sqlmodel import SQLModel, Field, Session
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
# Veritabanı URL'sini oluştur
|
||||
DATABASE_URL = (
|
||||
f"postgresql://{os.getenv('USERNAME_DB')}:"
|
||||
f"{os.getenv('PASSWORD_DB')}@"
|
||||
f"{os.getenv('HOST_DB')}:"
|
||||
f"{os.getenv('PORT_DB')}/"
|
||||
f"{os.getenv('NAME_DB')}"
|
||||
)
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
SECRET_KEY = os.getenv("SECRET_KEY")
|
||||
ALGORITHM = os.getenv("ALGORITHM")
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", 30))
|
||||
|
||||
DATABASE_URL = os.getenv("DATABASE_URL")
|
||||
# Engine oluştur
|
||||
engine = create_engine(DATABASE_URL, echo=False)
|
||||
def init_db():
|
||||
SQLModel.metadata.create_all(engine)
|
||||
# Session factory oluştur
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
Base = declarative_base() #sqlalchemy için bu sınıfı kullanıyoruz 'class DBUser(Base)' şeklinde tanımlıyoruz
|
||||
#models te içe aktarmayı unutma
|
||||
|
||||
def init_db():
|
||||
Base.metadata.drop_all(engine) # Veritabanını her başlangıcta siler burayada dikkat !!!!!!!!
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
# Session dependency (FastAPI için)
|
||||
def get_session_db():
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
|
||||
### SECRET KEY ###
|
||||
|
|
@ -39,7 +48,7 @@ origins = [
|
|||
|
||||
app = FastAPI()
|
||||
@app.on_event("startup")
|
||||
def on_startup():
|
||||
def startup_event():
|
||||
init_db()
|
||||
|
||||
app.add_middleware(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue