Skip to content

Commit 7d070e2

Browse files
author
Uttam Singh
committed
Refactor main.py to remove circular import and use database.py
1 parent b7ae060 commit 7d070e2

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

backend/app/database.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from sqlalchemy import create_engine
2+
from sqlalchemy.orm import sessionmaker, declarative_base
3+
import os
4+
from dotenv import load_dotenv
5+
6+
load_dotenv()
7+
8+
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./app.db")
9+
10+
engine = create_engine(DATABASE_URL, pool_pre_ping=True)
11+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
12+
Base = declarative_base()
13+
14+
def get_db():
15+
db = SessionLocal()
16+
try:
17+
yield db
18+
finally:
19+
db.close()

backend/app/main.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
from fastapi import FastAPI, UploadFile, File, Depends, HTTPException, Form
22
from fastapi.middleware.cors import CORSMiddleware
3-
from sqlalchemy import create_engine, Column, Integer, String, Date, Text
4-
from sqlalchemy.orm import sessionmaker, declarative_base, Session
3+
from sqlalchemy.orm import Session
54
from pydantic import BaseModel
65
from datetime import date
76
import os, shutil
87
from dotenv import load_dotenv
98
import requests
109

11-
# ✅ Import users and models
12-
from app.models import user
10+
# ✅ Import from database (moved get_db here to avoid circular import)
11+
from app.database import Base, engine, get_db
12+
13+
# ✅ Import users router (no circular import now)
1314
from app.routers import users
1415

1516
# Optional OpenAI
@@ -21,13 +22,22 @@
2122
# Load environment variables
2223
load_dotenv()
2324

24-
# === Database Setup ===
25-
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./app.db")
26-
engine = create_engine(DATABASE_URL, pool_pre_ping=True)
27-
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
28-
Base = declarative_base()
25+
# === FastAPI App ===
26+
app = FastAPI(title="FAT-EIBL (Edme) – API")
27+
28+
# === CORS ===
29+
allow = os.getenv("ALLOW_ORIGINS", "*").split(",")
30+
app.add_middleware(
31+
CORSMiddleware,
32+
allow_origins=allow,
33+
allow_credentials=True,
34+
allow_methods=["*"],
35+
allow_headers=["*"],
36+
)
37+
38+
# === Models (local models only for tasks/audit) ===
39+
from sqlalchemy import Column, Integer, String, Date, Text
2940

30-
# === Models ===
3141
class Task(Base):
3242
__tablename__ = "tasks"
3343
id = Column(Integer, primary_key=True, index=True)
@@ -46,30 +56,9 @@ class AuditLog(Base):
4656
action = Column(String(50))
4757
detail = Column(Text)
4858

49-
# ✅ Create all tables (includes users table too)
59+
# ✅ Create all tables (includes Task, AuditLog, and user models)
5060
Base.metadata.create_all(bind=engine)
5161

52-
# === DB Session ===
53-
def get_db():
54-
db = SessionLocal()
55-
try:
56-
yield db
57-
finally:
58-
db.close()
59-
60-
# === FastAPI App ===
61-
app = FastAPI(title="FAT-EIBL (Edme) – API")
62-
63-
# === CORS ===
64-
allow = os.getenv("ALLOW_ORIGINS", "*").split(",")
65-
app.add_middleware(
66-
CORSMiddleware,
67-
allow_origins=allow,
68-
allow_credentials=True,
69-
allow_methods=["*"],
70-
allow_headers=["*"],
71-
)
72-
7362
# === Pydantic Schemas ===
7463
class TaskCreate(BaseModel):
7564
title: str
@@ -189,4 +178,3 @@ def ai_chat(prompt: str = Form(...)):
189178

190179
# --- USER ROUTES ---
191180
app.include_router(users.router, prefix="/users", tags=["Users"])
192-

0 commit comments

Comments
 (0)