Spaces:
Sleeping
Sleeping
Pavanupadhyay27 commited on
Commit ·
ec57cbc
1
Parent(s): cf9da27
fix(backend): add root route for Hugging Face health check and auto-normalize DATABASE_URL username/password
Browse files- backend/app/core/config.py +29 -0
- backend/app/main.py +10 -1
backend/app/core/config.py
CHANGED
|
@@ -10,6 +10,35 @@ class Settings(BaseSettings):
|
|
| 10 |
# Database Configuration
|
| 11 |
DATABASE_URL: str
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# JWT & Security
|
| 14 |
JWT_SECRET_KEY: str
|
| 15 |
JWT_ALGORITHM: str = "HS256"
|
|
|
|
| 10 |
# Database Configuration
|
| 11 |
DATABASE_URL: str
|
| 12 |
|
| 13 |
+
@validator("DATABASE_URL", pre=True)
|
| 14 |
+
def normalize_database_url(cls, v):
|
| 15 |
+
import urllib.parse
|
| 16 |
+
if not isinstance(v, str):
|
| 17 |
+
return v
|
| 18 |
+
|
| 19 |
+
if v.startswith("postgresql://") or v.startswith("postgres://"):
|
| 20 |
+
prefix = "postgresql://" if v.startswith("postgresql://") else "postgres://"
|
| 21 |
+
rest = v[len(prefix):]
|
| 22 |
+
if "@" in rest:
|
| 23 |
+
creds, host_info = rest.rsplit("@", 1)
|
| 24 |
+
if ":" in creds:
|
| 25 |
+
user, password = creds.split(":", 1)
|
| 26 |
+
else:
|
| 27 |
+
user = creds
|
| 28 |
+
password = ""
|
| 29 |
+
|
| 30 |
+
# Auto-append project ref for Supabase pooler if missing
|
| 31 |
+
if "pooler.supabase.com" in host_info:
|
| 32 |
+
if user == "postgres":
|
| 33 |
+
user = "postgres.erzowqgbpeobbzpjkmtt"
|
| 34 |
+
|
| 35 |
+
# Auto-encode password safely
|
| 36 |
+
decoded_password = urllib.parse.unquote(password)
|
| 37 |
+
encoded_password = urllib.parse.quote_plus(decoded_password)
|
| 38 |
+
|
| 39 |
+
v = f"postgresql://{user}:{encoded_password}@{host_info}"
|
| 40 |
+
return v
|
| 41 |
+
|
| 42 |
# JWT & Security
|
| 43 |
JWT_SECRET_KEY: str
|
| 44 |
JWT_ALGORITHM: str = "HS256"
|
backend/app/main.py
CHANGED
|
@@ -56,7 +56,15 @@ def startup_event():
|
|
| 56 |
finally:
|
| 57 |
db.close()
|
| 58 |
|
| 59 |
-
# Health check
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
@app.get("/health", tags=["Status"])
|
| 61 |
def health_check():
|
| 62 |
return {
|
|
@@ -65,6 +73,7 @@ def health_check():
|
|
| 65 |
"version": "1.0.0"
|
| 66 |
}
|
| 67 |
|
|
|
|
| 68 |
# Include API Routers
|
| 69 |
app.include_router(auth.router, prefix=f"{settings.API_V1_STR}/auth", tags=["Authentication"])
|
| 70 |
app.include_router(employees.router, prefix=f"{settings.API_V1_STR}/employees", tags=["Employee Management"])
|
|
|
|
| 56 |
finally:
|
| 57 |
db.close()
|
| 58 |
|
| 59 |
+
# Health check and root route
|
| 60 |
+
@app.get("/")
|
| 61 |
+
def read_root():
|
| 62 |
+
return {
|
| 63 |
+
"status": "healthy",
|
| 64 |
+
"message": "NetraID Backend API is running",
|
| 65 |
+
"docs": "/docs"
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
@app.get("/health", tags=["Status"])
|
| 69 |
def health_check():
|
| 70 |
return {
|
|
|
|
| 73 |
"version": "1.0.0"
|
| 74 |
}
|
| 75 |
|
| 76 |
+
|
| 77 |
# Include API Routers
|
| 78 |
app.include_router(auth.router, prefix=f"{settings.API_V1_STR}/auth", tags=["Authentication"])
|
| 79 |
app.include_router(employees.router, prefix=f"{settings.API_V1_STR}/employees", tags=["Employee Management"])
|