-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
143 lines (109 loc) · 3.72 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from typing import Any, Dict
from fastapi import FastAPI, Depends, HTTPException, status, Security
from fastapi.security import APIKeyHeader
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from connections.redis_db import REDIS_CLIENT as r
from frontend.index_html import html
from helper_functions.helpers import getallpairs, getallpairs_starswith, getone
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(title="FastAPI CURD on Redis")
app.mount("/static", StaticFiles(directory="frontend/static"), name="static")
origins = [
"http://localhost:5500",
"http://localhost",
"http://localhost:8080",
"https://aetest.andierni.ch",
"https://quiz.andierni.ch",
"http://127.0.0.1:5500",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Start Api-Key security setup
# allowed keys
api_keys = [
"my_api_key",
"letmein"
]
# the name of the header that must be provided
api_key_header = APIKeyHeader(name="REDIS_CRUD_API_KEY", auto_error=False)
def get_api_key(
api_key_header: str = Security(api_key_header),
) -> str:
if api_key_header in api_keys:
return api_key_header
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid or missing API Key",
)
# End Api-Key security setup
# protect a route like this:
@app.get("/protected", tags=["Security Example"])
def private(api_key: str = Security(get_api_key)):
"""A private endpoint that requires a valid API key to be provided in the header."""
return f"Private Endpoint reached sucessfully. API Key: {api_key}"
#root route - see frontend
@app.get("/", tags=["Frontend Landing Page"])
async def root():
return HTMLResponse(html)
### CRUD ###
## CREATE ##
# Create route
@app.post("/create", tags=["CREATE"])
def create(key: str, value: Any):
r.set(key, value)
return {"message": "Record created successfully"}
# Create route for hash (dict)
@app.post("/create_dict", tags=["CREATE"])
def create_dict(key: str, value: dict):
r.hset(key, mapping=value)
return {"message": "Record created successfully"}
## READ ##
@app.get("/read_one", tags=["READ"])
async def read_one(result: dict = Depends(getone)):
return result
# Read all key, value pairs
@app.get("/read_all", tags=["READ"])
def read_all(allpairs: Dict = Depends(getallpairs)):
return allpairs
# Read all key, value pais that start with
@app.get("/read_all_startwith", tags=["READ"])
def read_all_startwith(allpairs_startwith: Dict = Depends(getallpairs_starswith)):
return allpairs_startwith
## UPDATE ##
# Update route
@app.put("/update", tags=["UPDATE"])
def update(key: str, value: Any):
if not r.exists(key):
return {"message": "Key not found"}
r.set(key, value)
return {"message": "Record updated successfully"}
# Update dict route
@app.put("/update_dict", tags=["UPDATE"])
def update_dict(key: str, value: dict):
if not r.exists(key):
return {"message": "Key not found"}
r.hset(key, mapping=value)
return {"message": "Record updated successfully"}
## DELETE ##
# Delete route
@app.delete("/delete", tags=["DELETE"])
def delete(key: str):
if not r.exists(key):
return {"message": "Key not found"}
r.delete(key)
return {"message": "Record deleted successfully"}
# Delete keys that starts with
@app.delete("/delete_startswith", tags=["DELETE"])
def delete_startswith(key: str):
keys = r.keys(f"{key}*")
if not keys:
return {"message": f"No key found that starts with {key}"}
for key in keys:
r.delete(key)
return {"message": "Records deleted successfully"}