Delete app.py
Browse files
app.py
DELETED
|
@@ -1,134 +0,0 @@
|
|
| 1 |
-
from fastapi import FastAPI, Query
|
| 2 |
-
import duckdb
|
| 3 |
-
from contextlib import asynccontextmanager
|
| 4 |
-
import re
|
| 5 |
-
|
| 6 |
-
app = FastAPI()
|
| 7 |
-
con = None
|
| 8 |
-
|
| 9 |
-
@asynccontextmanager
|
| 10 |
-
async def lifespan(app: FastAPI):
|
| 11 |
-
global con
|
| 12 |
-
print("🟢 Starting...")
|
| 13 |
-
con = duckdb.connect()
|
| 14 |
-
con.execute("INSTALL httpfs; LOAD httpfs;")
|
| 15 |
-
|
| 16 |
-
# Email Database
|
| 17 |
-
PARQUET_URL = "https://huggingface.co/datasets/tfqdeadlo/Inddatainonefile/resolve/main/users_data.parquet"
|
| 18 |
-
|
| 19 |
-
try:
|
| 20 |
-
print("🟡 Loading parquet...")
|
| 21 |
-
con.execute(f"""
|
| 22 |
-
CREATE OR REPLACE VIEW users_view AS
|
| 23 |
-
SELECT * FROM read_parquet('{PARQUET_URL}')
|
| 24 |
-
""")
|
| 25 |
-
|
| 26 |
-
count = con.execute("SELECT COUNT(*) FROM users_view").fetchone()[0]
|
| 27 |
-
print(f"✅ Loaded! {count:,} records")
|
| 28 |
-
|
| 29 |
-
except Exception as e:
|
| 30 |
-
print(f"❌ Error: {e}")
|
| 31 |
-
|
| 32 |
-
# Sample data
|
| 33 |
-
con.execute("""
|
| 34 |
-
CREATE OR REPLACE VIEW users_view AS
|
| 35 |
-
SELECT * FROM (
|
| 36 |
-
VALUES
|
| 37 |
-
('9999999891','Krishan Bidhuri','Dharamveer','Delhi','9999599891','AIRTEL DELHI','417479131232','krishan@email.com'),
|
| 38 |
-
('9999999751','Manoj Sharawat','Rajender','Delhi',NULL,'AIRTEL DELHI','423900570509','manoj@email.com')
|
| 39 |
-
) AS t(mobile,name,fname,address,alt,circle,id,email)
|
| 40 |
-
""")
|
| 41 |
-
|
| 42 |
-
print("✅ Sample data loaded")
|
| 43 |
-
|
| 44 |
-
yield
|
| 45 |
-
con.close()
|
| 46 |
-
|
| 47 |
-
app.router.lifespan_context = lifespan
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
@app.get("/")
|
| 51 |
-
def home():
|
| 52 |
-
return {
|
| 53 |
-
"status": "online",
|
| 54 |
-
"message": "Email Search API",
|
| 55 |
-
"endpoint": "/search?email=test@gmail.com"
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
@app.get("/search")
|
| 60 |
-
async def search(email: str = Query(..., description="Email Address")):
|
| 61 |
-
|
| 62 |
-
email = email.strip().lower()
|
| 63 |
-
|
| 64 |
-
if not re.match(r"^[^@]+@[^@]+\.[^@]+$", email):
|
| 65 |
-
return {
|
| 66 |
-
"status": "error",
|
| 67 |
-
"message": "Invalid email address"
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
try:
|
| 71 |
-
query = """
|
| 72 |
-
SELECT *
|
| 73 |
-
FROM users_view
|
| 74 |
-
WHERE lower(email)=?
|
| 75 |
-
LIMIT 1
|
| 76 |
-
"""
|
| 77 |
-
|
| 78 |
-
result = con.execute(query, [email]).fetchone()
|
| 79 |
-
|
| 80 |
-
if not result:
|
| 81 |
-
return {
|
| 82 |
-
"status": "not_found",
|
| 83 |
-
"message": f"{email} not found"
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
columns = [
|
| 87 |
-
"mobile",
|
| 88 |
-
"name",
|
| 89 |
-
"fname",
|
| 90 |
-
"address",
|
| 91 |
-
"alt",
|
| 92 |
-
"circle",
|
| 93 |
-
"id",
|
| 94 |
-
"email"
|
| 95 |
-
]
|
| 96 |
-
|
| 97 |
-
return {
|
| 98 |
-
"status": "success",
|
| 99 |
-
"data": dict(zip(columns, result))
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
except Exception as e:
|
| 103 |
-
return {
|
| 104 |
-
"status": "error",
|
| 105 |
-
"message": str(e)
|
| 106 |
-
}
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
@app.get("/stats")
|
| 110 |
-
async def stats():
|
| 111 |
-
try:
|
| 112 |
-
count = con.execute("SELECT COUNT(*) FROM users_view").fetchone()[0]
|
| 113 |
-
return {
|
| 114 |
-
"status": "success",
|
| 115 |
-
"total_records": count
|
| 116 |
-
}
|
| 117 |
-
except:
|
| 118 |
-
return {
|
| 119 |
-
"status": "error"
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
@app.get("/health")
|
| 124 |
-
async def health():
|
| 125 |
-
try:
|
| 126 |
-
con.execute("SELECT 1").fetchone()
|
| 127 |
-
return {
|
| 128 |
-
"status": "healthy",
|
| 129 |
-
"database": "connected"
|
| 130 |
-
}
|
| 131 |
-
except:
|
| 132 |
-
return {
|
| 133 |
-
"status": "unhealthy"
|
| 134 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|