Skip to content

Commit

Permalink
wip: config refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
grillazz committed Apr 7, 2024
1 parent ebcf103 commit 5d623b5
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
MONGODB_DATABASE: greenhouse
MONGODB_COLLECTION: greens
MONGODB_TEST: farmland
MONGODB_PARAMS: retryWrites=true&w=majority
MONGO_URL: mongodb://farmer:[email protected]:27017/?retryWrites=true&w=majority

services:
Expand Down
23 changes: 5 additions & 18 deletions greens/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -29,26 +15,27 @@ 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,
port=self.MONGODB_PORT,
username=self.MONGODB_USER,
password=self.MONGODB_PASSWORD,

path=self.mongodb_name, # TODO: url query params goes here
path=self.MONGODB_PARAMS
)


settings = Settings()
2 changes: 1 addition & 1 deletion greens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion greens/routers/v1/vegs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5d623b5

Please sign in to comment.