-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from grillazz/refactor
Refactor database url
- Loading branch information
Showing
5 changed files
with
89 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,48 @@ | ||
import os | ||
|
||
from pydantic import PostgresDsn, RedisDsn | ||
from pydantic_settings import BaseSettings | ||
from pydantic import PostgresDsn, RedisDsn, computed_field | ||
from pydantic_core import MultiHostUrl | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
|
||
class Settings(BaseSettings): | ||
asyncpg_url: PostgresDsn = os.getenv("SQL_URL") | ||
model_config = SettingsConfigDict( | ||
env_file=".env", | ||
env_ignore_empty=True, | ||
extra="ignore" | ||
) | ||
redis_url: RedisDsn = os.getenv("REDIS_URL") | ||
jwt_algorithm: str = os.getenv("JWT_ALGORITHM") | ||
jwt_expire: int = os.getenv("JWT_EXPIRE") | ||
|
||
SQL_USER: str | ||
SQL_PASS: str | ||
SQL_HOST: str | ||
SQL_DB: str | ||
|
||
@computed_field | ||
@property | ||
def asyncpg_url(self) -> PostgresDsn: | ||
""" | ||
This is a computed field that generates a PostgresDsn URL for asyncpg. | ||
The URL is built using the MultiHostUrl.build method, which takes the following parameters: | ||
- scheme: The scheme of the URL. In this case, it is "postgresql+asyncpg". | ||
- username: The username for the SQL database, retrieved from the SQL_USER environment variable. | ||
- password: The password for the SQL database, retrieved from the SQL_PASS environment variable. | ||
- host: The host of the SQL database, retrieved from the SQL_HOST environment variable. | ||
- path: The path of the SQL database, retrieved from the SQL_DB environment variable. | ||
Returns: | ||
PostgresDsn: The constructed PostgresDsn URL for asyncpg. | ||
""" | ||
return MultiHostUrl.build( | ||
scheme="postgresql+asyncpg", | ||
username=self.SQL_USER, | ||
password=self.SQL_PASS, | ||
host=self.SQL_HOST, | ||
path=self.SQL_DB, | ||
) | ||
|
||
|
||
settings = Settings() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters