Skip to content

Commit

Permalink
Merge pull request #64 from grillazz/63-refactor-project-config
Browse files Browse the repository at this point in the history
63 refactor project config
  • Loading branch information
grillazz committed Apr 7, 2024
2 parents a2f1793 + 5d623b5 commit d969d2e
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 45 deletions.
14 changes: 7 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ UP=up
DOWN=down
WEB_SERVER=web_server

MONGO_HOST=mongodb
MONGO_PORT=27017
MONGO_USER=farmer
MONGO_PASS=tractor
MONGO_DB=greenhouse
MONGO_COLLECTION=greens
MONGO_TEST_DB=farmland
MONGODB_HOST=mongodb
MONGODB_PORT=27017
MONGODB_USER=farmer
MONGODB_PASSWORD=tractor
MONGODB_DATABASE=greenhouse
MONGODB_COLLECTION=greens
MONGODB_TEST=farmland
MONGO_URL=mongodb://farmer:tractor@mongodb:27017/?retryWrites=true&w=majority

15 changes: 8 additions & 7 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ jobs:
UP: up
DOWN: down
WEB_SERVER: web_server
MONGO_HOST: mongodb
MONGO_PORT: 27017
MONGO_USER: farmer
MONGO_PASS: tractor
MONGO_DB: greenhouse
MONGO_COLLECTION: greens
MONGO_TEST_DB: farmland
MONGODB_HOST: mongodb
MONGODB_PORT: 27017
MONGODB_USER: farmer
MONGODB_PASSWORD: tractor
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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Jakub Miazek
Copyright (c) 2024 Jakub Miazek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ services:
ports:
- "27017:27017"
environment:
- "MONGO_INITDB_DATABASE=${MONGO_DB}"
- "MONGO_INITDB_ROOT_USERNAME=${MONGO_USER}"
- "MONGO_INITDB_ROOT_PASSWORD=${MONGO_PASS}"
- "MONGO_INITDB_DATABASE=${MONGODB_DATABASE}"
- "MONGO_INITDB_ROOT_USERNAME=${MONGODB_USER}"
- "MONGO_INITDB_ROOT_PASSWORD=${MONGODB_PASSWORD}"
command:
mongod --quiet --logpath /dev/null
43 changes: 25 additions & 18 deletions greens/config.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
import os

from pydantic import MongoDsn, computed_field
from pydantic_core import MultiHostUrl
from pydantic_settings import BaseSettings


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")
down: str = os.getenv("DOWN", "down")
web_server: str = os.getenv("WEB_SERVER", "web_server")

db_url: str = os.getenv("MONGO_URL", "")
db_name: str = os.getenv("MONGO_DB", "")
collection: str = os.getenv("MONGO_COLLECTION", "")
test_db_name: str = os.getenv("MONGO_TEST_DB", "")
mongodb_database: str = os.getenv("MONGODB_DATABASE", "")
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_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.db_name, 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
6 changes: 0 additions & 6 deletions pytest.ini

This file was deleted.

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.test_db_name, 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 d969d2e

Please sign in to comment.