From 6e6ae931f7842f6b9fc085b525a14d917287f31b Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Sat, 17 Dec 2022 09:53:50 +0100 Subject: [PATCH] Basic Redis connection configuration --- .gitignore | 1 + README.md | 28 ++++++++++++++-------------- conf/rectes.toml.example | 1 + src/rectes/redis_db.py | 6 ++++++ src/rectes/settings.py | 27 +++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 conf/rectes.toml.example create mode 100644 src/rectes/redis_db.py create mode 100644 src/rectes/settings.py diff --git a/.gitignore b/.gitignore index 615ddc6..f6c4a2c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ venv/ dist/ rectes.egg-info/ build/ +conf/rectes.toml diff --git a/README.md b/README.md index d71b2f4..ffeb56d 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Service is built with Python, FastAPI and is using Redis for data storage. $ git clone git@git.ivnglkv.ru:ivnglkv/rectes.git $ python3 -m venv venv $ . ./venv/bin/activate -$ pip install ".[hiredis]" +$ pip install . ``` --- @@ -37,19 +37,6 @@ Steps to install Rectes: 2. (optional) Create virtual environment 3. Install package -#### Optional `hiredis` module - -Rectes server is using `aioredis` library for interaction with Redis. It's authors recommend using -it with `hiredis` module for performance and stability reasons -([source](https://github.com/aio-libs/aioredis-py#installation)). -Rectes offers `hiredis` as optional but recommended dependency too. Thus, the recommended way to install -Rectes will be with `[hiredis]` option. If you don't want to use `hiredis` for any reasons, install -package without options: - -``` -$ pip install . -``` - ### Running Rectes server After installation is done, you can start rectes with `rectes` command. @@ -59,6 +46,19 @@ The web server will be started with `uvicorn` ASGI web server. $ rectes ``` +### Configuration + +Configuration is done through config file. By default, path is `/etc/rectes/rectes.toml`. +You can override this by setting environment variable `RECTES_CONF_FILE` value to actual file +location, i.e.: + +```bash +$ RECTES_CONF_FILE=/home/user/.conf/rectes.toml rectes +``` + +You can find all available configuration options in the example file, located +at [conf/rectes.toml](conf/rectes.toml) under Git root. + ## Contributing Codestyle is enforced with Black, and additional checks are done with the help of pre-commit-hooks, diff --git a/conf/rectes.toml.example b/conf/rectes.toml.example new file mode 100644 index 0000000..1458255 --- /dev/null +++ b/conf/rectes.toml.example @@ -0,0 +1 @@ +redis.url = 'redis://localhost' diff --git a/src/rectes/redis_db.py b/src/rectes/redis_db.py new file mode 100644 index 0000000..0cefcb3 --- /dev/null +++ b/src/rectes/redis_db.py @@ -0,0 +1,6 @@ +# noinspection PyUnresolvedReferences,PyProtectedMember +from redis import asyncio as aioredis + +from rectes.settings import settings + +redis = aioredis.from_url(settings.redis.url) diff --git a/src/rectes/settings.py b/src/rectes/settings.py new file mode 100644 index 0000000..8502888 --- /dev/null +++ b/src/rectes/settings.py @@ -0,0 +1,27 @@ +import os + +import tomllib + + +class Settings: + def __init__(self, data: dict = None): + if data: + self._data = data + else: + with open( + os.getenv("RECTES_CONF_FILE", "/etc/rectes/rectes.toml"), "rb" + ) as f: + self._data = tomllib.load(f) + + def __getattr__(self, item): + try: + value = self._data[item] + except KeyError: + raise AttributeError + if isinstance(value, dict): + return Settings(data=value) + else: + return value + + +settings = Settings()