84 lines
2.3 KiB
Python
84 lines
2.3 KiB
Python
from sqlalchemy import create_engine
|
||
from sqlalchemy.orm import sessionmaker, DeclarativeBase
|
||
from fastapi import FastAPI
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
from sqlalchemy import Table, Column, Integer, String, Float, Boolean, ForeignKey
|
||
from passlib.context import CryptContext
|
||
from dotenv import load_dotenv
|
||
import os
|
||
|
||
load_dotenv()
|
||
|
||
|
||
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)
|
||
# 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
|
||
|
||
class Base(DeclarativeBase):
|
||
pass #yeni sqlalchemy sürümünde bu sınıfı kullanı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) # Veritabanını oluşturur
|
||
|
||
# Session dependency (FastAPI için)
|
||
def get_session_db() -> 'Generator[Session, None]':
|
||
db = SessionLocal()
|
||
try:
|
||
yield db
|
||
finally:
|
||
db.close()
|
||
|
||
user_collection = Table( # user -> collection
|
||
"user_collection",
|
||
Base.metadata,
|
||
Column("user_id", Integer, ForeignKey("users_table.user_id"), primary_key=True),
|
||
Column("collection_id", Integer, ForeignKey("collections_table.collection_id"), primary_key=True),
|
||
)
|
||
|
||
|
||
collection_item = Table( # collection -> item
|
||
"collection_item",
|
||
Base.metadata,
|
||
Column("collection_id", ForeignKey("collections_table.collection_id"), primary_key=True),
|
||
Column("item_id", ForeignKey("items_table.item_id"), primary_key=True)
|
||
)
|
||
|
||
|
||
|
||
### SECRET KEY ###
|
||
origins = [
|
||
"http://localhost",
|
||
"http://localhost:8080",
|
||
"http://localhost:3000",
|
||
"http://localhost:8000",
|
||
]
|
||
|
||
app = FastAPI()
|
||
@app.on_event("startup")
|
||
def startup_event():
|
||
init_db()
|
||
|
||
app.add_middleware(
|
||
CORSMiddleware,
|
||
allow_origins=origins,
|
||
allow_credentials=True,
|
||
allow_methods=["*"],
|
||
allow_headers=["*"],
|
||
)
|
||
|
||
|
||
|