Skip to content

Commit d969d2e

Browse files
authored
Merge pull request #64 from grillazz/63-refactor-project-config
63 refactor project config
2 parents a2f1793 + 5d623b5 commit d969d2e

File tree

9 files changed

+47
-45
lines changed

9 files changed

+47
-45
lines changed

.env.example

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ UP=up
77
DOWN=down
88
WEB_SERVER=web_server
99

10-
MONGO_HOST=mongodb
11-
MONGO_PORT=27017
12-
MONGO_USER=farmer
13-
MONGO_PASS=tractor
14-
MONGO_DB=greenhouse
15-
MONGO_COLLECTION=greens
16-
MONGO_TEST_DB=farmland
10+
MONGODB_HOST=mongodb
11+
MONGODB_PORT=27017
12+
MONGODB_USER=farmer
13+
MONGODB_PASSWORD=tractor
14+
MONGODB_DATABASE=greenhouse
15+
MONGODB_COLLECTION=greens
16+
MONGODB_TEST=farmland
1717
MONGO_URL=mongodb://farmer:tractor@mongodb:27017/?retryWrites=true&w=majority
1818

.github/workflows/build-and-test.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ jobs:
2323
UP: up
2424
DOWN: down
2525
WEB_SERVER: web_server
26-
MONGO_HOST: mongodb
27-
MONGO_PORT: 27017
28-
MONGO_USER: farmer
29-
MONGO_PASS: tractor
30-
MONGO_DB: greenhouse
31-
MONGO_COLLECTION: greens
32-
MONGO_TEST_DB: farmland
26+
MONGODB_HOST: mongodb
27+
MONGODB_PORT: 27017
28+
MONGODB_USER: farmer
29+
MONGODB_PASSWORD: tractor
30+
MONGODB_DATABASE: greenhouse
31+
MONGODB_COLLECTION: greens
32+
MONGODB_TEST: farmland
33+
MONGODB_PARAMS: retryWrites=true&w=majority
3334
MONGO_URL: mongodb://farmer:[email protected]:27017/?retryWrites=true&w=majority
3435

3536
services:

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Jakub Miazek
3+
Copyright (c) 2024 Jakub Miazek
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ services:
2525
ports:
2626
- "27017:27017"
2727
environment:
28-
- "MONGO_INITDB_DATABASE=${MONGO_DB}"
29-
- "MONGO_INITDB_ROOT_USERNAME=${MONGO_USER}"
30-
- "MONGO_INITDB_ROOT_PASSWORD=${MONGO_PASS}"
28+
- "MONGO_INITDB_DATABASE=${MONGODB_DATABASE}"
29+
- "MONGO_INITDB_ROOT_USERNAME=${MONGODB_USER}"
30+
- "MONGO_INITDB_ROOT_PASSWORD=${MONGODB_PASSWORD}"
3131
command:
3232
mongod --quiet --logpath /dev/null

greens/config.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
11
import os
22

3+
from pydantic import MongoDsn, computed_field
4+
from pydantic_core import MultiHostUrl
35
from pydantic_settings import BaseSettings
46

57

68
class Settings(BaseSettings):
7-
"""
8-
Represents the configuration settings for the application.
9-
10-
Args:
11-
environment (str): The environment in which the application is running. Defaults to "local".
12-
testing (str): The testing mode of the application. Defaults to "0".
13-
up (str): The up status of the application. Defaults to "up".
14-
down (str): The down status of the application. Defaults to "down".
15-
web_server (str): The web server used by the application. Defaults to "web_server".
16-
db_url (str): The URL of the MongoDB database.
17-
db_name (str): The name of the MongoDB database.
18-
collection (str): The name of the MongoDB collection.
19-
test_db_name (str): The name of the MongoDB test database.
20-
21-
"""
9+
"""Settings for the application"""
2210
environment: str = os.getenv("ENVIRONMENT", "local")
2311
testing: str = os.getenv("TESTING", "0")
2412
up: str = os.getenv("UP", "up")
2513
down: str = os.getenv("DOWN", "down")
2614
web_server: str = os.getenv("WEB_SERVER", "web_server")
2715

2816
db_url: str = os.getenv("MONGO_URL", "")
29-
db_name: str = os.getenv("MONGO_DB", "")
30-
collection: str = os.getenv("MONGO_COLLECTION", "")
31-
test_db_name: str = os.getenv("MONGO_TEST_DB", "")
17+
mongodb_database: str = os.getenv("MONGODB_DATABASE", "")
18+
mongodb_collection: str = os.getenv("MONGODB_COLLECTION", "")
19+
mongodb_test: str = os.getenv("MONGODB_TEST", "")
20+
21+
MONGODB_HOST: str
22+
MONGODB_PORT: int
23+
MONGODB_USER: str
24+
MONGODB_PASSWORD: str
25+
MONGODB_PARAMS: str
26+
27+
@computed_field
28+
@property
29+
def mongodb_url(self) -> MongoDsn:
30+
return MultiHostUrl.build(
31+
scheme="mongodb",
32+
host=self.MONGODB_HOST,
33+
port=self.MONGODB_PORT,
34+
username=self.MONGODB_USER,
35+
password=self.MONGODB_PASSWORD,
36+
37+
path=self.MONGODB_PARAMS
38+
)
3239

3340

3441
settings = Settings()

greens/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async def lifespan(app: FastAPI):
1616
app.state.logger = get_logger(__name__)
1717
app.state.logger.info("Starting greens on your farmland...mmm")
1818
app.state.mongo_client, app.state.mongo_db, app.state.mongo_collection = await init_mongo(
19-
global_settings.db_name, global_settings.db_url, global_settings.collection
19+
global_settings.mongodb_database, global_settings.mongodb_url, global_settings.mongodb_collection
2020
)
2121
try:
2222
yield

greens/routers/v1/vegs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from greens.schemas.vegs import Document, DocumentResponse, ObjectIdField
77
from greens.services.repository import create_document, retrieve_document
88

9-
collection = global_settings.collection
9+
collection = global_settings.mongodb_collection
1010

1111
router = APIRouter()
1212

pytest.ini

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ async def client() -> AsyncGenerator:
2525
) as client:
2626
app.state.logger = get_logger(__name__)
2727
app.state.mongo_client, app.state.mongo_db, app.state.mongo_collection = await init_mongo(
28-
global_settings.test_db_name, global_settings.db_url, global_settings.collection
28+
global_settings.mongodb_test, global_settings.db_url, global_settings.mongodb_collection
2929
)
3030
yield client

0 commit comments

Comments
 (0)