-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.py
44 lines (38 loc) · 1.3 KB
/
env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from dotenv import load_dotenv
import os
load_dotenv()
# Get the Database credentials
def get_db_credentials() -> list:
USER = str(os.environ.get("POSTGRES_USER")).strip()
HOST = str(os.getenv("POSTGRES_HOST")).strip()
PORT = str(os.environ.get("POSTGRES_PORT")).strip()
DATABASE = str(os.environ.get("POSTGRES_DB")).strip()
SECRET_KEY = str(os.getenv("SECRET_KEY")).strip()
DEBUG = os.getenv("DEBUG")
if not all([HOST, USER, DATABASE, SECRET_KEY, PORT]):
raise ValueError("Database credentials not set")
if os.environ.get("DEV") == "True":
HOST = "localhost"
PASSWORD = str(os.getenv("POSTGRES_PASSWORD")).strip()
DATABASE_URL = f"postgresql://{USER}:{PASSWORD}@{HOST}:{PORT}/{DATABASE}"
API_URL = str(os.getenv("API_URL")).strip()
else:
HOST = "database"
PORT = "5432"
PASSWORD = str(read_secret_file("/run/secrets/db-password")).strip()
DATABASE_URL = f"postgresql://{USER}:{PASSWORD}@{HOST}:{PORT}/{DATABASE}"
API_URL = "api"
return [
USER,
PASSWORD,
HOST,
DATABASE,
DATABASE_URL,
SECRET_KEY,
API_URL,
PORT,
DEBUG,
]
def read_secret_file(secret_path):
with open(secret_path, "r") as file:
return file.read().strip()