From d1c3d98c2f5f1cb2e2c54d6765a8fb27fcc73a0f Mon Sep 17 00:00:00 2001 From: iamfoysal Date: Sun, 8 Dec 2024 19:54:53 +0600 Subject: [PATCH] Dockerized project run without Docker with production configuration --- README.md | 53 +++++++++++++++++++ .../config/settings/base.py | 14 ++++- .../merge_local_dotenvs_in_dotenv.py | 27 ++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 {{dxh_py.project_slug}}/merge_local_dotenvs_in_dotenv.py diff --git a/README.md b/README.md index 3c5e097..8aabc35 100644 --- a/README.md +++ b/README.md @@ -216,3 +216,56 @@ mv / Scattered throughout the Python and HTML of this project are places marked with "your stuff". This is where third-party libraries are to be integrated with your project. + +--- + +### Running a Dockerized Django Project Locally Using Uvicorn + +Follow these steps to run your Dockerized Django project locally using Uvicorn: + +--- + +#### **Step 1: Update the Configuration to Use `.env` Files** +1. Open the `config/base.py` file in your project. +2. Locate the following line: + ```python + READ_DOT_ENV_FILE = env.bool("READ_DOT_ENV_FILE", default=False) + ``` +3. Change the default value to `True`: + ```python + READ_DOT_ENV_FILE = env.bool("READ_DOT_ENV_FILE", default=True) + ``` + +This ensures the project reads environment variables from a single `.env` file. + +--- + +#### **Step 2: Generate the `.env` File** +Use the provided utility scripts to merge existing environment files into a single `.env` file. + +1. For **local development**, run: + ```bash + python3 merge_local_dotenvs_in_dotenv.py + ``` + +2. For **production**, create a `.env` file with production-specific values by running: + ```bash + python3 merge_production_dotenvs_in_dotenv.py + ``` + +This will generate a `.env` file in project root. Review the file and update database credentials or other values if necessary. + +--- + +#### **Step 3: Run the Project with Uvicorn** +Start the project using Uvicorn: + +```bash +uvicorn config.asgi:application --host 0.0.0.0 --reload --reload-include '*.html' +``` + +- **`--host 0.0.0.0`**: Makes the application accessible on all network interfaces. +- **`--reload`**: Enables auto-reloading for code changes. +- **`--reload-include '*.html'`**: Watches for changes in `.html` files for auto-reloading. + +--- diff --git a/{{dxh_py.project_slug}}/config/settings/base.py b/{{dxh_py.project_slug}}/config/settings/base.py index 7700891..3379c83 100644 --- a/{{dxh_py.project_slug}}/config/settings/base.py +++ b/{{dxh_py.project_slug}}/config/settings/base.py @@ -46,7 +46,19 @@ # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#databases {% if dxh_py.use_docker == "y" -%} -DATABASES = {"default": env.db("DATABASE_URL")} +if READ_DOT_ENV_FILE: + DATABASES = { + "default": { + "ENGINE": "django.db.backends.postgresql", + "NAME": env("POSTGRES_DB"), + "USER": env("POSTGRES_USER"), + "PASSWORD": env("POSTGRES_PASSWORD"), + "HOST": env("POSTGRES_HOST"), + "PORT": env("POSTGRES_PORT"), + } + } +else: + DATABASES = {"default": env.db("DATABASE_URL")} {%- else %} {% if dxh_py.database_engine == 'postgresql' -%} DATABASES = { diff --git a/{{dxh_py.project_slug}}/merge_local_dotenvs_in_dotenv.py b/{{dxh_py.project_slug}}/merge_local_dotenvs_in_dotenv.py new file mode 100644 index 0000000..edba7d5 --- /dev/null +++ b/{{dxh_py.project_slug}}/merge_local_dotenvs_in_dotenv.py @@ -0,0 +1,27 @@ +import os +from collections.abc import Sequence +from pathlib import Path + +BASE_DIR = Path(__file__).parent.resolve() +PRODUCTION_DOTENVS_DIR = BASE_DIR / ".envs" / ".local" +PRODUCTION_DOTENV_FILES = [ + PRODUCTION_DOTENVS_DIR / ".django", + PRODUCTION_DOTENVS_DIR / ".postgres", +] +DOTENV_FILE = BASE_DIR / ".env" + + +def merge( + output_file: Path, + files_to_merge: Sequence[Path], +) -> None: + merged_content = "" + for merge_file in files_to_merge: + merged_content += merge_file.read_text() + merged_content += os.linesep + + output_file.write_text(merged_content) + + +if __name__ == "__main__": + merge(DOTENV_FILE, PRODUCTION_DOTENV_FILES)