Skip to content

Commit

Permalink
feat: add uvicorn settings
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasanchez committed Aug 11, 2023
1 parent 4c99fbe commit 2268494
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
12 changes: 11 additions & 1 deletion src/template/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@
# pylint: disable=wrong-import-position
import uvicorn

uvicorn.run("template.main:app", host="0.0.0.0")
import template.settings.uvicorn_settings

settings = template.UvicornSettings()

uvicorn.run(
"template.main:app",
host=settings.HOST,
port=settings.PORT,
log_level=settings.LOG_LEVEL,
reload=settings.RELOAD,
)
21 changes: 8 additions & 13 deletions src/template/settings/api_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
API Settings
"""
from pydantic import BaseModel, EmailStr, HttpUrl
from pydantic_settings import BaseSettings
from pydantic_settings import BaseSettings, SettingsConfigDict

from template.version import __version__

Expand Down Expand Up @@ -58,6 +58,9 @@ class ApplicationSettings(BaseSettings):
PROJECT_CONTACT (ContactInfo): FastAPI project contact details.
VERSION (str): Application version.
DOCS_URL (str): Path where swagger ui will be served at.
Resources:
1. https://docs.pydantic.dev/latest/usage/pydantic_settings/
"""

DEBUG: bool = True
Expand All @@ -73,15 +76,7 @@ class ApplicationSettings(BaseSettings):
# All your additional application configuration should go either here or in
# separate file in this submodule.

class Config:
"""Config subclass needed to customize BaseSettings settings.
Attributes:
case_sensitive (bool): When case_sensitive is True, the environment
variable names must match field names (optionally with a prefix)
env_prefix (str): The prefix for environment variable.
Resources:
https://pydantic-docs.helpmanual.io/usage/settings/
"""

case_sensitive = True
env_prefix = "FASTAPI_"
model_config = SettingsConfigDict(
case_sensitive=True,
env_prefix="FASTAPI_",
)
40 changes: 40 additions & 0 deletions src/template/settings/uvicorn_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Uvicorn settings
"""

from pydantic import IPvAnyAddress
from pydantic_settings import BaseSettings, SettingsConfigDict


class UvicornSettings(BaseSettings):
"""Define UVICORN configuration model.
Constructor will attempt to determine the values of any fields not passed
as keyword arguments by reading from the environment. Default values will
still be used if the matching environment variable is not set.
Environment variables:
* UVICORN_HOST
* UVICORN_PORT
* UVICORN_LOG_LEVEL
* UVICORN_RELOAD
Attributes:
HOST (IPvAnyAddress): Host to run application on.
PORT (int): Port to run application on.
LOG_LEVEL (str): Logging level.
RELOAD (bool): Enable/disable auto-reload.
Resources:
1. https://docs.pydantic.dev/latest/usage/pydantic_settings/
"""

HOST: IPvAnyAddress = IPvAnyAddress("127.0.0.1")
PORT: int = 8000
LOG_LEVEL: str = "info"
RELOAD: bool = False

model_config = SettingsConfigDict(
case_sensitive=True,
env_prefix="UVICORN_",
)
21 changes: 21 additions & 0 deletions tests/unit/settings/test_uvicorn_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Uvicorn settings tests.
"""
from template.settings.uvicorn_settings import UvicornSettings


class TestUvicornSettings:
"""
Test suite for Uvicorn Settings
"""

def test_uvicorn_default_values(self):
"""
Test API default values
"""
settings = UvicornSettings()

assert not settings.RELOAD
assert settings.LOG_LEVEL
assert settings.PORT
assert settings.HOST

0 comments on commit 2268494

Please sign in to comment.