From 5d623b5ad8f1398eea8738ebe1356c471d5d1278 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sun, 7 Apr 2024 17:21:54 +0200 Subject: [PATCH] wip: config refactor --- .github/workflows/build-and-test.yml | 1 + greens/config.py | 23 +++++------------------ greens/main.py | 2 +- greens/routers/v1/vegs.py | 2 +- tests/conftest.py | 2 +- 5 files changed, 9 insertions(+), 21 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index d7107db..7ffb2b5 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -30,6 +30,7 @@ jobs: MONGODB_DATABASE: greenhouse MONGODB_COLLECTION: greens MONGODB_TEST: farmland + MONGODB_PARAMS: retryWrites=true&w=majority MONGO_URL: mongodb://farmer:tractor@127.0.0.1:27017/?retryWrites=true&w=majority services: diff --git a/greens/config.py b/greens/config.py index 3830beb..30152b3 100644 --- a/greens/config.py +++ b/greens/config.py @@ -6,21 +6,7 @@ class Settings(BaseSettings): - """ - Represents the configuration settings for the application. - - Args: - environment (str): The environment in which the application is running. Defaults to "local". - testing (str): The testing mode of the application. Defaults to "0". - up (str): The up status of the application. Defaults to "up". - down (str): The down status of the application. Defaults to "down". - web_server (str): The web server used by the application. Defaults to "web_server". - db_url (str): The URL of the MongoDB database. - db_name (str): The name of the MongoDB database. - collection (str): The name of the MongoDB collection. - test_db_name (str): The name of the MongoDB test database. - - """ + """Settings for the application""" environment: str = os.getenv("ENVIRONMENT", "local") testing: str = os.getenv("TESTING", "0") up: str = os.getenv("UP", "up") @@ -29,18 +15,18 @@ class Settings(BaseSettings): db_url: str = os.getenv("MONGO_URL", "") mongodb_database: str = os.getenv("MONGODB_DATABASE", "") - collection: str = os.getenv("MONGODB_COLLECTION", "") + mongodb_collection: str = os.getenv("MONGODB_COLLECTION", "") mongodb_test: str = os.getenv("MONGODB_TEST", "") MONGODB_HOST: str MONGODB_PORT: int MONGODB_USER: str MONGODB_PASSWORD: str + MONGODB_PARAMS: str @computed_field @property def mongodb_url(self) -> MongoDsn: - return MultiHostUrl.build( scheme="mongodb", host=self.MONGODB_HOST, @@ -48,7 +34,8 @@ def mongodb_url(self) -> MongoDsn: username=self.MONGODB_USER, password=self.MONGODB_PASSWORD, - path=self.mongodb_name, # TODO: url query params goes here + path=self.MONGODB_PARAMS ) + settings = Settings() diff --git a/greens/main.py b/greens/main.py index 223aeb1..a09ea7f 100644 --- a/greens/main.py +++ b/greens/main.py @@ -16,7 +16,7 @@ async def lifespan(app: FastAPI): app.state.logger = get_logger(__name__) app.state.logger.info("Starting greens on your farmland...mmm") app.state.mongo_client, app.state.mongo_db, app.state.mongo_collection = await init_mongo( - global_settings.mongodb_database, global_settings.db_url, global_settings.collection + global_settings.mongodb_database, global_settings.mongodb_url, global_settings.mongodb_collection ) try: yield diff --git a/greens/routers/v1/vegs.py b/greens/routers/v1/vegs.py index 2160fa1..0abe7dc 100644 --- a/greens/routers/v1/vegs.py +++ b/greens/routers/v1/vegs.py @@ -6,7 +6,7 @@ from greens.schemas.vegs import Document, DocumentResponse, ObjectIdField from greens.services.repository import create_document, retrieve_document -collection = global_settings.collection +collection = global_settings.mongodb_collection router = APIRouter() diff --git a/tests/conftest.py b/tests/conftest.py index 7aadf8c..41b4dee 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -25,6 +25,6 @@ async def client() -> AsyncGenerator: ) as client: app.state.logger = get_logger(__name__) app.state.mongo_client, app.state.mongo_db, app.state.mongo_collection = await init_mongo( - global_settings.mongodb_test, global_settings.db_url, global_settings.collection + global_settings.mongodb_test, global_settings.db_url, global_settings.mongodb_collection ) yield client