Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, Header, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import json, os | |
| app = FastAPI() | |
| app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]) | |
| DATA_FILE = "/data/billdata.json" | |
| API_KEY = os.environ.get("API_KEY", "changeme") | |
| os.makedirs("/data", exist_ok=True) | |
| def auth(key): | |
| if key != API_KEY: | |
| raise HTTPException(401, "bad key") | |
| def getData(x_api_key: str = Header()): | |
| auth(x_api_key) | |
| if not os.path.exists(DATA_FILE): | |
| return {"providers": [], "receivers": [], "settings": {}} | |
| return json.loads(open(DATA_FILE).read()) | |
| def saveData(body: dict, x_api_key: str = Header()): | |
| auth(x_api_key) | |
| with open(DATA_FILE, "w") as f: | |
| json.dump(body, f) | |
| return {"status": "ok"} |