From 7266dd3cd58c4400f218bbf5446c90680e85c1e8 Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Sat, 17 Dec 2022 13:44:03 +0100 Subject: [PATCH] Basic implementation of creating new secret endpoint --- src/rectes/main.py | 19 +++++++++++++++++++ src/rectes/utils.py | 11 +++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/rectes/utils.py diff --git a/src/rectes/main.py b/src/rectes/main.py index 0aa38c5..ca2411f 100644 --- a/src/rectes/main.py +++ b/src/rectes/main.py @@ -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}", + } diff --git a/src/rectes/utils.py b/src/rectes/utils.py new file mode 100644 index 0000000..8fccdf2 --- /dev/null +++ b/src/rectes/utils.py @@ -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