init
This commit is contained in:
commit
911c162b86
12
Dockerfile
Normal file
12
Dockerfile
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
FROM python:3.10-slim
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY requirements.txt .
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
EXPOSE 8000
|
||||||
|
|
||||||
|
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
18
docker-compose.yml
Normal file
18
docker-compose.yml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
app:
|
||||||
|
build: .
|
||||||
|
container_name: fastapi_app
|
||||||
|
ports:
|
||||||
|
- "8000:8000"
|
||||||
|
depends_on:
|
||||||
|
- redis
|
||||||
|
environment:
|
||||||
|
- REDIS_URL=redis://redis:6379
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:alpine
|
||||||
|
container_name: redis
|
||||||
|
ports:
|
||||||
|
- "6379:6379"
|
37
main.py
Normal file
37
main.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
from fastapi import FastAPI, HTTPException, Depends
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from aioredis import Redis
|
||||||
|
from aioredis.client import Redis as RedisClient
|
||||||
|
import aioredis
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
REDIS_URL = "redis://localhost:6379"
|
||||||
|
REDIS_KEY = "stored_string"
|
||||||
|
|
||||||
|
async def get_redis() -> RedisClient:
|
||||||
|
return await aioredis.from_url(REDIS_URL, encoding="utf-8", decode_responses=True)
|
||||||
|
|
||||||
|
class StringRequest(BaseModel):
|
||||||
|
text: str
|
||||||
|
|
||||||
|
@app.post("/string", status_code=201)
|
||||||
|
async def create_string(request: StringRequest, redis: Redis = Depends(get_redis)):
|
||||||
|
if await redis.exists(REDIS_KEY):
|
||||||
|
raise HTTPException(status_code=400, detail="String already exists. Use PATCH to update.")
|
||||||
|
await redis.set(REDIS_KEY, request.text)
|
||||||
|
return {"message": "String created successfully", "text": request.text}
|
||||||
|
|
||||||
|
@app.patch("/string")
|
||||||
|
async def update_string(request: StringRequest, redis: Redis = Depends(get_redis)):
|
||||||
|
if not await redis.exists(REDIS_KEY):
|
||||||
|
raise HTTPException(status_code=404, detail="No string found to update. Use POST to create one.")
|
||||||
|
await redis.set(REDIS_KEY, request.text)
|
||||||
|
return {"message": "String updated successfully", "text": request.text}
|
||||||
|
|
||||||
|
@app.get("/string")
|
||||||
|
async def get_string(redis: Redis = Depends(get_redis)):
|
||||||
|
stored_string = await redis.get(REDIS_KEY)
|
||||||
|
if stored_string is None:
|
||||||
|
raise HTTPException(status_code=404, detail="No string found.")
|
||||||
|
return {"text": stored_string}
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fastapi
|
||||||
|
aioredis[standard]
|
||||||
|
uvicorn
|
Loading…
x
Reference in New Issue
Block a user