-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Pydantic BaseSettings for config settings #87
Merged
+173
−140
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
25fd237
Use Pydantic BaseSettings for config settings
StephenBrown2 a7db29f
Update fastapi dep to >=0.47.0 and email_validator to email-validator
StephenBrown2 194132c
Fix deprecation warning for Pydantic >=1.0
StephenBrown2 6c5a0d4
Properly support old-format comma separated strings for BACKEND_CORS_…
StephenBrown2 51ed63e
Merge branch 'master' into pydantic_basesettings
tiangolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
141 changes: 89 additions & 52 deletions
141
{{cookiecutter.project_slug}}/backend/app/app/core/config.py
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,55 +1,92 @@ | ||
import os | ||
import secrets | ||
from typing import List | ||
|
||
from pydantic import AnyHttpUrl, BaseSettings, EmailStr, HttpUrl, PostgresDsn, validator | ||
|
||
def getenv_boolean(var_name, default_value=False): | ||
result = default_value | ||
env_value = os.getenv(var_name) | ||
if env_value is not None: | ||
result = env_value.upper() in ("TRUE", "1") | ||
return result | ||
|
||
|
||
API_V1_STR = "/api/v1" | ||
|
||
SECRET_KEY = os.getenvb(b"SECRET_KEY") | ||
if not SECRET_KEY: | ||
SECRET_KEY = os.urandom(32) | ||
|
||
ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 * 8 # 60 minutes * 24 hours * 8 days = 8 days | ||
|
||
SERVER_NAME = os.getenv("SERVER_NAME") | ||
SERVER_HOST = os.getenv("SERVER_HOST") | ||
BACKEND_CORS_ORIGINS = os.getenv( | ||
"BACKEND_CORS_ORIGINS" | ||
) # a string of origins separated by commas, e.g: "http://localhost, http://localhost:4200, http://localhost:3000, http://localhost:8080, http://local.dockertoolbox.tiangolo.com" | ||
PROJECT_NAME = os.getenv("PROJECT_NAME") | ||
SENTRY_DSN = os.getenv("SENTRY_DSN") | ||
|
||
POSTGRES_SERVER = os.getenv("POSTGRES_SERVER") | ||
POSTGRES_USER = os.getenv("POSTGRES_USER") | ||
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD") | ||
POSTGRES_DB = os.getenv("POSTGRES_DB") | ||
SQLALCHEMY_DATABASE_URI = ( | ||
f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_SERVER}/{POSTGRES_DB}" | ||
) | ||
|
||
SMTP_TLS = getenv_boolean("SMTP_TLS", True) | ||
SMTP_PORT = None | ||
_SMTP_PORT = os.getenv("SMTP_PORT") | ||
if _SMTP_PORT is not None: | ||
SMTP_PORT = int(_SMTP_PORT) | ||
SMTP_HOST = os.getenv("SMTP_HOST") | ||
SMTP_USER = os.getenv("SMTP_USER") | ||
SMTP_PASSWORD = os.getenv("SMTP_PASSWORD") | ||
EMAILS_FROM_EMAIL = os.getenv("EMAILS_FROM_EMAIL") | ||
EMAILS_FROM_NAME = PROJECT_NAME | ||
EMAIL_RESET_TOKEN_EXPIRE_HOURS = 48 | ||
EMAIL_TEMPLATES_DIR = "/app/app/email-templates/build" | ||
EMAILS_ENABLED = SMTP_HOST and SMTP_PORT and EMAILS_FROM_EMAIL | ||
|
||
FIRST_SUPERUSER = os.getenv("FIRST_SUPERUSER") | ||
FIRST_SUPERUSER_PASSWORD = os.getenv("FIRST_SUPERUSER_PASSWORD") | ||
|
||
USERS_OPEN_REGISTRATION = getenv_boolean("USERS_OPEN_REGISTRATION") | ||
|
||
EMAIL_TEST_USER = "test@example.com" | ||
|
||
class Settings(BaseSettings): | ||
|
||
API_V1_STR: str = "/api/v1" | ||
|
||
SECRET_KEY: str = secrets.token_urlsafe(32) | ||
|
||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8 # 60 minutes * 24 hours * 8 days = 8 days | ||
|
||
SERVER_NAME: str | ||
SERVER_HOST: AnyHttpUrl | ||
# BACKEND_CORS_ORIGINS is a JSON-formatted list of origins | ||
# e.g: '["http://localhost", "http://localhost:4200", "http://localhost:3000", \ | ||
# "http://localhost:8080", "http://local.dockertoolbox.tiangolo.com"]' | ||
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = [] | ||
|
||
@validator("BACKEND_CORS_ORIGINS", pre=True) | ||
def assemble_cors_origins(cls, v): | ||
if isinstance(v, str) and not v.startswith("["): | ||
return [i.strip() for i in v.split(",")] | ||
return v | ||
|
||
PROJECT_NAME: str | ||
SENTRY_DSN: HttpUrl = None | ||
|
||
@validator("SENTRY_DSN", pre=True) | ||
def sentry_dsn_can_be_blank(cls, v): | ||
if len(v) == 0: | ||
return None | ||
return v | ||
|
||
POSTGRES_SERVER: str | ||
POSTGRES_USER: str | ||
POSTGRES_PASSWORD: str | ||
POSTGRES_DB: str | ||
SQLALCHEMY_DATABASE_URI: PostgresDsn = None | ||
|
||
@validator("SQLALCHEMY_DATABASE_URI", pre=True) | ||
def assemble_db_connection(cls, v, values): | ||
if isinstance(v, str): | ||
return v | ||
return PostgresDsn.build( | ||
scheme="postgresql", | ||
user=values.get("POSTGRES_USER"), | ||
password=values.get("POSTGRES_PASSWORD"), | ||
host=values.get("POSTGRES_SERVER"), | ||
path=f"/{values.get('POSTGRES_DB') or ''}", | ||
) | ||
|
||
SMTP_TLS: bool = True | ||
SMTP_PORT: int = None | ||
SMTP_HOST: str = None | ||
SMTP_USER: str = None | ||
SMTP_PASSWORD: str = None | ||
EMAILS_FROM_EMAIL: EmailStr = None | ||
EMAILS_FROM_NAME: str = None | ||
|
||
@validator("EMAILS_FROM_NAME") | ||
def get_project_name(cls, v, values): | ||
if not v: | ||
return values["PROJECT_NAME"] | ||
return v | ||
|
||
EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 48 | ||
EMAIL_TEMPLATES_DIR: str = "/app/app/email-templates/build" | ||
EMAILS_ENABLED: bool = False | ||
|
||
@validator("EMAILS_ENABLED", pre=True) | ||
def get_emails_enabled(cls, v, values): | ||
return bool( | ||
values.get("SMTP_HOST") | ||
and values.get("SMTP_PORT") | ||
and values.get("EMAILS_FROM_EMAIL") | ||
) | ||
|
||
EMAIL_TEST_USER: EmailStr = "test@example.com" | ||
|
||
FIRST_SUPERUSER: EmailStr | ||
FIRST_SUPERUSER_PASSWORD: str | ||
|
||
USERS_OPEN_REGISTRATION: bool = False | ||
|
||
class Config: | ||
case_sensitive = True | ||
|
||
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
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
4 changes: 2 additions & 2 deletions
4
{{cookiecutter.project_slug}}/backend/app/app/tests/api/api_v1/test_celery.py
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will conflict with PR #106