Ali2206 commited on
Commit
a44cf7a
·
verified ·
1 Parent(s): 48660ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -8
app.py CHANGED
@@ -8,29 +8,31 @@ from motor.motor_asyncio import AsyncIOMotorClient
8
 
9
  app = FastAPI()
10
 
11
- # === CORS Middleware ===
12
  app.add_middleware(
13
  CORSMiddleware,
14
- allow_origins=["*"], # ⚠️ Restrict in production
15
  allow_credentials=True,
16
  allow_methods=["*"],
17
  allow_headers=["*"],
18
  )
19
 
20
- # === MongoDB Setup ===
21
- MONGO_URI = os.getenv("MONGO_URI")
 
22
  if not MONGO_URI:
23
- raise RuntimeError("Missing MONGO_URI environment variable")
24
 
 
25
  client = AsyncIOMotorClient(MONGO_URI, tls=True, tlsCAFile=certifi.where())
26
  db = client["cps_db"]
27
  users_collection = db["users"]
28
 
29
- # === Password Hashing ===
30
  def hash_password(password: str) -> str:
31
  return hashlib.sha256(password.encode()).hexdigest()
32
 
33
- # === Pydantic Models ===
34
  class SignupForm(BaseModel):
35
  email: str
36
  password: str
@@ -65,4 +67,4 @@ async def login(data: LoginForm):
65
 
66
  @app.get("/")
67
  def root():
68
- return {"message": "🚀 MongoDB FastAPI backend is live"}
 
8
 
9
  app = FastAPI()
10
 
11
+ # === CORS setup ===
12
  app.add_middleware(
13
  CORSMiddleware,
14
+ allow_origins=["*"], # Open CORS (adjust for production)
15
  allow_credentials=True,
16
  allow_methods=["*"],
17
  allow_headers=["*"],
18
  )
19
 
20
+ # === MongoDB Atlas Setup ===
21
+ MONGO_URI = os.getenv("MONGO_URI") # Make sure this is set in HF Secrets
22
+
23
  if not MONGO_URI:
24
+ raise RuntimeError(" MONGO_URI not set in environment variables!")
25
 
26
+ # Use certifi for secure SSL connection
27
  client = AsyncIOMotorClient(MONGO_URI, tls=True, tlsCAFile=certifi.where())
28
  db = client["cps_db"]
29
  users_collection = db["users"]
30
 
31
+ # === Utils ===
32
  def hash_password(password: str) -> str:
33
  return hashlib.sha256(password.encode()).hexdigest()
34
 
35
+ # === Models ===
36
  class SignupForm(BaseModel):
37
  email: str
38
  password: str
 
67
 
68
  @app.get("/")
69
  def root():
70
+ return {"message": " MongoDB FastAPI backend is up"}