add redis

This commit is contained in:
sergey 2024-10-08 19:10:34 +03:00
parent 05c6f2b8da
commit 9020f8880b
10 changed files with 97 additions and 7 deletions

View File

@ -2,7 +2,15 @@ OAA_CFG__RUN__HOST=0.0.0.0
OAA_CFG__RUN__PORT=8000
OAA_CFG__RUN__RELOAD=True
OAA_CFG__LOG__LEVEL=20
OAA_CFG__TOKEN__ADMIN=pE0ULNppBtQOOxRuVw1tdiXTG5dZTaSx
OAA_CFG__TOKEN__USER=A3mojtenQqYwJAgrSiiFaVHX6fRoVW15
OAA_CFG__ADMIN_USER__LOGIN=admin
OAA_CFG__ADMIN_USER__PWD=P@ssw0rd!
OAA_CFG__REDDIS__HOST=localhost
OAA_CFG__REDDIS__PORT=6379
OAA_CFG__REDDIS__DB=0
OAA_CFG__REDDIS__LOGIN=admin
OAA_CFG__REDDIS__PWD=P@ssw0rd!

View File

@ -28,13 +28,13 @@ class LogConfig(BaseModel):
)
class Prefix(BaseModel):
class PrefixConfig(BaseModel):
swagger: str = "/docs"
api_v1: str = "/api/v1"
tg_v1: str = api_v1 + "/tg"
class Token(BaseModel):
class TokenConfig(BaseModel):
admin: str
user: str
@ -44,6 +44,14 @@ class AdminUser(BaseModel):
pwd: str
class ReddisConfig(BaseModel):
host: str = "localhost"
port: int = 6379
db: int = 0
login: str
pwd: str
class SwaggerConfig(BaseModel):
openapi_url: str = "/openapi.json"
title: str = "API"
@ -66,9 +74,10 @@ class Settings(BaseSettings):
run: RunConfig = RunConfig()
swagger: SwaggerConfig = SwaggerConfig()
log: LogConfig = LogConfig()
prefix: Prefix = Prefix()
token: Token
prefix: PrefixConfig = PrefixConfig()
token: TokenConfig
admin_user: AdminUser
reddis: ReddisConfig
conf = Settings()

10
main.py
View File

@ -5,12 +5,22 @@ from fastapi import FastAPI
from fastapi.responses import ORJSONResponse
from starlette.staticfiles import StaticFiles
from routers import router
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app: FastAPI):
yield
await db_helper.dispose()
main_app = FastAPI(
default_response_class=ORJSONResponse,
docs_url=None,
lifespan=lifespan,
)
main_app.include_router(router)
main_app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")

17
poetry.lock generated
View File

@ -528,6 +528,21 @@ files = [
{file = "pyyaml-6.0.2.tar.gz", hash = "sha256:d584d9ec91ad65861cc08d42e834324ef890a082e591037abe114850ff7bbc3e"},
]
[[package]]
name = "redis"
version = "5.1.1"
description = "Python client for Redis database and key-value store"
optional = false
python-versions = ">=3.8"
files = [
{file = "redis-5.1.1-py3-none-any.whl", hash = "sha256:f8ea06b7482a668c6475ae202ed8d9bcaa409f6e87fb77ed1043d912afd62e24"},
{file = "redis-5.1.1.tar.gz", hash = "sha256:f6c997521fedbae53387307c5d0bf784d9acc28d9f1d058abeac566ec4dbed72"},
]
[package.extras]
hiredis = ["hiredis (>=3.0.0)"]
ocsp = ["cryptography (>=36.0.1)", "pyopenssl (==23.2.1)", "requests (>=2.31.0)"]
[[package]]
name = "sniffio"
version = "1.3.1"
@ -829,4 +844,4 @@ files = [
[metadata]
lock-version = "2.0"
python-versions = "^3.12"
content-hash = "03bfacdc4e1c32b9de32fc239cf6219e6c07d189a07fe6d29f8d050fc7166f53"
content-hash = "94c62ee12b20f7b2cdc03c8d1e8e8e31478a5dc9eb541ada0119fbb8a4b62222"

View File

@ -12,6 +12,7 @@ pydantic-settings = "^2.5.2"
fastapi = "^0.115.0"
uvicorn = {extras = ["standard"], version = "^0.31.0"}
orjson = "^3.10.7"
redis = "^5.1.1"
[tool.poetry.group.dev.dependencies]

View File

@ -0,0 +1,6 @@
from .crud import get_value, set_value
__all__ = [
"get_value",
"set_value",
]

16
reddis/crud.py Normal file
View File

@ -0,0 +1,16 @@
import logging as log
from r_helper import RedisClient
async def set_value(key, value):
async with RedisClient() as redis_client:
await redis_client.set_value(key, value)
log.info("Set %s = %s", key, value)
async def get_value(key):
async with RedisClient() as redis_client:
value = await redis_client.get_value(key)
log.info("Get %s = %s", key, value)
return value

25
reddis/r_helper.py Normal file
View File

@ -0,0 +1,25 @@
from redis.asyncio import Redis
from config import conf
class RedisClient:
async def __aenter__(self):
self.client = Redis(
host=conf.reddis.host,
port=conf.reddis.port,
db=conf.reddis.db,
username=conf.reddis.login,
password=conf.reddis.pwd,
)
return self.client
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.client.close()
async def set_value(self, key, value):
async with self as client:
await client.set(key, value)
async def get_value(self, key):
async with self as client:
return await client.get(key)

View File

@ -43,6 +43,7 @@ pyproject_hooks==1.2.0
python-dotenv==1.0.1
PyYAML==6.0.2
RapidFuzz==3.10.0
redis==5.1.1
requests==2.32.3
requests-toolbelt==1.0.0
SecretStorage==3.3.3

View File

@ -11,7 +11,6 @@ router = APIRouter()
router.include_router(
swagger_router,
prefix=conf.prefix.swagger,
tags=["Swagger"],
)
router.include_router(
tg_send_router,