diff --git a/src/rectes/main.py b/src/rectes/main.py index 0aa38c5..5e23523 100644 --- a/src/rectes/main.py +++ b/src/rectes/main.py @@ -1,8 +1,18 @@ from fastapi import FastAPI +from rectes.models import Secret, SecretSaveResult +from rectes.redis_db import redis +from rectes.utils import get_new_key + app = FastAPI() -@app.get("/") -def read_root(): - return {"Hello": "World"} +@app.post("/secret", response_model=SecretSaveResult) +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/models.py b/src/rectes/models.py new file mode 100644 index 0000000..c51f16d --- /dev/null +++ b/src/rectes/models.py @@ -0,0 +1,10 @@ +from pydantic import BaseModel + + +class Secret(BaseModel): + data: str + + +class SecretSaveResult(BaseModel): + status: str + retrieval_url: str 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