Using env vars for configuration instead of toml config
This commit is contained in:
parent
f7ab0697a5
commit
64edeb8d40
7 changed files with 33 additions and 33 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,9 +1,9 @@
|
||||||
|
.env
|
||||||
.idea/
|
.idea/
|
||||||
.nvim.lua
|
.nvim.lua
|
||||||
.python-version
|
.python-version
|
||||||
.venv/
|
.venv/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
build/
|
build/
|
||||||
conf/pssecret.toml
|
|
||||||
dist/
|
dist/
|
||||||
pssecret.egg-info/
|
pssecret.egg-info/
|
||||||
|
|
13
README.md
13
README.md
|
@ -45,13 +45,10 @@ $ pssecret
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
Configuration is done through config file. By default, path is `/etc/pssecret/pssecret.toml`.
|
Configuration is done via environment variables.
|
||||||
You can override this by setting environment variable `PSSECRET_CONF_FILE` value to actual file
|
|
||||||
location, e.g.:
|
|
||||||
|
|
||||||
```console
|
Environment variables:
|
||||||
$ PSSECRET_CONF_FILE=/home/user/.conf/pssecret.toml pssecret
|
|
||||||
```
|
|
||||||
|
|
||||||
You can find all available configuration options in the example file, located
|
- `REDIS_URL`: URL for Redis access. Check what values are supported [here](https://redis.readthedocs.io/en/stable/connections.html#redis.Redis.from_url).
|
||||||
at [conf/pssecret.toml.example](conf/pssecret.toml.example) under Git root.
|
|
||||||
|
You can also declare these variables in a `.env` file in the working directory.
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
redis.url = 'redis://localhost'
|
|
22
poetry.lock
generated
22
poetry.lock
generated
|
@ -656,6 +656,26 @@ files = [
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0"
|
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]]
|
[[package]]
|
||||||
name = "pygments"
|
name = "pygments"
|
||||||
version = "2.18.0"
|
version = "2.18.0"
|
||||||
|
@ -1119,4 +1139,4 @@ hiredis = ["hiredis"]
|
||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.0"
|
lock-version = "2.0"
|
||||||
python-versions = "^3.11"
|
python-versions = "^3.11"
|
||||||
content-hash = "1b9fc2055c3b8b01ce8590e50215ad011b9bcc2770f4c99e42a2523b99baa888"
|
content-hash = "1f2ca7562492fce7198a033828bce3cd8b9ed4cd38fc9e5c35784113bb816827"
|
||||||
|
|
|
@ -3,4 +3,4 @@ from redis import asyncio as aioredis
|
||||||
|
|
||||||
from pssecret.settings import settings
|
from pssecret.settings import settings
|
||||||
|
|
||||||
redis = aioredis.from_url(settings.redis.url)
|
redis = aioredis.from_url(str(settings.redis_url))
|
||||||
|
|
|
@ -1,26 +1,9 @@
|
||||||
import os
|
from pydantic import RedisDsn
|
||||||
import tomllib
|
from pydantic_settings import BaseSettings
|
||||||
|
|
||||||
|
|
||||||
class Settings:
|
class Settings(BaseSettings):
|
||||||
def __init__(self, data: dict = None):
|
redis_url: RedisDsn = RedisDsn("redis://localhost")
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
|
|
@ -23,6 +23,7 @@ pssecret = 'pssecret.cli:cli'
|
||||||
|
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = "^3.11"
|
python = "^3.11"
|
||||||
|
pydantic-settings = "2.7.0"
|
||||||
click = "8.1.8"
|
click = "8.1.8"
|
||||||
fastapi = { version = "0.115.6", extras = [ "standard" ] }
|
fastapi = { version = "0.115.6", extras = [ "standard" ] }
|
||||||
redis = "5.2.1"
|
redis = "5.2.1"
|
||||||
|
|
Loading…
Reference in a new issue