Basic implementation of creating new secret endpoint
This commit is contained in:
parent
d80db89989
commit
7266dd3cd5
2 changed files with 30 additions and 0 deletions
|
@ -1,8 +1,27 @@
|
|||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
from rectes.redis_db import redis
|
||||
from rectes.utils import get_new_key
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Secret(BaseModel):
|
||||
data: str
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"Hello": "World"}
|
||||
|
||||
|
||||
@app.post("/secret")
|
||||
async def set_secret(data: Secret):
|
||||
new_key = await get_new_key()
|
||||
await redis.setex(new_key, 60 * 60 * 24, data.data)
|
||||
|
||||
return {
|
||||
"status": "saved",
|
||||
"retrieval_url": f"/secret/{new_key}",
|
||||
}
|
||||
|
|
11
src/rectes/utils.py
Normal file
11
src/rectes/utils.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
from uuid import uuid4
|
||||
|
||||
from rectes.redis_db import redis
|
||||
|
||||
|
||||
async def get_new_key() -> str:
|
||||
while True:
|
||||
new_key = str(uuid4())
|
||||
|
||||
if not await redis.exists(new_key):
|
||||
return new_key
|
Loading…
Reference in a new issue