|
1 | 1 | import os |
2 | 2 |
|
| 3 | +from pydantic import MongoDsn, computed_field |
| 4 | +from pydantic_core import MultiHostUrl |
3 | 5 | from pydantic_settings import BaseSettings |
4 | 6 |
|
5 | 7 |
|
6 | 8 | 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""" |
22 | 10 | environment: str = os.getenv("ENVIRONMENT", "local") |
23 | 11 | testing: str = os.getenv("TESTING", "0") |
24 | 12 | up: str = os.getenv("UP", "up") |
25 | 13 | down: str = os.getenv("DOWN", "down") |
26 | 14 | web_server: str = os.getenv("WEB_SERVER", "web_server") |
27 | 15 |
|
28 | 16 | 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 | + ) |
32 | 39 |
|
33 | 40 |
|
34 | 41 | settings = Settings() |
0 commit comments