Spaces:
Sleeping
Sleeping
File size: 848 Bytes
9ee4216 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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")
@app.get("/data")
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())
@app.post("/data")
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"} |