NirmitSachde commited on
Commit
9ee4216
·
verified ·
1 Parent(s): 6b2579d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Header, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ import json, os
4
+
5
+ app = FastAPI()
6
+ app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
7
+
8
+ DATA_FILE = "/data/billdata.json"
9
+ API_KEY = os.environ.get("API_KEY", "changeme")
10
+
11
+ os.makedirs("/data", exist_ok=True)
12
+
13
+ def auth(key):
14
+ if key != API_KEY:
15
+ raise HTTPException(401, "bad key")
16
+
17
+ @app.get("/data")
18
+ def getData(x_api_key: str = Header()):
19
+ auth(x_api_key)
20
+ if not os.path.exists(DATA_FILE):
21
+ return {"providers": [], "receivers": [], "settings": {}}
22
+ return json.loads(open(DATA_FILE).read())
23
+
24
+ @app.post("/data")
25
+ def saveData(body: dict, x_api_key: str = Header()):
26
+ auth(x_api_key)
27
+ with open(DATA_FILE, "w") as f:
28
+ json.dump(body, f)
29
+ return {"status": "ok"}