From 3d466c8e82e818f6613679781135a15ba0ed07a2 Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Mon, 13 Jun 2022 00:54:27 +0300 Subject: [PATCH 1/7] Running application with special command --- setup.cfg | 5 +++++ src/rectes/__init__.py | 5 +++++ src/rectes/cli.py | 6 ++++++ 3 files changed, 16 insertions(+) create mode 100644 src/rectes/cli.py diff --git a/setup.cfg b/setup.cfg index 384c067..abc8b2d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -8,6 +8,7 @@ packages = find: install_requires = fastapi==0.78.0 aioredis==2.0.1 + click==8.1.3 python_requires = >=3.10 [options.extras_require] @@ -16,5 +17,9 @@ hiredis = development = pre-commit==2.19.0 +[options.entry_points] +console_scripts = + rectes = rectes:cli + [options.packages.find] where = src diff --git a/src/rectes/__init__.py b/src/rectes/__init__.py index e69de29..482ed66 100644 --- a/src/rectes/__init__.py +++ b/src/rectes/__init__.py @@ -0,0 +1,5 @@ +__all__ = [ + "cli", +] + +from .cli import cli diff --git a/src/rectes/cli.py b/src/rectes/cli.py new file mode 100644 index 0000000..bb605b9 --- /dev/null +++ b/src/rectes/cli.py @@ -0,0 +1,6 @@ +import click + + +@click.command() +def cli(): + print("Hello, world") From 2897e81a11ca2990c707b6e56834c83ec58b4461 Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Mon, 13 Jun 2022 00:56:09 +0300 Subject: [PATCH 2/7] Added Installation section to README --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/README.md b/README.md index 3b13b1c..19fa8cb 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,47 @@ on a server will be IP address of the client that made request to the server. Service is built with Python, FastAPI and is using Redis for data storage. +## Installation + +### Requirements + +- Python 3.10+ +- Redis +- Python development libraries (optional, only needed for `hiredis` module installation) + +### How to install + +#### TL/DR + +``` +$ git clone git@git.ivnglkv.ru:ivnglkv/rectes.git +$ python3 -m venv venv +$ . ./venv/bin/activate +$ pip install ".[hiredis]" +``` + +--- + +Steps to install Rectes: + +1. Clone repository +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 . +``` + + ## Contributing Codestyle is enforced with Black, and additional checks are done with the help of pre-commit-hooks, From 846054922e9b93a5db72105ab8bef4835270035e Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Mon, 13 Jun 2022 01:03:13 +0300 Subject: [PATCH 3/7] Added instructions on how to start application to README --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 19fa8cb..a732117 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,14 @@ package without options: $ pip install . ``` +### Running Rectes server + +After installation is done, you can start rectes with `rectes` command. +The web server will be started with `uvicorn` ASGI web server. + +``` +$ rectes +``` ## Contributing From 8ee608364fbcc133f1e3e52671da3e3c45217283 Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Sat, 17 Dec 2022 09:37:41 +0100 Subject: [PATCH 4/7] Python 3.11 --- .pre-commit-config.yaml | 2 +- README.md | 2 +- setup.cfg | 7 +++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8a118a5..b75103d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ repos: rev: 22.3.0 hooks: - id: black - language_version: python3.10 + language_version: python3.11 - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.3.0 diff --git a/README.md b/README.md index a732117..d71b2f4 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Service is built with Python, FastAPI and is using Redis for data storage. ### Requirements -- Python 3.10+ +- Python 3.11+ - Redis - Python development libraries (optional, only needed for `hiredis` module installation) diff --git a/setup.cfg b/setup.cfg index abc8b2d..be2106f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -7,14 +7,13 @@ package_dir = = src packages = find: install_requires = fastapi==0.78.0 - aioredis==2.0.1 + redis[hiredis]==4.4.0 click==8.1.3 -python_requires = >=3.10 +python_requires = >=3.11 [options.extras_require] -hiredis = - hiredis==2.0.0 development = + uvicorn pre-commit==2.19.0 [options.entry_points] From 6e6ae931f7842f6b9fc085b525a14d917287f31b Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Sat, 17 Dec 2022 09:53:50 +0100 Subject: [PATCH 5/7] 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() From c7c6474432f087dea5b762165cc43623493bfd98 Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Sat, 17 Dec 2022 09:54:22 +0100 Subject: [PATCH 6/7] Bash syntax highlighting in README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ffeb56d..81583c9 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Service is built with Python, FastAPI and is using Redis for data storage. #### TL/DR -``` +```bash $ git clone git@git.ivnglkv.ru:ivnglkv/rectes.git $ python3 -m venv venv $ . ./venv/bin/activate @@ -42,7 +42,7 @@ Steps to install Rectes: After installation is done, you can start rectes with `rectes` command. The web server will be started with `uvicorn` ASGI web server. -``` +```bash $ rectes ``` @@ -64,7 +64,7 @@ at [conf/rectes.toml](conf/rectes.toml) under Git root. Codestyle is enforced with Black, and additional checks are done with the help of pre-commit-hooks, Flake8 and isort. Prior to making any commits, install `pre-commit` tool and install hooks: -``` +```bash # Alternatively, you could use 'pip install ".[development]"' $ pip install pre-commit==2.19.0 $ pre-commit install From b10f01dc702b9b9a80aba25935a978dd90664162 Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Sat, 17 Dec 2022 09:57:09 +0100 Subject: [PATCH 7/7] Fixed wrong link in the README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 81583c9..d360349 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ $ 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. +at [conf/rectes.toml.example](conf/rectes.toml.example) under Git root. ## Contributing