-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
97 lines (79 loc) · 2.32 KB
/
main.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import traceback
from fastapi import FastAPI, HTTPException
from fastapi.requests import Request
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import json
from deta import Deta
async def catch_exceptions_middleware(request: Request, call_next):
try:
return await call_next(request)
except Exception as e:
err = traceback.format_exception(type(e), e, e.__traceback__)
return JSONResponse(content={"traceback": err}, status_code=500)
app = FastAPI()
# app.middleware('http')(catch_exceptions_middleware)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
deta = Deta('b01e4uo1_AKg3ZAFjdQkFwrUKxYPwmPRohk85egpb') # configure your Deta project
db = deta.Base('simpleDB')
db2 = deta.Base('simpleDB')
@app.patch("/lot/{lot_name}/increment/")
async def increment(lot_name):
updates = {
"count": db.util.increment()
}
return db.update(updates, lot_name)
@app.patch("/lot/{lot_name}/increment/{num}")
async def increment(lot_name, num):
updates = {
"count": db.util.increment(int(num))
}
return db.update(updates, lot_name)
@app.patch("/lot/{lot_name}/decrement/")
async def increment(lot_name):
updates = {
"count": db.util.increment(-1)
}
return db.update(updates, lot_name)
@app.get("/lot/{lot_name}")
def getLot(lot_name):
lot = db.get(lot_name)
return lot
@app.post("/lot/{lot_name}")
def createLot(lot_name):
lot = db.put({
"name": lot_name,
"count": 0,
}, lot_name)
return lot
@app.delete("/lot/{lot_name}")
def createLot(lot_name):
lot = db.delete(lot_name)
return lot
@app.get("/")
def getMap():
f = open('map.geojson', 'r')
map = json.load(f)
features = map.get("features")
for feature in features:
feature['properties'] = db.get(feature['properties']['name'])
f.close()
return map
@app.post("/reset/")
def reset():
print(db.fetch()._items)
for item in db.fetch()._items:
db.delete(item.name)
f = open('map.geojson', 'r')
map = json.load(f)
features = map.get("features")
for feature in features:
db.put(feature['properties'], feature['properties']['name'])
f.close()
return db.fetch()