-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor environment variable loading using pydantic-settings
- Loading branch information
Showing
6 changed files
with
77 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
PORT=8000 | ||
IP_ADDRESS="localhost" | ||
SMTP_SERVER ='smtp.gmail.com' | ||
EMAIL=your-email | ||
EMAIL_PW=email-password | ||
EMAIL_RECEIVER=email-to-receive-surf-report | ||
COMMAND=localhost:8000 | ||
SUBJECT='Surf Report' | ||
SMTP_SERVER="smtp.gmail.com" | ||
SMTP_PORT=587 | ||
EMAIL="your-email" | ||
EMAIL_PW="email-password" | ||
EMAIL_RECEIVER="email-to-receive-surf-report" | ||
COMMAND="localhost:8000" | ||
SUBJECT="Surf Report" |
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from pathlib import Path | ||
from typing import Literal, Union | ||
|
||
from pydantic import EmailStr, Field, IPvAnyAddress | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
|
||
class CommonSettings(BaseSettings): | ||
""" | ||
Base class for defining common settings. | ||
model_config (SettingsConfigDict)Configuration dictionary | ||
for specifying the settings file and encoding. | ||
""" | ||
model_config = SettingsConfigDict( | ||
env_file=f"{Path(__file__).parent.parent}/.env", | ||
env_file_encoding="utf-8", | ||
extra="ignore", | ||
) | ||
|
||
|
||
class ServerSettings(CommonSettings): | ||
""" | ||
Class for defining server env settings. | ||
""" | ||
|
||
PORT: int = Field(default=8000) | ||
IP_ADDRESS: Union[IPvAnyAddress | Literal["localhost"]] = Field( | ||
default="localhost" | ||
) | ||
|
||
|
||
class EmailSettings(ServerSettings): | ||
""" | ||
Class for defining email env settings. | ||
""" | ||
|
||
SMTP_SERVER: str = Field(default="smtp.gmail.com") # default gmail server | ||
SMTP_PORT: int = Field(default=587) # default 587 | ||
EMAIL: EmailStr # required | ||
EMAIL_PW: str # required | ||
EMAIL_RECEIVER: EmailStr # required | ||
COMMAND: str = Field( | ||
default_factory=lambda cls: f"{cls.IP_ADDRESS}:{cls.PORT}" | ||
) | ||
SUBJECT: str = Field(default="Surf Report") |