Skip to content

Commit

Permalink
UPD handling of envs
Browse files Browse the repository at this point in the history
  • Loading branch information
eboileau committed Mar 8, 2024
1 parent 5210faa commit 26d7048
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
7 changes: 7 additions & 0 deletions docker/app_container/files/entry_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
FLASK_SECRET_FILE = f"{SECRETS_FOLDER}/flask-secret"
DATABASE_PASSWORD_FILE = f"{SECRETS_FOLDER}/mariadb-scimodom"

SMTP_SERVER = environ.get("SMTP_SERVER")
SMTP_FROM_ADDRESS = environ.get("SMTP_FROM_ADDRESS")
PUBLIC_URL = environ.get("PUBLIC_URL")


def write_env_file():
db_password = get_secret(DATABASE_PASSWORD_FILE)
Expand All @@ -18,6 +22,9 @@ def write_env_file():
SECRET_KEY={flask_secret}
SESSION_COOKIE_SAMESITE=None
SESSION_COOKIE_SECURE=True
SMTP_SERVER={SMTP_SERVER}
SMTP_FROM_ADDRESS={SMTP_FROM_ADDRESS}
PUBLIC_URL={PUBLIC_URL}
UPLOAD_PATH=/uploads
IMPORT_PATH=/import
DATA_PATH=/data
Expand Down
22 changes: 15 additions & 7 deletions docker/env_example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
DOCKER=podman
DOCKER_COMPOSE=podman-compose

# That should fit for the first instance created with podman-compose.
# In doubt check "podman-compose ps" or "docker ps".
DB_CONTAINER_NAME=docker_scimodom_db_1
APP_CONTAINER_NAME=docker_scimodom_app_1

DB_IMAGE_NAME=scimodom_db:latest
APP_IMAGE_NAME=scimodom_app:latest

# App environment variables

HTTP_PORT=8000

# We use gunicorn. The docs suggest 2*CPU worker processes.
Expand All @@ -23,13 +33,10 @@ HTTP_WORKER_TIMEOUT=120
# Enter here the URL to access the API.
HTTP_PUBLIC_URL=http://127.0.0.1:5000

# That should fit for the first instance created with podman-compose.
# In doubt check "podman-compose ps" or "docker ps".
DB_CONTAINER_NAME=docker_scimodom_db_1
APP_CONTAINER_NAME=docker_scimodom_app_1

DB_IMAGE_NAME=scimodom_db:latest
APP_IMAGE_NAME=scimodom_app:latest
# Email server settings
SMTP_SERVER=outgoing-email-server.my-site.org
[email protected]
PUBLIC_URL=https://sci-modom.my-site.org

# You may use relative paths below. However the backup
# scripts will only work if called from the right location
Expand Down Expand Up @@ -58,6 +65,7 @@ HOST_DB_DATA_DIR=./db_data
HOST_CONFIG_DIR=./config

# Only used for DEV setup

HOST_DEV_DB_DATA_DIR=./db_data_dev
MARIADB_DEV_PORT=3306

Expand Down
37 changes: 31 additions & 6 deletions server/src/scimodom/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
)


class MissingEnviron(Exception):
"""Exception handling for missing environment variable."""

pass


class Config:
"""Set Flask and logging variables.
Expand All @@ -21,16 +27,35 @@ class Config:
ENV_FILE: ClassVar[str] = os.getenv("ENV_FILE", ".env")
load_dotenv(ENV_FILE)

try:
DATABASE_URI: ClassVar[str] = os.environ["DATABASE_URI"]
SECRET_KEY: ClassVar[str] = os.environ["SECRET_KEY"]
except KeyError:
msg = (
"Undefined environment variable(s): DATABASE_URI and/or SECRET_KEY. "
"Check your .env file!"
)
raise MissingEnviron(msg)

try:
SMTP_SERVER: ClassVar[str] = os.environ["SMTP_SERVER"]
SMTP_FROM_ADDRESS: ClassVar[str] = os.environ["SMTP_FROM_ADDRESS"]
PUBLIC_URL: ClassVar[str] = os.environ["PUBLIC_URL"]
except KeyError:
msg = (
"Undefined environment variable(s): SMTP_SERVER, SMTP_FROM_ADDRESS, and/or PUBLIC_URL. "
"Check your .env file!"
)
raise MissingEnviron(msg)

FLASK_DEBUG: ClassVar[bool] = eval(os.getenv("FLASK_DEBUG", "False"))
DATABASE_URI: ClassVar[str | None] = os.getenv("DATABASE_URI")
SECRET_KEY: ClassVar[str | None] = os.getenv("SECRET_KEY")
SESSION_COOKIE_SAMESITE: ClassVar[str | None] = os.getenv("SESSION_COOKIE_SAMESITE")
SESSION_COOKIE_SECURE: ClassVar[str | None] = os.getenv("SESSION_COOKIE_SECURE")
SESSION_COOKIE_SECURE: ClassVar[bool] = eval(
os.getenv("SESSION_COOKIE_SECURE", "True")
)

JWT_SECRET_KEY: ClassVar[str | None] = SECRET_KEY
JWT_ACCESS_TOKEN_EXPIRES = datetime.timedelta(days=1)
SMTP_SERVER: ClassVar[str] = os.getenv("SMTP_SERVER")
SMTP_FROM_ADDRESS: ClassVar[str] = os.getenv("SMTP_FROM_ADDRESS")
PUBLIC_URL: ClassVar[str] = os.getenv("PUBLIC_URL")

IMPORT_PATH: ClassVar[str | Path] = os.getenv("IMPORT_PATH", "import")
DATA_PATH: ClassVar[str | Path] = os.getenv("DATA_PATH", "data")
Expand Down

0 comments on commit 26d7048

Please sign in to comment.