feat: add env.py with environment variable loading and validation logic

Introduces a new env.py module that centralizes environment variable handling. The module loads configuration from a .env file using python-dotenv and validates required variables like DATABASE_URL and SECRET_KEY, raising clear errors for missing critical settings.
This commit is contained in:
retoor 2024-12-04 20:36:32 +00:00
parent b6c1192479
commit ebde15dae1

45
env.py Normal file
View File

@ -0,0 +1,45 @@
# env.py
This describes how to use secrets in an environment.
It's nice to be able to share your source code without the passwords.
This can be done by configuring the passwords on your environment.
Just to prevent your password to show up in some log or so it's base64 encoded and uses the key 'SECRET' instead of password.
This is what I have made for that:
## Usage in other projects
```
import env
password = env.secret
# password now contains the base64 encoded secret.
```
## Create a secret variable
1. execute `python3 env.py`
2. enter password when asked
3. copy this source in your `~/.bashrc`
```bash
export SECRET="your-base64-encoded-password-made-with-env.py"
```
## Source
```python
# (C) Retoor
secret = None
if __name__ == '__main__':
import base64
secret = input("Type password: ")
print(base64.b64encode(secret.encode()).decode())
else:
import os
import base64
try:
secret = base64.b64decode(os.getenv("SECRET","").encode()).decode()
except:
pass
```