-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.py
47 lines (40 loc) · 1.4 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
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
import os
import redis
redis_client = redis.StrictRedis(host='0.0.0.0', port=6379, db=0, decode_responses=True)
app = FastAPI()
app.mount("/.well-known", StaticFiles(directory=".well-known"), name="static")
# Route to list all TODOs
@app.get("/todos")
def list_todos():
todos = {}
for key in redis_client.keys():
if key != 'todo_id':
todos[key] = "["+key+"] "+str(redis_client.get(key))
return todos
# Route to list a specific TODO
@app.get("/todos/{todo_id}")
def list_todo(todo_id: int):
todo = redis_client.get(str(todo_id))
if todo:
return {"todo_id": todo_id, "todo": todo}
else:
raise HTTPException(status_code=404, detail="Todo not found")
# Route to add a TODO
@app.post("/todos")
def add_todo(todo: str):
# Generate a unique todo_id
todo_id = redis_client.incr('todo_id')
redis_client.set(str(todo_id), todo)
return {"todo_id": todo_id, "todo": todo}
# Route to delete a TODO
@app.delete("/todos/{todo_id}")
def delete_todo(todo_id: int):
if not redis_client.exists(str(todo_id)):
raise HTTPException(status_code=404, detail="Todo not found")
redis_client.delete(str(todo_id))
return {"result": "Todo deleted"}
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, log_level="info")