36 lines
833 B
Python
36 lines
833 B
Python
from fastapi import FastAPI
|
|
from fastapi.exceptions import HTTPException
|
|
|
|
from pssecret.models import Secret, SecretSaveResult
|
|
from pssecret.redis_db import redis
|
|
from pssecret.utils import get_new_key
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@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}",
|
|
}
|
|
|
|
|
|
@app.get(
|
|
"/secret/{secret_key}",
|
|
response_model=Secret,
|
|
responses={404: {"description": "The item was not found"}},
|
|
)
|
|
async def get_secret(secret_key):
|
|
data = await redis.get(secret_key)
|
|
|
|
if data is None:
|
|
raise HTTPException(404)
|
|
|
|
await redis.delete(secret_key)
|
|
return {
|
|
"data": data,
|
|
}
|