Ali2206 commited on
Commit
e07a90f
·
verified ·
1 Parent(s): 3d0dcf7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -7,7 +7,7 @@ import os
7
 
8
  app = FastAPI()
9
 
10
- # Allow all CORS origins (change this if needed for production)
11
  app.add_middleware(
12
  CORSMiddleware,
13
  allow_origins=["*"],
@@ -16,16 +16,18 @@ app.add_middleware(
16
  allow_headers=["*"],
17
  )
18
 
19
- # ✅ Ensure DB path is in a writable directory for Hugging Face Spaces
20
- HF_DATA_DIR = os.getenv("HF_HOME", "/data")
21
- os.makedirs(HF_DATA_DIR, exist_ok=True)
22
- db_path = os.path.join(HF_DATA_DIR, "users.db")
 
 
23
 
24
  # Connect to the database
25
- conn = sqlite3.connect(db_path, check_same_thread=False)
26
  cursor = conn.cursor()
27
 
28
- # Create users table if it doesn't exist
29
  cursor.execute('''
30
  CREATE TABLE IF NOT EXISTS users (
31
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -35,11 +37,11 @@ cursor.execute('''
35
  ''')
36
  conn.commit()
37
 
38
- # Utility function to hash passwords securely
39
  def hash_password(password: str) -> str:
40
  return hashlib.sha256(password.encode()).hexdigest()
41
 
42
- # Request models
43
  class SignupForm(BaseModel):
44
  email: str
45
  password: str
@@ -48,7 +50,7 @@ class LoginForm(BaseModel):
48
  email: str
49
  password: str
50
 
51
- # Route to sign up new users
52
  @app.post("/signup")
53
  def signup(data: SignupForm):
54
  email = data.email.lower().strip()
@@ -61,7 +63,7 @@ def signup(data: SignupForm):
61
  except sqlite3.IntegrityError:
62
  raise HTTPException(status_code=409, detail="Email already registered")
63
 
64
- # Route to log in existing users
65
  @app.post("/login")
66
  def login(data: LoginForm):
67
  email = data.email.lower().strip()
@@ -75,7 +77,7 @@ def login(data: LoginForm):
75
  else:
76
  raise HTTPException(status_code=401, detail="Invalid credentials")
77
 
78
- # Test root route
79
  @app.get("/")
80
  def root():
81
- return {"message": "FastAPI Auth API is running 🚀"}
 
7
 
8
  app = FastAPI()
9
 
10
+ # Allow cross-origin requests (adjust for production)
11
  app.add_middleware(
12
  CORSMiddleware,
13
  allow_origins=["*"],
 
16
  allow_headers=["*"],
17
  )
18
 
19
+ # ✅ Use a safe, writable location
20
+ DB_DIR = "/tmp"
21
+ DB_PATH = os.path.join(DB_DIR, "users.db")
22
+
23
+ # Ensure the directory exists
24
+ os.makedirs(DB_DIR, exist_ok=True)
25
 
26
  # Connect to the database
27
+ conn = sqlite3.connect(DB_PATH, check_same_thread=False)
28
  cursor = conn.cursor()
29
 
30
+ # Create users table
31
  cursor.execute('''
32
  CREATE TABLE IF NOT EXISTS users (
33
  id INTEGER PRIMARY KEY AUTOINCREMENT,
 
37
  ''')
38
  conn.commit()
39
 
40
+ # Hash password using SHA256
41
  def hash_password(password: str) -> str:
42
  return hashlib.sha256(password.encode()).hexdigest()
43
 
44
+ # Pydantic models
45
  class SignupForm(BaseModel):
46
  email: str
47
  password: str
 
50
  email: str
51
  password: str
52
 
53
+ # Signup route
54
  @app.post("/signup")
55
  def signup(data: SignupForm):
56
  email = data.email.lower().strip()
 
63
  except sqlite3.IntegrityError:
64
  raise HTTPException(status_code=409, detail="Email already registered")
65
 
66
+ # Login route
67
  @app.post("/login")
68
  def login(data: LoginForm):
69
  email = data.email.lower().strip()
 
77
  else:
78
  raise HTTPException(status_code=401, detail="Invalid credentials")
79
 
80
+ # Root route
81
  @app.get("/")
82
  def root():
83
+ return {"message": "FastAPI Auth API is running "}