Using env vars for configuration instead of toml config

This commit is contained in:
Ivan Golikov 2024-12-27 00:34:54 +01:00
parent f7ab0697a5
commit 64edeb8d40
7 changed files with 33 additions and 33 deletions

2
.gitignore vendored
View file

@ -1,9 +1,9 @@
.env
.idea/
.nvim.lua
.python-version
.venv/
__pycache__/
build/
conf/pssecret.toml
dist/
pssecret.egg-info/

View file

@ -45,13 +45,10 @@ $ pssecret
### Configuration
Configuration is done through config file. By default, path is `/etc/pssecret/pssecret.toml`.
You can override this by setting environment variable `PSSECRET_CONF_FILE` value to actual file
location, e.g.:
Configuration is done via environment variables.
```console
$ PSSECRET_CONF_FILE=/home/user/.conf/pssecret.toml pssecret
```
Environment variables:
You can find all available configuration options in the example file, located
at [conf/pssecret.toml.example](conf/pssecret.toml.example) under Git root.
- `REDIS_URL`: URL for Redis access. Check what values are supported [here](https://redis.readthedocs.io/en/stable/connections.html#redis.Redis.from_url).
You can also declare these variables in a `.env` file in the working directory.

View file

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

22
poetry.lock generated
View file

@ -656,6 +656,26 @@ files = [
[package.dependencies]
typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0"
[[package]]
name = "pydantic-settings"
version = "2.7.0"
description = "Settings management using Pydantic"
optional = false
python-versions = ">=3.8"
files = [
{file = "pydantic_settings-2.7.0-py3-none-any.whl", hash = "sha256:e00c05d5fa6cbbb227c84bd7487c5c1065084119b750df7c8c1a554aed236eb5"},
{file = "pydantic_settings-2.7.0.tar.gz", hash = "sha256:ac4bfd4a36831a48dbf8b2d9325425b549a0a6f18cea118436d728eb4f1c4d66"},
]
[package.dependencies]
pydantic = ">=2.7.0"
python-dotenv = ">=0.21.0"
[package.extras]
azure-key-vault = ["azure-identity (>=1.16.0)", "azure-keyvault-secrets (>=4.8.0)"]
toml = ["tomli (>=2.0.1)"]
yaml = ["pyyaml (>=6.0.1)"]
[[package]]
name = "pygments"
version = "2.18.0"
@ -1119,4 +1139,4 @@ hiredis = ["hiredis"]
[metadata]
lock-version = "2.0"
python-versions = "^3.11"
content-hash = "1b9fc2055c3b8b01ce8590e50215ad011b9bcc2770f4c99e42a2523b99baa888"
content-hash = "1f2ca7562492fce7198a033828bce3cd8b9ed4cd38fc9e5c35784113bb816827"

View file

@ -3,4 +3,4 @@ from redis import asyncio as aioredis
from pssecret.settings import settings
redis = aioredis.from_url(settings.redis.url)
redis = aioredis.from_url(str(settings.redis_url))

View file

@ -1,26 +1,9 @@
import os
import tomllib
from pydantic import RedisDsn
from pydantic_settings import BaseSettings
class Settings:
def __init__(self, data: dict = None):
if data:
self._data = data
else:
with open(
os.getenv("PSSECRET_CONF_FILE", "/etc/pssecret/pssecret.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
class Settings(BaseSettings):
redis_url: RedisDsn = RedisDsn("redis://localhost")
settings = Settings()

View file

@ -23,6 +23,7 @@ pssecret = 'pssecret.cli:cli'
[tool.poetry.dependencies]
python = "^3.11"
pydantic-settings = "2.7.0"
click = "8.1.8"
fastapi = { version = "0.115.6", extras = [ "standard" ] }
redis = "5.2.1"