rsnarsna commited on
Commit ·
b4b88b8
1
Parent(s): 1e55931
db conection fixed
Browse files- backend/main.py +20 -5
backend/main.py
CHANGED
|
@@ -18,9 +18,16 @@ from sqlalchemy.orm import sessionmaker, declarative_base, relationship, Session
|
|
| 18 |
# ==========================================
|
| 19 |
# Database Configuration
|
| 20 |
# ==========================================
|
| 21 |
-
SQLALCHEMY_DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres:gVgyOv6DL9vkzGmh@
|
| 22 |
-
|
| 23 |
-
engine = create_engine(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 25 |
|
| 26 |
Base = declarative_base()
|
|
@@ -93,11 +100,19 @@ class TokenData(BaseModel):
|
|
| 93 |
# ==========================================
|
| 94 |
# FastAPI App setup
|
| 95 |
# ==========================================
|
| 96 |
-
# Create DB tables
|
| 97 |
-
Base.metadata.create_all(bind=engine)
|
| 98 |
|
| 99 |
app = FastAPI(title="Task 1 API")
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# --- Static files & Templates ---
|
| 102 |
BASE_DIR = Path(__file__).resolve().parent
|
| 103 |
app.mount("/static", StaticFiles(directory=str(BASE_DIR / "static")), name="static")
|
|
|
|
| 18 |
# ==========================================
|
| 19 |
# Database Configuration
|
| 20 |
# ==========================================
|
| 21 |
+
SQLALCHEMY_DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres.nujbaaxesubgawnazmza:gVgyOv6DL9vkzGmh@aws-1-ap-south-1.pooler.supabase.com:6543/postgres")
|
| 22 |
+
|
| 23 |
+
engine = create_engine(
|
| 24 |
+
SQLALCHEMY_DATABASE_URL,
|
| 25 |
+
pool_pre_ping=True,
|
| 26 |
+
pool_size=5,
|
| 27 |
+
max_overflow=10,
|
| 28 |
+
pool_recycle=300,
|
| 29 |
+
connect_args={"connect_timeout": 10},
|
| 30 |
+
)
|
| 31 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 32 |
|
| 33 |
Base = declarative_base()
|
|
|
|
| 100 |
# ==========================================
|
| 101 |
# FastAPI App setup
|
| 102 |
# ==========================================
|
|
|
|
|
|
|
| 103 |
|
| 104 |
app = FastAPI(title="Task 1 API")
|
| 105 |
|
| 106 |
+
@app.on_event("startup")
|
| 107 |
+
def on_startup():
|
| 108 |
+
"""Create DB tables on startup — retry-safe."""
|
| 109 |
+
try:
|
| 110 |
+
Base.metadata.create_all(bind=engine)
|
| 111 |
+
print("✅ Database tables created / verified.")
|
| 112 |
+
except Exception as e:
|
| 113 |
+
print(f"⚠️ Could not connect to database on startup: {e}")
|
| 114 |
+
print(" The app will start anyway; DB operations will fail until the DB is reachable.")
|
| 115 |
+
|
| 116 |
# --- Static files & Templates ---
|
| 117 |
BASE_DIR = Path(__file__).resolve().parent
|
| 118 |
app.mount("/static", StaticFiles(directory=str(BASE_DIR / "static")), name="static")
|