Create admin.py
Browse files- src/api/admin.py +50 -0
src/api/admin.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException, Request, Response
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import secrets
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
router = APIRouter()
|
| 7 |
+
|
| 8 |
+
class LoginRequest(BaseModel):
|
| 9 |
+
password: str
|
| 10 |
+
|
| 11 |
+
class ConfigUpdate(BaseModel):
|
| 12 |
+
key: str
|
| 13 |
+
value: str
|
| 14 |
+
|
| 15 |
+
# Simple session storage (in-memory for HF single instance)
|
| 16 |
+
admin_sessions = {}
|
| 17 |
+
|
| 18 |
+
@router.post("/auth")
|
| 19 |
+
async def admin_auth(req: LoginRequest):
|
| 20 |
+
from src.config import get_settings
|
| 21 |
+
settings = get_settings()
|
| 22 |
+
|
| 23 |
+
if req.password != settings.admin_password:
|
| 24 |
+
raise HTTPException(status_code=401, detail="Invalid password")
|
| 25 |
+
|
| 26 |
+
token = secrets.token_hex(32)
|
| 27 |
+
admin_sessions[token] = time.time() + 86400 # 24 hours
|
| 28 |
+
|
| 29 |
+
resp = Response(content='{"status": "ok"}')
|
| 30 |
+
resp.set_cookie(key="admin_session", value=token, httponly=True, max_age=86400)
|
| 31 |
+
return resp
|
| 32 |
+
|
| 33 |
+
@router.get("/config")
|
| 34 |
+
async def get_config():
|
| 35 |
+
from src.config import get_settings
|
| 36 |
+
settings = get_settings()
|
| 37 |
+
return {
|
| 38 |
+
"max_results": settings.max_results,
|
| 39 |
+
"maintenance_mode": settings.maintenance_mode
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
@router.post("/config")
|
| 43 |
+
async def update_config(req: ConfigUpdate, request: Request):
|
| 44 |
+
# Verify session
|
| 45 |
+
token = request.cookies.get("admin_session")
|
| 46 |
+
if not token or token not in admin_sessions:
|
| 47 |
+
raise HTTPException(status_code=401, detail="Not authenticated")
|
| 48 |
+
|
| 49 |
+
# In real implementation, update DB or env
|
| 50 |
+
return {"status": "ok", "updated": req.key}
|