diff --git a/Dockerfile b/Dockerfile index d0edb12..4557baf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,6 @@ WORKDIR /app # App code COPY . . -COPY example.env .env # Install deps (only main/runtime deps) RUN poetry install --no-interaction --no-ansi --only main diff --git a/MANIFEST.in b/MANIFEST.in index 2be67bf..7a836d8 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1,2 @@ global-include *.yaml +global-include *.env diff --git a/README.md b/README.md index 4d48944..59b2651 100644 --- a/README.md +++ b/README.md @@ -50,11 +50,7 @@ Then run the following command to get up and running: poetry lock && poetry install --all-extras ``` -Once you have that, get a baseline `.env` and run the API: - -```shell -cp example.env .env -``` +Once you have that run the API: ```shell poetry run python3 icoapi/api.py @@ -75,13 +71,7 @@ INSTALL_DIR="/etc/icoapi" SERVICE_PATH="/etc/systemd/system" ``` -Please note that the install script expects that the root folder of the repository contains an `.env` configuration file. You can use `example.env` as starting point: - -```sh -cp example.env .env -``` - -After you created the configuration file, run the script to install normally: +Run the script to install normally: ```sh ./install.sh diff --git a/example.env b/icoapi/config/default.env similarity index 100% rename from example.env rename to icoapi/config/default.env diff --git a/icoapi/scripts/file_handling.py b/icoapi/scripts/file_handling.py index 5013a45..47c33bf 100644 --- a/icoapi/scripts/file_handling.py +++ b/icoapi/scripts/file_handling.py @@ -5,6 +5,8 @@ from typing import Tuple import shutil import re +from importlib.resources import files + from dotenv import load_dotenv from platformdirs import user_data_dir @@ -26,6 +28,11 @@ def load_env_file(): bundle_dir = sys._MEIPASS logger.warning(f"Environment variables not found in local directory. Trying to load from app data: {bundle_dir}") env_loaded = load_dotenv(os.path.join(bundle_dir, "config", ".env"), verbose=True) + if not env_loaded: + # Fourth try: load default configuration from package data + package_data = files('icoapi').joinpath("config") + logger.warning(f"Environment variables not found in app data. Trying to load from package data: %s", package_data) + env_loaded = load_dotenv(stream=(package_data.joinpath("default.env").open('r', encoding='utf-8'))) if not env_loaded: logger.critical(f"Environment variables not found") raise EnvironmentError(".env not found") diff --git a/install.sh b/install.sh index fcda584..3bd1f3c 100755 --- a/install.sh +++ b/install.sh @@ -27,7 +27,7 @@ sudo mkdir -p $INSTALL_DIR sudo chown $USER:$USER $INSTALL_DIR echo "Copying application files..." -FILES_AND_DIRS=("config" "$MODULE_NAME" "pyproject.toml" "README.md") +FILES_AND_DIRS=("$MODULE_NAME" "pyproject.toml" "README.md") for ITEM in "${FILES_AND_DIRS[@]}"; do #cp -r "$ITEM" "$INSTALL_DIR"