-
Notifications
You must be signed in to change notification settings - Fork 2
Read MongoDB connection parameters from environment variables #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
eecavanna
merged 17 commits into
main
from
53-update-fastapi-application-obtain-mongodb-credentials-from-environment-not-hard-coded
Jul 19, 2025
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8d2fef2
Get MongoDB connection parameters from `.env` file
eecavanna cdeede5
Add test that involves accessing MongoDB database
eecavanna ae455cd
Use `ruff` to format Python code
eecavanna 929fa82
Configure GHA step to fail if container exits with non-zero code
eecavanna ac7a8ed
Override `MONGO_HOST` environment variable for GHA tests
eecavanna 09374c0
Use standard shell syntax for setting environment variable
eecavanna 0a5a48a
Use same `MONGO_HOST` value for entire Docker Compose stack
eecavanna f608fc7
Customize `MONGO_HOST` in a GHA-compatible way
eecavanna 817dbef
Customize `MONGO_USERNAME` and `MONGO_PASSWORD` for GHA container stack
eecavanna 7c6d6a4
Prevent running GHA workflow twice in response to same event
eecavanna bb50b28
Propagate host env vars into containers and run all tests locally also
eecavanna bf9575a
Prevent local test step from using `.venv/` leftover by container
eecavanna 77f175f
Refine comments
eecavanna 8dafd53
Use privileged user to delete `.venv/` leftover by container
eecavanna f849844
Run tests only within Docker Compose stack, not on host (simpler setup)
eecavanna 59c8a17
Display error message when host or `.env` file lacks required env. var.
eecavanna cc54a52
Define environment variables for `docker compose down` step
eecavanna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # This file specifies directories/files we want Docker to omit from the Docker build context. | ||
| # Docs: https://docs.docker.com/build/concepts/context/#dockerignore-files | ||
|
|
||
| # Omit Python virtual environments. | ||
| .venv/ | ||
|
|
||
| # Omit Python cache files. | ||
| __pycache__/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # See `src/config.py` for documentation about these environment variables. | ||
|
|
||
| MONGO_HOST=mongo | ||
| MONGO_PORT=27017 | ||
| MONGO_USERNAME=admin | ||
| MONGO_PASSWORD=root | ||
| MONGO_DATABASE=bertron |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,17 @@ services: | |
| # Use the container image built in the "development" stage of the Dockerfile. | ||
| build: { context: ".", dockerfile: Dockerfile, target: development } | ||
| restart: unless-stopped | ||
| # Propagate environment variables from the host into the container. | ||
| # Note: The `:?` suffix makes it so Docker Compose displays an error if the | ||
| # environment variable is either (a) undefined, or (b) empty, in the | ||
| # host environment. | ||
| # Docs: https://docs.docker.com/compose/how-tos/environment-variables/variable-interpolation/#additional-information | ||
| environment: | ||
| MONGO_HOST: ${MONGO_HOST:?} | ||
| MONGO_PORT: ${MONGO_PORT:?} | ||
| MONGO_USERNAME: ${MONGO_USERNAME:?} | ||
| MONGO_PASSWORD: ${MONGO_PASSWORD:?} | ||
| MONGO_DATABASE: ${MONGO_DATABASE:?} | ||
| ports: | ||
| # Map a host port (by default, 8000, but it can be overridden via an | ||
| # environment variable) to port 8000 of the container; the latter being | ||
|
|
@@ -30,8 +41,8 @@ services: | |
| - "${MONGO_PORT:-27017}:27017" | ||
| restart: unless-stopped | ||
| environment: | ||
| MONGO_INITDB_ROOT_USERNAME: admin | ||
| MONGO_INITDB_ROOT_PASSWORD: root | ||
| MONGO_INITDB_ROOT_USERNAME: "${MONGO_USERNAME:?}" | ||
| MONGO_INITDB_ROOT_PASSWORD: "${MONGO_PASSWORD:?}" | ||
| volumes: | ||
| - mongo_data:/data/db | ||
|
|
||
|
|
@@ -40,9 +51,6 @@ services: | |
| build: { context: ".", dockerfile: Dockerfile, target: development } | ||
| # This service should not start automatically - only run on demand | ||
| profiles: ["tools"] | ||
| environment: | ||
| # Set the MongoDB connection string to connect to the mongo service | ||
| MONGO_URI: "mongodb://admin:root@mongo:27017" | ||
|
Comment on lines
-43
to
-45
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was not being used. Rather than update it to make it reference the newly-introduced environment variables (but still not be used), I opted to delete it. |
||
| volumes: | ||
| # Mount the root directory to access the ingest script and data files | ||
| - ".:/app" | ||
|
|
@@ -56,6 +64,12 @@ services: | |
| build: { context: ".", dockerfile: Dockerfile, target: development } | ||
| # This service should not start automatically - only run on demand | ||
| profiles: ["tools"] | ||
| environment: | ||
| MONGO_HOST: ${MONGO_HOST:?} | ||
| MONGO_PORT: ${MONGO_PORT:?} | ||
| MONGO_USERNAME: ${MONGO_USERNAME:?} | ||
| MONGO_PASSWORD: ${MONGO_PASSWORD:?} | ||
| MONGO_DATABASE: ${MONGO_DATABASE:?} # the test suite will disregard this | ||
| depends_on: | ||
| - app | ||
| - mongo | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from typing import Optional | ||
|
|
||
| from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
|
||
|
|
||
| class Settings(BaseSettings): | ||
| r""" | ||
| Configuration settings for the application. | ||
|
|
||
| Order of precedence (highest to lowest) for settings values: | ||
| 1. Process-level environment variable definitions. | ||
| 2. Environment variable definitions loaded from a `.env` file. | ||
| 3. Default values defined in this class. | ||
|
|
||
| References: | ||
| - https://docs.pydantic.dev/latest/concepts/pydantic_settings/#dotenv-env-support | ||
| """ | ||
|
|
||
| # Load environment variable definitions from a `.env` file, if one exists, | ||
| # in the current working directory. We allow the `.env` file to contain | ||
| # extra environment variables (beyond those modeled below), since this | ||
| # repository currently contains a variety of independent applications. | ||
| model_config = SettingsConfigDict(env_file=".env", extra="ignore") | ||
|
|
||
| # MongoDB connection settings. | ||
| mongo_host: str = "localhost" | ||
| mongo_port: int = 27017 # note: env. var. value will be coerced into int | ||
| mongo_username: Optional[str] = None | ||
| mongo_password: Optional[str] = None | ||
| mongo_database: str = "bertron" | ||
|
|
||
|
|
||
| # Instantiate a settings object that can be imported into other modules. | ||
| settings = Settings() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| r""" | ||
| This module contains `pytest` fixture definitions that `pytest` will automatically make available | ||
| to all tests within this directory and its descendant directories. | ||
|
|
||
| From the `pytest` documentation: | ||
| > The `conftest.py` file serves as a means of providing fixtures for an entire directory. | ||
| > Fixtures defined in a `conftest.py` can be used by any test in that package without | ||
| > needing to import them (`pytest` will automatically discover them). | ||
| Source: https://docs.pytest.org/en/stable/reference/fixtures.html#conftest-py-sharing-fixtures-across-multiple-files | ||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
| from config import settings as cfg | ||
|
|
||
|
|
||
| # Note: We use `autouse=True` so that this fixture is automatically applied to each test | ||
| # within its scope (since we are in a `conftest.py` file, its scope consists of | ||
| # the current directory and all descendant directories). | ||
| @pytest.fixture(autouse=True) | ||
| def patched_cfg(): | ||
| r""" | ||
| A `pytest` fixture that temporarily patches the application configuration | ||
| so it references a test database. | ||
| """ | ||
|
|
||
| test_database_name = "bertron_test" | ||
|
eecavanna marked this conversation as resolved.
|
||
| main_database_name = cfg.mongo_database | ||
| assert main_database_name != test_database_name, ( | ||
| "The main database name matches the test database name. " | ||
| "Reconfigure your environment to ensure they differ." | ||
| ) | ||
| cfg.mongo_database = test_database_name | ||
| yield cfg | ||
| cfg.mongo_database = main_database_name | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Screenshot from before I made this change: