forked from thesephist/mira
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
37 lines (27 loc) · 867 Bytes
/
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
from deta import App, Deta
from fastapi import FastAPI, Request, responses
from fastapi.staticfiles import StaticFiles
deta = Deta()
db = deta.Base("people") # init the DB
# We are wrapping FastAPI with a Deta wrapper to be able to use `deta run`
# See line 31. It's optional.
app = App(FastAPI())
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
def index():
return responses.HTMLResponse(open("./index.html").read())
@app.post("/data")
async def post(r: Request):
items = await r.json()
for item in items:
db.put(item)
return item
@app.get("/data")
async def get():
return next(db.fetch())
# This command removes all the data frm the DB
# To trigger it, run `deta run` in the project's root
@app.lib.run()
def reset_db(event):
for item in next(db.fetch()):
db.delete(item["key"])