Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,240 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from pathlib import Path
|
| 3 |
-
|
| 4 |
-
import duckdb
|
| 5 |
-
from fastapi import FastAPI, HTTPException, Query
|
| 6 |
-
|
| 7 |
-
app = FastAPI(
|
| 8 |
-
title="InayatGaming Authorized Lookup API",
|
| 9 |
-
description="Private authorized lookup service",
|
| 10 |
-
version="2.1.0",
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
# -------------------------------------------------------------------
|
| 14 |
-
# Configuration
|
| 15 |
-
# -------------------------------------------------------------------
|
| 16 |
-
|
| 17 |
-
API_KEY = os.getenv("API_KEY")
|
| 18 |
-
DB_PATH = os.getenv(
|
| 19 |
-
"DB_PATH",
|
| 20 |
-
"/data/authorized_index.duckdb"
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
# Fields intentionally exposed by this API.
|
| 24 |
-
# Do not add sensitive source fields here.
|
| 25 |
-
PUBLIC_FIELDS = [
|
| 26 |
-
"district",
|
| 27 |
-
"pincode",
|
| 28 |
-
"state",
|
| 29 |
-
"town",
|
| 30 |
-
"source",
|
| 31 |
-
]
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# -------------------------------------------------------------------
|
| 35 |
-
# Helpers
|
| 36 |
-
# -------------------------------------------------------------------
|
| 37 |
-
|
| 38 |
-
def authenticate(api_key: str) -> None:
|
| 39 |
-
"""Validate the API key."""
|
| 40 |
-
|
| 41 |
-
if not API_KEY:
|
| 42 |
-
raise HTTPException(
|
| 43 |
-
status_code=500,
|
| 44 |
-
detail="API_KEY is not configured",
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
if api_key != API_KEY:
|
| 48 |
-
raise HTTPException(
|
| 49 |
-
status_code=401,
|
| 50 |
-
detail="Invalid API key",
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
def database_exists() -> bool:
|
| 55 |
-
"""Check whether the authorized DuckDB index exists."""
|
| 56 |
-
|
| 57 |
-
return Path(DB_PATH).is_file()
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
# -------------------------------------------------------------------
|
| 61 |
-
# Root
|
| 62 |
-
# -------------------------------------------------------------------
|
| 63 |
-
|
| 64 |
-
@app.get("/")
|
| 65 |
-
def root():
|
| 66 |
-
return {
|
| 67 |
-
"success": True,
|
| 68 |
-
"service": "InayatGaming Authorized Lookup API",
|
| 69 |
-
"version": "2.1.0",
|
| 70 |
-
"status": "online",
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
# -------------------------------------------------------------------
|
| 75 |
-
# Health
|
| 76 |
-
# -------------------------------------------------------------------
|
| 77 |
-
|
| 78 |
-
@app.get("/health")
|
| 79 |
-
def health():
|
| 80 |
-
return {
|
| 81 |
-
"status": "ok",
|
| 82 |
-
"database": database_exists(),
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
# -------------------------------------------------------------------
|
| 87 |
-
# API information
|
| 88 |
-
# -------------------------------------------------------------------
|
| 89 |
-
|
| 90 |
-
@app.get("/info")
|
| 91 |
-
def info(api_key: str = Query(...)):
|
| 92 |
-
authenticate(api_key)
|
| 93 |
-
|
| 94 |
-
return {
|
| 95 |
-
"success": True,
|
| 96 |
-
"service": "InayatGaming Authorized Lookup API",
|
| 97 |
-
"version": "2.1.0",
|
| 98 |
-
"database_path": DB_PATH,
|
| 99 |
-
"database_installed": database_exists(),
|
| 100 |
-
"lookup_endpoint": "/lookup",
|
| 101 |
-
"returned_fields": PUBLIC_FIELDS,
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
# -------------------------------------------------------------------
|
| 106 |
-
# Index status
|
| 107 |
-
# -------------------------------------------------------------------
|
| 108 |
-
|
| 109 |
-
@app.get("/index-status")
|
| 110 |
-
def index_status():
|
| 111 |
-
"""
|
| 112 |
-
Shows whether the authorized database exists.
|
| 113 |
-
|
| 114 |
-
This endpoint intentionally does not expose database contents.
|
| 115 |
-
"""
|
| 116 |
-
|
| 117 |
-
if not database_exists():
|
| 118 |
-
return {
|
| 119 |
-
"installed": False,
|
| 120 |
-
"path": DB_PATH,
|
| 121 |
-
"message": "Authorized index is not installed",
|
| 122 |
-
}
|
| 123 |
-
|
| 124 |
-
try:
|
| 125 |
-
con = duckdb.connect(
|
| 126 |
-
DB_PATH,
|
| 127 |
-
read_only=True,
|
| 128 |
-
)
|
| 129 |
-
|
| 130 |
-
try:
|
| 131 |
-
result = con.execute(
|
| 132 |
-
"""
|
| 133 |
-
SELECT COUNT(*)
|
| 134 |
-
FROM authorized_lookup
|
| 135 |
-
"""
|
| 136 |
-
).fetchone()
|
| 137 |
-
|
| 138 |
-
row_count = int(result[0]) if result else 0
|
| 139 |
-
|
| 140 |
-
return {
|
| 141 |
-
"installed": True,
|
| 142 |
-
"rows": row_count,
|
| 143 |
-
}
|
| 144 |
-
|
| 145 |
-
finally:
|
| 146 |
-
con.close()
|
| 147 |
-
|
| 148 |
-
except Exception as exc:
|
| 149 |
-
return {
|
| 150 |
-
"installed": True,
|
| 151 |
-
"database_error": str(exc),
|
| 152 |
-
}
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
# -------------------------------------------------------------------
|
| 156 |
-
# Lookup
|
| 157 |
-
# -------------------------------------------------------------------
|
| 158 |
-
|
| 159 |
-
@app.get("/lookup")
|
| 160 |
-
def lookup(
|
| 161 |
-
api_key: str = Query(...),
|
| 162 |
-
number: str = Query(...),
|
| 163 |
-
):
|
| 164 |
-
"""
|
| 165 |
-
Perform an authorized lookup against the local index.
|
| 166 |
-
|
| 167 |
-
The API returns only the explicitly allowed fields.
|
| 168 |
-
"""
|
| 169 |
-
|
| 170 |
-
# Authenticate before touching the database.
|
| 171 |
-
authenticate(api_key)
|
| 172 |
-
|
| 173 |
-
number = number.strip()
|
| 174 |
-
|
| 175 |
-
if not number:
|
| 176 |
-
raise HTTPException(
|
| 177 |
-
status_code=400,
|
| 178 |
-
detail="number is required",
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
-
# Do not perform a remote Parquet scan.
|
| 182 |
-
if not database_exists():
|
| 183 |
-
raise HTTPException(
|
| 184 |
-
status_code=503,
|
| 185 |
-
detail="Authorized index is not installed",
|
| 186 |
-
)
|
| 187 |
-
|
| 188 |
-
con = None
|
| 189 |
-
|
| 190 |
-
try:
|
| 191 |
-
con = duckdb.connect(
|
| 192 |
-
DB_PATH,
|
| 193 |
-
read_only=True,
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
row = con.execute(
|
| 197 |
-
"""
|
| 198 |
-
SELECT
|
| 199 |
-
district,
|
| 200 |
-
pincode,
|
| 201 |
-
state,
|
| 202 |
-
town,
|
| 203 |
-
source
|
| 204 |
-
FROM authorized_lookup
|
| 205 |
-
WHERE lookup_key = ?
|
| 206 |
-
LIMIT 1
|
| 207 |
-
""",
|
| 208 |
-
[number],
|
| 209 |
-
).fetchone()
|
| 210 |
-
|
| 211 |
-
if row is None:
|
| 212 |
-
return {
|
| 213 |
-
"success": True,
|
| 214 |
-
"match": False,
|
| 215 |
-
"data": None,
|
| 216 |
-
}
|
| 217 |
-
|
| 218 |
-
data = {
|
| 219 |
-
"district": row[0],
|
| 220 |
-
"pincode": row[1],
|
| 221 |
-
"state": row[2],
|
| 222 |
-
"town": row[3],
|
| 223 |
-
"source": row[4],
|
| 224 |
-
}
|
| 225 |
-
|
| 226 |
-
return {
|
| 227 |
-
"success": True,
|
| 228 |
-
"match": True,
|
| 229 |
-
"data": data,
|
| 230 |
-
}
|
| 231 |
-
|
| 232 |
-
except duckdb.Error as exc:
|
| 233 |
-
raise HTTPException(
|
| 234 |
-
status_code=500,
|
| 235 |
-
detail=f"Database error: {exc}",
|
| 236 |
-
)
|
| 237 |
-
|
| 238 |
-
finally:
|
| 239 |
-
if con is not None:
|
| 240 |
-
con.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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