Basic Redis connection configuration

This commit is contained in:
Ivan Golikov 2022-12-17 09:53:50 +01:00
parent 8ee608364f
commit 6e6ae931f7
5 changed files with 49 additions and 14 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ venv/
dist/ dist/
rectes.egg-info/ rectes.egg-info/
build/ build/
conf/rectes.toml

View file

@ -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 $ git clone git@git.ivnglkv.ru:ivnglkv/rectes.git
$ python3 -m venv venv $ python3 -m venv venv
$ . ./venv/bin/activate $ . ./venv/bin/activate
$ pip install ".[hiredis]" $ pip install .
``` ```
--- ---
@ -37,19 +37,6 @@ Steps to install Rectes:
2. (optional) Create virtual environment 2. (optional) Create virtual environment
3. Install package 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 ### Running Rectes server
After installation is done, you can start rectes with `rectes` command. 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 $ 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 ## Contributing
Codestyle is enforced with Black, and additional checks are done with the help of pre-commit-hooks, Codestyle is enforced with Black, and additional checks are done with the help of pre-commit-hooks,

1
conf/rectes.toml.example Normal file
View file

@ -0,0 +1 @@
redis.url = 'redis://localhost'

6
src/rectes/redis_db.py Normal file
View file

@ -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)

27
src/rectes/settings.py Normal file
View file

@ -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()